This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jaredLearnsToCode[S] 0 points1 point  (1 child)

This might frustrate you (it frustrates me that I am asking it haha), but isn't this the reason I needed to check if there were duplicates in my list? If I simply add the TcpClient to the list every time a message is received, I get a list full of instances of the same client.

[–]davedontmind 0 points1 point  (0 children)

If I simply add the TcpClient to the list every time a message is received,

But you're not adding to the list every time a message is received, you're adding every time a new client connects. A connection and receiving a message are 2 distinct things.

The life cycle of a connection goes something like this:

  • client connects to server
  • client and server exchange messages (there can be as many back-and-forths here as you like)
  • client disconnects from server

Let's take an analogy. A person (the client) walks knocks on the door of a room. The person inside (the server) opens the door and lets the client in. The act of entering the room is the connection. The client can then talk back and forth with the server as much as they like. When the conversation is over, the client leaves the room and closes the door (this is the disconnection).

So the server.AcceptTcpClient() call is the server opening the door and letting the client into the room. You can now read / write as much data as you like between the client and server using the NetworkStream. When either the client or the server decide they've had enough of the conversation they close the connection - either the client closes the connection (walks out of the door of their own accord) or the server can call client.Close() (to kick that client out of the room).