1

I am using C# language to allow clients that are designed in WPF platform to send string via server to another clients.

But problem with my code is that I am broadcasting from my server to all clients same message while using TCP protocol. I know that TCP is famous with the fact that you can specify endpoint.But I don't know how.

This is what I do in server.

foreach (Socket _connectedUsers in clientSockets)
{
    byte[] data = Encoding.UTF32.GetBytes(Message);
    Socket socket = (Socket)_connectedUsers;
    socket.Send(data);
}

What this code does is broadcasting message to everyone from a list in clientSockets.

Now the question.

How can I do something like:

byte[] data = Encoding.UTF32.GetBytes(Message);
Socket socket = (Endpoint User IP Address);
socket.Send(data);
3
  • You can't broadcast using TCP. Commented Apr 24, 2020 at 19:04
  • You are not broadcasting, just sending the same message to all your client connections. So yes, you are already doing it - just for them all. So by using only one clientSocker, you only sending to one specific client. UDP can broadcast. Commented Apr 24, 2020 at 19:38
  • @FrankNielsen So, how can I send just to one and specific endpoint? Instead of sending to everyone? Commented Apr 26, 2020 at 12:03

1 Answer 1

1

You have to identity the client you want to send the message to, and do the same for only that client socket.

var _connectedUser = FindClientById( client.Id, clientSockets );
byte[] data = Encoding.UTF32.GetBytes(Message);
Socket socket = (Socket)_connectedUser;
socket.Send(data);

So you challenge is to identity the clientSocket that represent the client you want to send the message to.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this sounds logically correct. Have you any idea how can I do that?
That will be a different, and more architectural question. But i think you can similar questions on this site, on how to keep track client connections.
I was wondering, if you are aware how to convert IPEndPoint to Socket?
You can not convert it. The IPEndPoint is something the socket either connects to or is listening on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.