0

I have some code here to connect to a server and I'm getting the error java.net.ConnectException: Connection Refused.

I tried using ping on the command line and I received packets of data so I know that it's working.

I also tried using telnet to connect to my IP Address and it gave me the same Connection Refused error. Is there something wrong with my code? I'm unsure as to why my connection is getting refused.

Client:

public class InstantMessagingClient 
{
    private Socket socket;

    public InstantMessagingClient(String serverName, int port)
    {

        new Thread() {
            public void run()
            {
                try
                {
                    System.out.println("Connecting to " + serverName + " and port: " + port);
                    //socket.setSoTimeout(10000);
                    socket = new Socket(serverName, port);
                    System.out.println("Client: connected to server at address: " + socket.getLocalAddress() + " and port: " + socket.getPort());
                }
                catch(Exception e)
                {
                    System.out.println(e.toString());
                }

            }
        }.start();
    }
}

Server:

    public class InstantMessagingServer 
{
    private int port;
    private Socket clientSocket;
    private ServerSocket serverSocket;

    public InstantMessagingServer(int port)
    {
        try
        {
            this.port = port;
            serverSocket = new ServerSocket(port);
            serverSocket.setSoTimeout(10000);
            System.out.println("Server: Connected to port");
            new Thread() 
            {
                public void run()
                {
                    while(true)
                    {
                        try
                        {
                            System.out.println("Server: Waiting for port " + serverSocket.getLocalPort());
                            System.out.println(serverSocket.getLocalSocketAddress());
                            clientSocket = serverSocket.accept();
                            System.out.println("Server: Just connected to " + clientSocket.getRemoteSocketAddress());

                        }
                        catch(Exception e)
                        {
                            System.out.println(e.toString());
                            break;
                        }
                    }
                }
            }.start();
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }

}

Main:

public void main()
{
InstantMessagingServer server = new InstantMessagingServer(18638);
InstantMessagingClient client = new InstantMessagingClient("(My IP Address)", 18638);
}

1 Answer 1

1

I'm getting the error java.net.ConnectException: Connection Refused.

That has exactly one meaning. Nothing was listening at the IP:port you tried to connect to.

I tried using ping on the command line and I received packets of data so I know that it's working.

You know that the host is working. Doesn't prove anything about the port or the service you're trying to connect to.

I also tried using telnet to connect to my IP Address and it gave me the same Connection Refused error.

For the same reason.

Is there something wrong with my code?

Either the IP:port is wrong or the server simply hasn't been started.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.