how to send message to all client connects to server?
Printable View
how to send message to all client connects to server?
You can use the DOS command - "NET Send * This is the message"
or did you need something more elaborate?
i'm using winsock.....so every time server send message to all client, it only need to send one time .......can my idea work? simlar to socket broadcast function
In what way do you ue Winsock?
If it's TCP you have to send each client a message, since you need to have one WinSock- TCP connection for each client.
Using the UDP Protokol you can have all clients listening and the server has to send only once. But each Client has to be listening!
I do this in one of my apps. More or less it goes like this: When a client connects, I add the Socket to a Hashtable. To send a message to all clients, loop through the Hashtable, pull out the Socket, and send your message.
Mike
put socket in hashtable?
Yes. You could use any sort of collection that can accept an object.Quote:
Originally Posted by kenny_oh
I use a Hashtable because my message definition contains a user ID. So I store the user ID as the key, and the Socket as the value. If you don't need to store the user ID or can't retrieve it from messages, you probably don't need a Hashtable - an array of objects might work too.
The idea is that you have a collection of all the Sockets that belong to the clients, then you can just iterate through the Socket collection and send a message.
Here's a code snippet from a test I did a couple years ago - it has flaws like no error handling, it's using the synchronous .Send method, and the for...each could be improved, but hopefully it gives you the idea.
MikeVB Code:
Private Sub SendMessageToClients(ByVal message As String) Try Dim myDE As DictionaryEntry Dim s As Socket For Each myDE In serverSockets ' serverSockets is a Hashtable s = myDE.Value s.Send(Encoding.ASCII.GetBytes(message)) Next End Sub