简体   繁体   中英

Making a client respond to incoming connection requests in C++

I am building a dummy P2P application in C++ and have a conceptual doubt. (I am a complete beginner with networking.)

My client A creates a socket and connects to a server. Another client B comes and asks the server about the IP and port of the client A. Client B now wants to open a connection directly with A. I create another thread in client A that wants to do the listening for incoming connection requests. Please verify my understanding through these points.

  1. The file descriptor that I have after creating the socket in client A for the connection with the server can only be used by the server to read/write from it.

  2. The connection requests that Client B sends to A will end up in some other queue which needs to be handled separately and will be invisible to the connection that client A already has. Basically this implies that what goes into the file descriptor, goes through the associated port but what comes into the port may not get into the file descriptor. Only from the right source.

  3. Client A need to create a new socket bonded with the same port and address as the previous one to handle the incoming requests.

Best,

TCP is a point-to-point connection, it can only be used for a single connection from your application to another. If you want to accept incoming connections you need a second socket (passive, listening) which is used as a connection point for other applications. When another application connects to this (passive, listening) socket you get a third socket for the actual connection.

So:

  1. Yes.

  2. Yes.

  3. No. Your connection from A to the server can use any arbitrary port. The (passive, listening) socket in A needs to be bound to a "well-known" port that application B connects to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM