2012-09-16 136 views
0

我有兩個代碼這裏與廣播機構的UDP聊天,其中服務器可以將消息廣播到所有客戶端同時採用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); 
} 

沒有任何錯誤,這些代碼的運行,但是當我從客戶端發送一個字符串到服務器的服務器無法接收STRI ng,反之亦然。

如果有人能幫我解釋爲什麼會發生這種情況。

我運行兩個不同終端在同一系統windows..hence本地主機地址在服務器和客戶端

+0

什麼是服務器和客戶端的網絡配置(IP地址和網絡掩碼)? –

回答

0

首先:

server.sin_port=2000; 

你忘了,使這個網絡訂單。

+0

我將端口更改爲網絡順序,但客戶端和服務器仍然沒有收到其他消息! –

1

一些平臺,例如Windows將不會接收廣播,除非套接字綁定到INADDR_ANY,即0.0.0.0。