1

I'm trying to setup a protected client and protected server connection using sockets. Whenever I run the program I receive a connection error saying the connection was refused. I'm using Java in Eclipse IDE running on MAC OS. Any ideas why this code cannot connect to my localhost?

Protected Client:

public static void main(String[] args) throws Exception 
    {
        String host = "localhost";
        int port = 7999;
        String user = "George";         
        String password = "abc123";     
        Socket s = new Socket(host, port);

        ProtectedClient client = new ProtectedClient();
        client.sendAuthentication(user, password, s.getOutputStream());

        s.close();
    }

Protected Server

public static void main(String[] args) throws Exception 
{
    int port = 7999;    //7999
    ServerSocket s = new ServerSocket(port);
    Socket client = s.accept();

    ProtectedServer server = new ProtectedServer();

    if (server.authenticate(client.getInputStream()))
      System.out.println("Client logged in.");
    else
      System.out.println("Client failed to log in.");

    s.close();
}

When I run the program I receive the following error:

Exception in thread "main" java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:228)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:431)
    at java.net.Socket.connect(Socket.java:527)
    at java.net.Socket.connect(Socket.java:476)
    at java.net.Socket.<init>(Socket.java:373)
    at java.net.Socket.<init>(Socket.java:216)
    at ProtectedClient.main(ProtectedClient.java:36)
8
  • 1
    Have you started your server? Also, it looks like your server will close after one client. Commented Nov 19, 2013 at 20:17
  • Did u check the proxy? Commented Nov 19, 2013 at 20:24
  • @constantlearner there's no usage of proxy in connecting to loopback interface such as localhost Commented Nov 19, 2013 at 20:26
  • Do you have a firewall blocking the port 7999? Commented Nov 19, 2013 at 20:31
  • It would be good practice to remove the throws Exception from the main method and handle the Exceptions where they occur Commented Nov 19, 2013 at 20:46

1 Answer 1

1

I have gotten this error before when using particular sockets. Try a different socket like 5687. (Or if you need that particular socket, try making sure your firewall allows it).

Make sure you call client.close() on the server as well. If your program worked once it might be blocking that port.

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

2 Comments

If that was the case a BindException would be thrown rather than a generic Exception. However, this is still good practice.
Thank you for the client.close() suggestion. I am now receiving "Client logged in." print line.

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.