0

我创建了单个服务器和单个客户端回显应用程序。它适用于单个服务器和单个客户端。但现在我想通过为单个服务器处理多个客户端来使其更实用。我突然想到在服务器端使用listen() 函数来处理多个客户端连接,但后来我知道listen 仅用于TCP。请帮我解决这个问题。下面是我的单服务器和单客户端的功能代码。帮助我如何修改它以制作单服务器多客户端应用程序。

服务器:

#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 );

//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

while(1)
{
    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);



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

}

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

}

客户:

#define PORT 8888   //The port on which to listen for incoming data
#define SERVER "10.0.1.25"  //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);

while(1)
{
    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);
    }

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

}

closesocket(s);
WSACleanup();

return 0;

}
4

1 回答 1

2

让我为您解释一些可能令人困惑的事情。

首先,在您的服务器中,您需要设置serverSocket.sin_addr.s_addrINADDR_ANY即使您想广播。你不想成功INADDR_BROADCAST。这只是告诉服务器绑定您计算机拥有的任何IP 地址,因为一台计算机可以拥有多个 IP。你做对了。

但是你怎么能从服务器广播呢?你需要使用这样setsockoptSO_BROADCAST参数...

int options = 1;
if ((setsockopt(s, SOL_SOCKET, SO_BROADCAST,(char *)&options,sizeof(options))) < 0){
    printf("%d",WSAGetLastError());
}

这将使您的服务器监听广播,但是您如何发送广播呢?您需要在调用函数之前在sockaddrINADDR_BROADCAST结构中的s_addr字段中进行设置。sendto

clientSocket.sin_addr.s_addr = INADDR_BROADCAST;

然后使用sendtowithclientSocket将您的消息播回。

其次,在您的客户端代码中,您有两个选项,具体取决于您的应用程序。

  1. 如果您希望您的客户端首先将消息发送到服务器(直接而不广播),那么您做对了,通过设置inet_addr(SERVER)客户端仅将消息发送到您的服务器。

  2. 但是,如果您希望客户端广播消息,那么您需要setsockopt在您的客户端中再次使用,与您的服务器相同,并将connectSocket结构中的地址设置为INADDR_BROADCAST.

最后,如果你告诉我们你想在这里完成什么会更好。如果您希望客户端首先连接到服务器,然后服务器将客户端通知给其他客户端,那么您可能会采用 TCP 而不是 UDP。

于 2013-03-12T09:18:38.220 回答