0

我在这里有两个代码,用于带有广播机制的 UDP 聊天,服务器可以同时向所有客户端广播消息

服务器代码...

    #include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc, char *argv[] )
{
    struct sockaddr_in client,server;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=htons(2000);
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    bind(s,(struct sockaddr *)&server,sizeof(server));
    printf("\nServer ready,waiting for client....\n");
    n=sizeof(client);
    int sock;                         /* Socket */
    struct sockaddr_in broadcastAddr; /* Broadcast address */
    char *broadcastIP;                /* IP broadcast address */
    unsigned short broadcastPort;     /* Server port */
    char *sendString;                 /* String to broadcast */
    int broadcastPermission;          /* Socket opt to set permission to broadcast */
    unsigned int sendStringLen;       /* Length of string to broadcast */

    if (argc < 4)                     /* Test for correct number of parameters */
    {
        fprintf(stderr,"Usage:  %s <IP Address> <Port> <Send String>\n", argv[0]);
        exit(1);
    }

    broadcastIP = argv[1];            /* First arg:  broadcast IP address */ 
    broadcastPort = atoi(argv[2]);    /* Second arg:  broadcast port */
    sendString = argv[3];             /* Third arg:  string to broadcast */

    /* Create socket for sending/receiving datagrams */
    sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
     broadcastPermission = 1;
    while(1)
    {
        recvfrom(s,b1,sizeof(b1),0,(struct sockaddr *) &client,&n);
        if(!(strcmp(b1,"end")))
            break;
        printf("\nClient:%s",b1);
        printf("\nServer:");
        gets(b2);
        sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &client,n);
     broadcastPermission = 1;
    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission));
           /* Construct local address structure */
    //memset(&broadcastAddr, 0, sizeof(broadcastAddr));   /* Zero out structure */
    broadcastAddr.sin_family = AF_INET;                 /* Internet address family */
    broadcastAddr.sin_addr.s_addr = inet_addr(broadcastIP);/* Broadcast IP address */
    broadcastAddr.sin_port = htons(broadcastPort);         /* Broadcast port */
    sendStringLen = strlen(sendString);
    for (;;) /* Run forever */
        {
         /* Broadcast sendString in datagram to clients every 3 seconds*/
         sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));
         //    DieWithError("sendto() sent a different number of bytes than expected");

        //sleep(3);   /* Avoids flooding the network */
     }




    }

}

客户端代码...

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#define MAXRECVSTRING 255
int main(int argc , char argv[])
{
    struct sockaddr_in client,server;
    int s,n;
    char b1[100],b2[100];
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=htons(2000);
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    printf("\nClient ready....\n");
    n=sizeof(server);
    int sock;                         /* Socket */
    struct sockaddr_in broadcastAddr; /* Broadcast Address */
    unsigned short broadcastPort;     /* Port */
    char recvString[MAXRECVSTRING+1]; /* Buffer for received string */
    int recvStringLen;                /* Length of received string */

    if (argc != 2)    /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Broadcast Port>\n", argv[0]);
        exit(1);
    }
     printf("\nClient ready…11111.\n");
    broadcastPort = htons(argv[1]);   /* First arg: broadcast port */
     printf("\nClient ready….1\n");
    /* Create a best-effort datagram socket using UDP */
    if(sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)<0)
    printf("no sock created");  
     printf("\nClient ready….2\n");
    while(1)
    {
        printf("\nClient:");
        gets(b2);
        sendto(s,b2,sizeof(b2),0,(struct sockaddr *) &server,n);
        if(strcmp(b2,"end")==0)
            break;
        recvfrom(s,b1,sizeof(b1),0,NULL,NULL);
        printf("\nServer:%s",b1);
    broadcastAddr.sin_family = AF_INET;                 /* Internet address family */
    broadcastAddr.sin_addr.s_addr = htonl(INADDR_ANY);  /* Any incoming interface */
    broadcastAddr.sin_port = htons(broadcastPort);      /* Broadcast port */

    /* Bind to the broadcast port */
    bind(sock, (struct sockaddr *) &broadcastAddr, sizeof(broadcastAddr));


    /* Receive a single datagram from the server */
    recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0);

    recvString[recvStringLen] = '\0';
    printf("Received: %s\n", recvString);    /* Print the received string */


    }
    close(sock);
}

这些代码运行没有任何错误,但是当我从客户端向服务器发送一个字符串时,服务器无法接收该字符串,反之亦然。

如果有人可以帮助我了解为什么会这样。

我在不同终端窗口中的同一系统上同时运行服务器和客户端..因此本地主机地址

4

3 回答 3

1

Some platforms, e.g. Windows, won't receive broadcasts unless the socket is bound to INADDR_ANY, i.e. 0.0.0.0.

于 2012-09-17T02:54:25.160 回答
0

首先:

server.sin_port=2000;

你忘记了这个网络顺序。

于 2012-09-16T17:39:01.660 回答
0

在您的服务器代码中:套接字未绑定到任何 IP 地址。虽然它广播消息并且客户端接收;反之亦然。因此,将您的服务器和客户端套接字绑定到 2 个不同的 IP 地址。当服务器广播消息时,在 sendto 函数中使用适当的广播地址(参见服务器-客户端 IP 的子网)。

于 2020-03-05T07:57:34.007 回答