2

クライアント サーバー UDP アプリケーションを開発しました。サーバー UDP ソケットは、BROADCAST UDP ソケットに設定されています。双方のコードはエラーを発生させませんが、BROADCAST UDP SERVER 側から送信されたメッセージはクライアント側で受信されません。親切に私のコードを見てください。私が理解できないいくつかの間違いがあることを知っています。私は本当に助けが必要です:

サーバ:

#define PORT 8888 //The port on which to listen for incoming data

int main()
{

SOCKET s;
struct sockaddr_in serverSocket, clientSocket;

char receiveBuffer[1000];
//int receiveBufferLength=1000;
int clientSocketLength;
int recv_len;

clientSocketLength = sizeof(clientSocket) ;

WSADATA wsa; 
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
  printf("Failed. Error Code : %d",WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("Socket Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
    printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");   

//Prepare the sockaddr_in structure
serverSocket.sin_family = AF_INET;
serverSocket.sin_addr.s_addr = INADDR_ANY;
serverSocket.sin_port = htons( PORT );

int broadcast =1;


if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*) &broadcast, sizeof(broadcast)) < 0) {
    //close(sock);
    printf("Error in setting broadcast option");
}   
//Bind
if( bind(s ,(struct sockaddr *)&serverSocket , sizeof(serverSocket)) == SOCKET_ERROR)
{
    printf("\nBind failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("Bind done\n\n");

//keep listening for data


    printf("\n\t\t\tWaiting for data...\n");
    fflush(stdout);
    //receiveBuffer[2000]=NULL;

    if((recv_len = recvfrom(s, receiveBuffer, 1000, 0, (struct sockaddr *) &clientSocket, &clientSocketLength)) == SOCKET_ERROR)
    {
        printf("\n\nrecvfrom() failed with error code : %d" , WSAGetLastError());
        //exit(EXIT_FAILURE);
        while(1);
    }

    //print details of the client/peer and the data received
    printf("\n\nReceived packet from %s:%d\n", inet_ntoa(clientSocket.sin_addr), ntohs(clientSocket.sin_port));
    printf("\nClient Says: " );
        printf(receiveBuffer,recv_len);

    serverSocket.sin_addr.s_addr = INADDR_BROADCAST;
    //now reply the client with the same data
    if (sendto(s, receiveBuffer, recv_len, 0, (struct sockaddr*) &serverSocket, sizeof(serverSocket)) == SOCKET_ERROR)
    {
        printf("\nsendto() failed with error code : %d" , WSAGetLastError());
       // exit(EXIT_FAILURE);
        while(1);
    }
    else
        printf("\nMessage Sent Back to Client");
    while(1);



closesocket(s);
WSACleanup();
return 0;

}

クライアント:

#define PORT 8888   //The port on which to listen for incoming data
#define SERVER "127.0.0.1"  //ip address of udp server
//#define PORT 8888   //The port on which to listen for incoming data

int main(void)
{
struct sockaddr_in connectedSocket;

int s;
int length=sizeof(connectedSocket);

char receiveBuffer[1000];
char message[1000];

//clear the buffer by filling null, it might have previously received data
memset(receiveBuffer,'\0', 1000);

WSADATA wsa;
//Initialise winsock
printf("\nInitialising Winsock...\n");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
    printf("\nFailed. Error Code : %d",WSAGetLastError());
    exit(EXIT_FAILURE);
}
printf("\n.........Initialised.\n");


//create socket
if ( (s=socket(AF_INET, SOCK_DGRAM, 0)) == SOCKET_ERROR)
{
    printf("\n\nsocket() failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}


//setup address structure
memset((char *) &connectedSocket, 0, sizeof(connectedSocket));
connectedSocket.sin_family = AF_INET;
connectedSocket.sin_port = htons(PORT);
 //connectedSocket.sin_port = INADDR_BROADCAST;
connectedSocket.sin_addr.S_un.S_addr = inet_addr(SERVER);

printf("\n\n\nEnter message : ");
    gets(message);

  //send the message
    if (sendto(s, message,sizeof(message) , 0 , (struct sockaddr *) &connectedSocket, sizeof(connectedSocket)) == SOCKET_ERROR)
    {
        printf("\nsendto() failed with error code : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }

        printf("\nMessage Successfully sent to Server");
      // fflush(stdout);

    if (recvfrom(s, receiveBuffer, 1000, 0, (struct sockaddr *) &connectedSocket,&length) == SOCKET_ERROR)
    {
       printf("\nrecvfrom() failed with error code : %d" , WSAGetLastError());
       exit(EXIT_FAILURE);
       while(1);
    }

    printf("\nServer Says : ");
    printf(receiveBuffer,sizeof(receiveBuffer));

  while(1);

closesocket(s);
WSACleanup();

return 0;

}

4

1 に答える 1

3

でクライアントに返信していますserverSocket。メッセージを受信した を既に持っているので、clientsocketそれを使用してサーバーからクライアントにメッセージを送り返します。

削除する ...

serverSocket.sin_addr.s_addr = INADDR_BROADCAST;

そして変更...

if (sendto(s, receiveBuffer, recv_len, 0, (struct sockaddr*) &serverSocket, sizeof(serverSocket)) == SOCKET_ERROR)

if (sendto(s, receiveBuffer, recv_len, 0, (struct sockaddr*) &clientSocket, sizeof(clientSocket)) == SOCKET_ERROR)

今すぐ動作するはずです。

于 2013-03-11T10:41:26.390 に答える