在同一 LAN 上多播 C++ 多台计算机
Multicasting C++ multiple computers on the same LAN
我正在为大学做作业,我被要求为我大学的 LAN 实现聊天。
我正在使用多播向组中的所有注册用户发送相同的消息。我的发件人是在 C++ 上开发的,而接收者是在 java 上开发的。在同一台计算机上测试时,我附加的代码工作正常,发送方发送,接收方接收,但是当 运行在另一台计算机上设置客户端时,它不会收到发送的消息。
服务器:
int main(){
/** MC socket **/
struct sockaddr_in groupSock;
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("225.5.4.30");
groupSock.sin_port = htons(54321);
bzero(&(groupSock.sin_zero),8);
int mcsock;
if ((mcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("Socket MC");
exit(1);
}
int nroM = 0;
while(1)
{
fflush(stdout);
stringstream resp;
resp << "Mensaje multicast: " << nroM << "\n";
cout << resp.str();
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
/*int datalen = 1024;*/
if(sendto(mcsock, resp.str().c_str(), strlen(resp.str().c_str()), 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
perror("Sending datagram message error");
nroM++;
sleep(2);
}
close(mcsock);
return 0;
}
客户:
class UDPCliente {
public static void main(String args[]) throws Exception{
InetAddress address = InetAddress.getByName("225.5.4.30");
// Create a buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server.
// Since the message is small here, 256 bytes should be enough.
byte[] buf = new byte[256];
// Create a new Multicast socket (that will allow other sockets/programs
// to join it as well.
try (MulticastSocket clientSocket = new MulticastSocket(54321)){
//Joint the Multicast group.
clientSocket.joinGroup(address);
while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.print(msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
只是为了提供额外信息,此代码具有适当的导入并包括编译和 运行。
谢谢!!
好吧,显然我换了另一台电脑并且代码有效。所以防火墙有事可做。我正在回答,所以如果有人需要同样的问题,如果包含并导入正确的库,我附加的代码将完美运行!
我正在为大学做作业,我被要求为我大学的 LAN 实现聊天。 我正在使用多播向组中的所有注册用户发送相同的消息。我的发件人是在 C++ 上开发的,而接收者是在 java 上开发的。在同一台计算机上测试时,我附加的代码工作正常,发送方发送,接收方接收,但是当 运行在另一台计算机上设置客户端时,它不会收到发送的消息。
服务器:
int main(){
/** MC socket **/
struct sockaddr_in groupSock;
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("225.5.4.30");
groupSock.sin_port = htons(54321);
bzero(&(groupSock.sin_zero),8);
int mcsock;
if ((mcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
perror("Socket MC");
exit(1);
}
int nroM = 0;
while(1)
{
fflush(stdout);
stringstream resp;
resp << "Mensaje multicast: " << nroM << "\n";
cout << resp.str();
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
/*int datalen = 1024;*/
if(sendto(mcsock, resp.str().c_str(), strlen(resp.str().c_str()), 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
perror("Sending datagram message error");
nroM++;
sleep(2);
}
close(mcsock);
return 0;
}
客户:
class UDPCliente {
public static void main(String args[]) throws Exception{
InetAddress address = InetAddress.getByName("225.5.4.30");
// Create a buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server.
// Since the message is small here, 256 bytes should be enough.
byte[] buf = new byte[256];
// Create a new Multicast socket (that will allow other sockets/programs
// to join it as well.
try (MulticastSocket clientSocket = new MulticastSocket(54321)){
//Joint the Multicast group.
clientSocket.joinGroup(address);
while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.print(msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
只是为了提供额外信息,此代码具有适当的导入并包括编译和 运行。
谢谢!!
好吧,显然我换了另一台电脑并且代码有效。所以防火墙有事可做。我正在回答,所以如果有人需要同样的问题,如果包含并导入正确的库,我附加的代码将完美运行!