TcpListener and TcpClient ports

I have searched for the answer to this question but I find conflicting information in most references to it that I could find.

As I understood, when a listener on a server receives an incoming connection request from a client, the newly created TcpClient object on the server connects to the client using a different port so that the listener can continue to listen on the listening port. This implies that multiple incoming connections from clients would require that the server assign a separate port for each for future conversations. This tallies if a port is a memory address as each socket would need to have its own buffers.

I am developing a multithreaded client / server app which happily holds a whole conversation with one client (client sends multiple files with ack messages from the server between each), but when I get a second client to talk at the same time the messages were getting garbled.

I ran Ethereal and although I dont understand a lot of what it says, it does appear to indicate that there are two different client ports talking to just one server port (the port I have assigned to the listener). I know that multithreaded socket programming is fraught with dangers but I have debugged sufficiently to show that two distinct threads are active and I have used Synclock to lock out critical functions.

I dont know whether the TcpClients on the server should have separate ports. Please any advice would be much appreciated.

Thanks


Answer this question

TcpListener and TcpClient ports

  • Ahmadbondk

    Can you explain your question
    Like Mike Flasko said above, don't just look at the server port.
    It is the tuple that matters.
    Hence eventhough it appears that the same port is being used, in
    reality the stack maintains multiple connections.

    Async or Sync does not matter



  • Steelgrave

    I beleive the answer to your question is no, the server will retain a single port number.  The differentiation is done based on the tuple (client port, client address).  For example, if I have a TCP server listening on port 5000 and then have 2 TCP clients connect to the server (in this case server and client are on the same machine), a snippet of the output from netstat -a is:

    TCP    MFLASKO-M3:5000        xxxxx.xxxx.corp.microsoft.com:0  LISTENING  TCP    MFLASKO-M3:42510       xxxxx.xxxxxx.corp.microsoft.com:0  LISTENING 
    TCP    MFLASKO-M3:1139        xxxxx.xxxxxx.corp.microsoft.com:0  LISTENING

      TCP    MFLASKO-M3:2022        localhost:5000         ESTABLISHED
      TCP    MFLASKO-M3:2025        localhost:5000         ESTABLISHED
      TCP    MFLASKO-M3:5000        localhost:2022         ESTABLISHED
      TCP    MFLASKO-M3:5000        localhost:2025         ESTABLISHED

    You may want to check out these links for more information:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconusingnon-blockingserversocket.asp

    http://msdn.microsoft.com/library/en-us/cpguide/html/cpconsocketcodeexamples.asp frame=true



  • Satan Klaus

    Much appreciated Mike, thanks.


  • BPM_CTO

    I have used the Asynchronous Server Socket Example but how the server can send a message to all clients
  • TcpListener and TcpClient ports