0

I'm trying to create a new socket, but it keeps finding a IO Exception, saying

Couldn't get the I/O for the connection to: localhost.

Here is my code snippet:

  Socket mySocket = null;
  BufferedReader in = null;

try{
    mySocket = new Socket("localhost",4444);
    in = new BufferedReader( new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
    System.err.println("Don't know about host: localhost.");
    System.exit(1);
} catch (IOException e) {
    System.err.println("Couldn't get the I/O for " + "the connection to: localhost.");
    System.exit(1);
}

I tried to change my hostname "localhost" to also my hostname for my computer (i.e. typing in hostname in cmd to find hostname). Also tried another port, but it all keeps giving me the same error. What are some tips or ways to debug this? Or can you pinpoint what the problem is? Thank you for your help.

1
  • 2
    I don't think you can access the input stream until the socket is connected. That is, you need some server listening on that port also. Commented Aug 22, 2011 at 5:19

1 Answer 1

2

It means that no-one is listening to the port 4444 on your machine. Socket is the connector to channel that have 2 edges: sockets. Typically there is one ServerSocket on server side and Socket on client side.

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

2 Comments

Hmm I was trying to use a Socket on the client side to listen to tcp data come through from another application that has been compiled to a jar already. I guess from your comment this isn't possible unless I somehow configure the application that is a jar file to have a ServerSocket as well? Hope that makes sense.
@O_O AlexR is right. You have to configure a ServerSocket for the server application, so you will be able to receive the client requests.

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.