简体   繁体   中英

How to find which TcpClient had send a message in C#

I created a system to connect a series of TcpClient to a TcpListener and exchange data in a chat-like system. Once the connection is established, the server adds the client to a List, and starts reading the stream waiting for messages.

Once the server has received a message, is there a build-in method to know which client sent it?

Alternative: I thought of attaching the client's RemoteEndPoint (Ip + port) to the message to use it as an identifier, which should be the same between the two versions of the TcpClient on the client and on the server, and unique compared to the other clients. Am I right?

Once the server has received a message, is there a build-in method to know which client sent it?

Anything there is for you to add; typically you would maintain some nominal per-connection state alongside each Socket / TcpClient / TcpListener / NetworkStream / Pipe (your choice of API) instance, so that when receiving a message you can trivially look up whatever you need. In some cases, it may be succifient to just use the Socket / TcpClient / etc instance as a key, but more often you'll have some kind of user-state/context information. This is basically entire up to you to implement.

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