Java Socket Programming



Hi, I'm a 2nd year undergrad with a personal project on my agenda.
The project I've been working on is a ChatApplication under the
Client/Server Architecture, which is still in its early stages. I'm
new to Socket Programming and have been putting in efforts to dig up
some starter tutorials. Just when I think i'm getting the hang of the
concepts, I've hit a snag.

From my understanding, only ONE Socket can be bound to ANY given port.
Through some toying with the java.net package and its api, I've written
a very simple server.

----------------------------------------------------------------------------------
import java.net.*;

public class SimpleServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;

try {
serverSocket = new ServerSocket(8000);
} catch (IOException e) {
System.err.println("Could not listen on port: 8000.");
System.exit(-1);
}

while (listening) {
System.out.println(serverSocket.accept());
}

serverSocket.close();
}
}
----------------------------------------------------------------------------------

What I've discovered from this SimpleServer.java is that, when the
"accept( )" method from ServerSocket returns, the "new Socket" returned
appears to be bound to port 8000.
But isn't that port already in use, since that's the port SimpleServer
is listening on?
Furthermore, if multiple connections is attempted, it appears that
every "new Socket" returned by "accept( )" will be bound to port 8000?
Wouldn't that mean there exist multiple client sockets all connected to
the server on ONE port?

When a few friends and I try to telnet on the port that SimpleServer is
listening on, here's the output on command prompt:

-----------------------------------------------------------------------------------
Socket[addr=65.93.27.235,port=61159,localport=8000]
Socket[addr=127.0.0.1,port=1597,localport=8000]
Socket[addr=70.28.249.97,port=2074,localport=8000]
-----------------------------------------------------------------------------------

From the output above, it appears all client socket connections are
connected on my localport=8000. Is this the correct behavious that I
should be expecting? I would like to think that the "new Sockets"
returned by "accept( )" would be bound on a different available port
than the one that SimpleServer is listening on, but apparently that
isn't the case. This result is mind-boggling for me, and I am unsure
of how to interpret it. I hope that I've stated my concern well
enough, so I may receive some guidance to this problem I'm facing.
Thank you all.

Regards,
DaveL

.



Relevant Pages

  • Re: Linux- Socket server listen on localhost, not on the machine IP
    ... my server socket is listening on the wanted port but at ... There are various ServerSocket constructors to let you do that. ... If you only specify a port number when you create the ServerSocket, ...
    (comp.lang.java.programmer)
  • Re: Invoking a method in another java application.
    ... > on a ServerSocket for incoming requests. ... When appis set up and listening, it can publish it's service on ... whatever port it happened to get / is configured for. ...
    (comp.lang.java.programmer)
  • Re: Best Plan of action for 2 forest.......
    ... PortQry reports the status of a port in one of the following ways: ... ..LISTENING This response indicates that a process is listening on the target ...
    (microsoft.public.windows.server.active_directory)
  • Re: RealVNC
    ... If we are talking about RealVNC it goes this way ... Then there is default Java listening port on port 5800 on the client machine ...
    (microsoft.public.windows.server.sbs)
  • Re: Cant join a domain
    ... Attempting to resolve name to IP address... ... TCP port 42: NOT LISTENING ...
    (microsoft.public.windows.server.active_directory)