Java Socket Programming

Discussion in 'Java' started by DaveL, Mar 31, 2006.

  1. DaveL

    DaveL Guest

    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.
    Through some toying with the package and its api, I've written
    a very simple server.


    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.");

    while (listening) {


    What I've discovered from this 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:

    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.

    DaveL, Mar 31, 2006
    1. Advertisements

  2. DaveL

    Ravi Guest

    Yes Beaton,
    I understood your explanation. I think his question is in such a way
    that if the serversocket listens in single port 8000.
    But how all sockets connected to the same listened port handles the
    information passing into that port.
    In programmatically , we are spawning the each socket connection into
    different threads, Correct?
    There should be virtual connection table in low level of network which
    correctly delivers the information to corresponding socket to our code
    Correct me if am wrong at any place

    Ravi, Mar 31, 2006
    1. Advertisements

  3. First, consider this:

    Socket s = new Socket(rhost, 8000); // connect to port 8000
    int rport = s.getPort(); // check remote port

    if (rport != 8000) {

    Are you suggesting that the result of connecting to a given port
    number *shouldn't* result in a connection to that same port number?

    What happens is that the Socket created in the call to accept() is a
    clone of the ServerSocket, and it inherits several attributes from the
    ServerSocket, including the port number.

    The reason this works is that each connection is uniquely identified
    by 4 attributes: the local address and local port number, and the
    remote address and remote port number.

    The remote addr and port for the ServerSocket are wildcards, since it
    isn't connected. The resulting connected Socket has these bound to the
    addr and port of the client's connecting Socket, so the ServerSocket
    and all Sockets created from it are unique and distinct, regardless of
    the local address and port.

    Two Sockets cannot be identical in all 4 of the attributes, so for
    example it isn't possible to create two ServerSockets listening on the
    same port, unless you also bind them to different local addresses
    (which requires you to have multiple local addresses to bind to).

    Gordon Beaton, Mar 31, 2006
  4. The sockets are different in more aspects than just the port number.
    Traffic arriving to the interface is dispatched by the network stack
    based on the *combination* of local address, local port, remote
    address and remote port. The port number alone does *not* identify the
    socket or the connection.

    Gordon Beaton, Mar 31, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.