Search code examples
javasockets

What does "Creates a socket address from an IP address" mean?


I was reading the javadoc for InetSocketAddress at http://docs.oracle.com/javase/8/docs/api/ and was wondering what the following phrase means:

Creates a socket address from an IP address and a port number.

What I want to do is create a socket on the local box that listens for incoming connections on the same port and begins communication with that client. I was using "localhost"/80 for the host name on my server, but netstat -l doesn't show anything listening on port 80.

Am I going about this the wrong way?


Solution

  • Then you need to create a ServerSocket.

    From memmory: It is something like

    ServerSocket mySocket =new ServerSocket(1234); // Listen on port 1234 on all network interfaces.
    
    Socket incommingSocket=mySoccket.accept(); // Wait until a client connect. Once a client connect return the socket for the connection.
    

    And then you can call getInputStream/getOutputStream on the incommingSocket object.