开发者

Send same packets to multiple clients

I have to develop a software to send same packets to multiple destination. But i must not use multicast scheme.!!!! ( because my boss is a stupid man ) so, any way, the problem is that: i have same packets and multiple IP address ( clients) and i can not use multicast how can i do that in the best开发者_高级运维 way? i must use c++ as a language and Linux as a platform. so please help me

Thanx


If your boss said you can't use multicast, maybe he/she has his/her reason. I guess broadcasting is out of the game too?

If these are the requisites, your only chance is to establish a TCP connection with every remote host you want to send packet to.

EDIT
UDP, conversely, would not provide much benefit over multicasting if your application will run over a LAN you are in charge for configuration of, that's the reason I specified TCP.

Maybe you have to describe your scenario a little better.


This could be done with either TCP or UDP depending on your reliability requirements. Can you tolerate lost or reordered packets? Are you prepared to handle timeouts and retransmission? If both answers are "yes", pick UDP. Otherwise stay with TCP. Then:

  • TCP case. Instead of single multicast UDP socket you would have a number of TCP sockets, one per destination. You will have to figure out the best scheme for connection establishment. Regular listening and accepting connecting clients works as usual. Then you just iterate over connected sockets and send your data to each one.
  • UDP case. This could be done with single UDP socket on the server side. If you know the IPs and ports of the clients (data receivers) use sendto(2) on the same data for each address/port. The clients would have to be recv(2)-ing at that time. If you don't know your clients upfront you'd need to devise a scheme for clients to request the data, or just register with the server. That's where recvfrom(2) is usefull - it gives you the address of the client.


You have restricted yourself by saying no to multicast. I guess sending packets to multiple clients is just a part of your requirement and unless you throw more light, it will be difficult to provide a complete solution.

Are you expecting two way communication between the client and the server ? in that case choosing multicast may prove complex. please clarify

You have to iterate through the clients and send packets one after another. You may want to persist the sessions if you are expecting response from the clients back.

Choice of UDP or TCP again depends on the nature of data being sent. with UDP you would need to handle out of sequence packets and also need to implement re-transmission.


You'll have to create a TCP Listerner on your server running at a particular port listening for incoming Tcp Client connections (Sockets).

Every time a client connects, you'll have to cache it in some kind of datastructre like a Name value pair (name being a unique name for the client amd value being the Network Stream of that client obtained as a result of the TCP socket).

Then when you are finally ready to transmit the data you could either iterate through this collection of name value pair connections and send them data as byte array one by one to each client or spawm off one thread per connected client and have it send the data concurrently.

TCP is a bulky protocol (due to its connection-oriented nature) and transmission of large data (like videos/images) can be quite slow.

UDP is definitely the choice for streaming large data packets but you'll have to trade-off with the delivery gurantee.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜