MAO-EYE 2018-11-11 16:05 采纳率: 100%
浏览 264

服务器只向第一个客户机广播

I'm trying to broadcast a message from a server to all clients, but only one client receives the message.

I want to run this server and two or more instances of this client (taken from Donahoo, Calvert, "TCP/IP Sockets in C", 1e; I can paste the code into this question on request).

The programs work fine with a single client, but when running two clients only one (the first) ever receives a message, while the second instance just gets stuck (on bind).

I don't know what I'm doing wrong, I'm sure the program is correct so perhaps I'm running it wrong. I start the server as:

$ ./BroadcastSender localhost 1337 hey &

As for the clients, I have tried two variations, the first:

$ ./BroadcastReceiver 1337 & ./BroadcastReceiver 1337 &

In the second variation I have added while (1) {} after close(sock) and then run as:

$ ./BroadcastReceiver 1337 &
$ ./BroadcastReceiver 1337 &

Both variations give the same result, namely that the first client receives the message, the other one doesn't, but instead gets stuck trying to bind.

Am I running the server/clients the wrong way, or is there something missing in the code? I'm new to sockets so I don't really see if there's anything in, say, the server code saying "I'm gonna broadcast to one client only".

Could you give me some pointers in the right direction? There are other questions and answers about broadcasting but I haven't found one that addresses this particular issue. Thank you.

转载于:https://stackoverflow.com/questions/53250574/server-broadcasts-only-to-first-client

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 matlab有关常微分方程的问题求解决
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法