1

ok im sending an int from one java program to another (on same computer at the minute). However, sometimes I'm getting an exception and it wont connect:

Exception in thread "main" java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at java.net.Socket.connect(Socket.java:478) at java.net.Socket.(Socket.java:375) at java.net.Socket.(Socket.java:189) at Client.main(Client.java:6)

Here is the code for sending:

        Socket socket = new Socket("localhost" , 8080);
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    out.writeInt(5);
    socket.close();

and for receiving:

    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = serverSocket.accept();

    DataInputStream din = new DataInputStream(socket.getInputStream());
    System.out.println(din.readInt());
            socket.close();

It's just odd because sometimes it will work and sometimes not. Does anyone have any ideas as to why?

user650309
  • 2,639
  • 7
  • 28
  • 47
  • 1
    Just a side-comment: remember to close your DataOutputStream after using it. – Mia Clarke Mar 15 '11 at 14:54
  • 4
    You positive the serverSocket is listening when the socket is being used? – vickirk Mar 15 '11 at 14:54
  • 2
    You get the error when creating the socket, thus it sometimes fail because there is no server. – ZeissS Mar 15 '11 at 14:55
  • I don't think it's related to what you send. try sending something else, like a byte array (remember to read the correct type as well) – MByD Mar 15 '11 at 14:56
  • 1
    Post the **root cause** of the exception please! – Buhake Sindi Mar 15 '11 at 14:56
  • 1
    Also, you know that your server code will only handle one client before dying right? (At least from what you've shown us). – Mark Peters Mar 15 '11 at 15:02
  • Could it be the you are still in the TCP TIME-WAIT delay? According to this (http://msdn.microsoft.com/en-us/library/ms819739.aspx), its 4 mins on Windows. – Chuk Lee Mar 15 '11 at 15:05

2 Answers2

1

Quoting from the description of the exception:

"Signals that an error occurred while attempting to connect a socket to a remote address and port. Typically, the connection was refused remotely (e.g., no process is listening on the remote address/port)."

Most likely is that nothing is listening to the port. Your server code appears to accept one connection and then shut down, so unless you are restarting the server for every connection this is your most likely cause. If not, check your server app is running.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
1

I bet you get this error if you:

  1. start your server
  2. Start your client
  3. Start your client again, without restarting the server

Your server only accepts one connection and then terminates.

If you want to accept an indefinite number of sequential connections, surround your server code with a loop like so:

ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
  Socket socket = serverSocket.accept();
  DataInputStream din = new DataInputStream(socket.getInputStream());      
  System.out.println(din.readInt());              
  socket.close();
}

After servicing a request it will listen again for another request or take a waiting request.

Matt Wonlaw
  • 12,146
  • 5
  • 42
  • 44
  • Just to say I fixed the problem. Issue was with closing socket at wrong time and a try....catch inside a catch. Thanks for your help! – user650309 Mar 28 '11 at 18:52