Say i have a server that listens to a TCP Port (through TcpListener), and i have five different connections to that server (through TcpClient). How would i go about sending information through streams to each one without giving the wrong information to the wrong client.
For Example:
Five clients all connected to the server.
I have to send the message "this is a message" to Client 1, but i dont want to send that message to client 2, 3, 4 and 5. I also want to send the message "this is another message" to client 3 and 5 but not send that message to client 1, 2 and 4.
How exactly do i go about doing this?
Asked
Active
Viewed 253 times
1

Mike Pennington
- 41,899
- 19
- 136
- 174

phxvyper
- 364
- 1
- 4
- 13
2 Answers
2
The TcpListener
class has the methods AcceptSocket
and AcceptTcpClient
. They return a different Socket
(or TcpClient
) for every client that connects to your server – you use these objects to send data to the individual clients.

millimoose
- 39,073
- 9
- 82
- 134
-
@NikkuAisuru I'm sure the documentation goes over that. – millimoose Oct 28 '12 at 22:17
-
So, if i use... TcpListener TCListen = new TcpListener("localhost", 90); TcpClient TClient = TCListen.AcceptTcpClient(); StreamWriter serverwriter = new StreamWriter(TClient.GetString()) then... serverwriter.Write("Message"); ... It'll only send "Message" to a single person? – phxvyper Nov 01 '12 at 00:51
2
After you accept Socket or call AcceptTcpClient, you can differentiate them using endpoint. If you want to know what to sent to which endpoint, you should differentiate them using their IP and/or port from which the connection is coming. You can refer to this post to see how to do that getting-the-ip-address-of-a-remote-socket-endpoint
EDIT: You can see how to send/receive data for example here

Community
- 1
- 1

Nikola Davidovic
- 8,556
- 1
- 27
- 33