This discussion is archived
13 Replies Latest reply: May 3, 2009 10:29 PM by 807588 RSS

Question about socket communication.

807588 Newbie
Currently Being Moderated
Hi.

I have question about socket. I have a serwer socket on port 4444. To this socket I connect two or more clients socket. On server after accept() I have two or more object instans of Socket class. Now - every "accept" socket has a local port 4444 !!! Now is my question. How OS or JVM know where to send data (to accept socket) when I send data from client socket ? I always think that ip and port is unique to every accept socket - but that is not true. Is there any additional data sended when I send my data (additional data that have information about socket or somethink like this ?)

Pleas answer !

Best regards
  • 1. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    Hi maciejmad,

    Don't worry for the JVM, he knows what he is doing. You have just to *{color:#0000ff}[manage a pool of threads|http://www.java2s.com/Code/Java/Network-Protocol/Manageapoolofthreadsforclients.htm]{color}* for your clients.
  • 2. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    Hi Chicon

    I know how to write serwer application with thread pool or somethink like this. I don,t asking about that.

    I want to know how OS or JVM know where to send reciving data. Ok I know ip and port, but on server side I have many accepted socket with this same local port. I repeat my question. How OS or JVM know witch socket to transfer data on this same server application ?
  • 3. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    maciejmad wrote:
    Hi Chicon

    How OS or JVM know witch socket to transfer data on this same server application ?
    If you have a close look at the ServerSocket methods, there's one called getLocalSocketAddress() : it gives the address of the socket. How does it get such infomation ? Answer = via TCP
  • 4. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    Have you tried it out?
  • 5. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    Yup ! Not recently, but as far as I can remember, it has worked !
  • 6. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    nvm.. but you do have multiple instances of sockets, each being unique even if they communicate through the same port.

    i.e.
    Socket client = server.accept();
    
    list.add( client );
    
    for ( Socket client : list )
    {
        InetAddress inet = client.getInetAddress();
        System.out.printf( "client name: %s   client address: %s\n", inet.getHostName(), inet.getHostAddress() );
    }
  • 7. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    Ok -      Once again.

    First - I dont send data from server to client but from client to server !

    Now :
    1. I start my server.
    2. Server accetp connection from client A - accept method create socket "CS-A" witch local port 4444 and local adress /127.0.0.1
    3. Server accept second connection from client B - accept method create socket "CS-B" witch local port 4444 and local adress /127.0.0.1 (the same port and adress as first accepted socket !!!!!!!!!!!!!!!!!!!)
    4. Client A send to server message "AAAA" to addres 127.0.0.1 and port 4444
    5. Client B send to server message "BBBB" to address 127.0.0.1 and port 4444 (this same adress and port as client A send)

    AND NOW:
    Server application get two message "AAAA" and "BBBB" but this two messages was distributed by socket "CS-A" and "CS-B".
    Socket CS-A recived only message "AAAA", and socket CS-B recived only message "BBBB".

    QUESTION : How server application knows where send this message. How she know that message "AAAA" must be send only to socket "CS-A" and message "BBBB" must be send only to socket CS-B ?
  • 8. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    QUESTION : How server application knows where send this message. How she know that message "AAAA" must be send only to socket "CS-A" and message "BBBB" must be send only to socket CS-B ?
    Besides the IP address and port number, each connection begins with a unique sequence number. The TCP stack uses this number to associate each incoming packet with the proper connection (and to be able to reassemble the message correctly, of course).
    First - I dont send data from server to client but from client to server !
    That's a good thing, because it won't work the other way around. ;)
  • 9. Re: Question about socket communication.
    EJP Guru
    Currently Being Moderated
    The TCP stack uses this number to associate each incoming packet with the proper connection
    No it doesn't. See below.
    (and to be able to reassemble the message correctly, of course).
    TCP uses the sequence number to correctly sequence incoming segments. That's what it's for - a sequence number. Nothing to do with identifying connections.

    The correct answer to the OP's question is that a TCP connection is characterized by 5 items:

    - the protocol (TCP)
    - the client IP address
    - the client port
    - the server IP address
    - the server port

    So even though the last two aren't unique across all the clients of the server, the 5-tuple is always unique. And it is present in every TCP segment that the server receives. So that's how TCP knows which socket to give the data to.

    >> First - I dont send data from server to client but from client to server !
    >
    That's a good thing, because it won't work the other way around. ;)
    I know, or hope, you're joking but this could seriously mislead future readers. For the record TCP/IP is full-duplex.
  • 10. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    So even though the last two aren't unique across all the clients of the server, the 5-tuple is always unique. And it is present in every TCP segment that the server receives. So that's how TCP knows which socket to give the data to.
    Right, well, I was way off. Very good explanation, by the way.
    I know, or hope, you're joking but this could seriously mislead future readers. For the record TCP/IP is full-duplex.
    What I had meant was that the client always initiates the connection, but yes, the statement was very misleading.
  • 11. Re: Question about socket communication.
    EJP Guru
    Currently Being Moderated
    Right, well, I was way off.
    So, with all due respect, why post guesses or misinformation at all? Your suggestion about sequence numbers doesn't even make sense. It's easy to imagine one connection overtaking another one's sequence number. It just wouldn't work.
  • 12. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    ejp - thx for answer that I need

    Rergards
  • 13. Re: Question about socket communication.
    807588 Newbie
    Currently Being Moderated
    So, with all due respect, why post guesses or misinformation at all? Your suggestion about sequence numbers doesn't even make sense. It's easy to imagine one connection overtaking another one's sequence number. It just wouldn't work.
    I'm not going to debate the degree of nonsense contained in my post. I was wrong, and I admit that. We are all susceptible to misconceptions from time to time. But to be fair, I probably should have qualified my statement with "I think" or such.

    Edited by: Sebastian.G on May 4, 2009 5:10 AM