gpt4 book ai didi

c - 在一台服务器上接收多个客户端数据包

转载 作者:可可西里 更新时间:2023-11-01 02:52:03 25 4
gpt4 key购买 nike

我有 3 个应用程序、一个服务器、2 个客户端。我正在尝试对此进行编程,以便我可以将我的客户端的登录请求发送到服务器并请求查看有关另一个客户端的信息。

问题:我对所有 3 个应用程序使用相同的 ip 和端口号(这都是在我计算机上的 3 个 visual studio 应用程序上完成的)否则我认为它无法连接到服务器。这样可以吗?

当我尝试在服务器的 while 循环之外使用 listen 和 accept 时,它适用于一个客户端,但不适用于另一个客户端。当我尝试将 listen 和 accept 放在 while 循环中时,它对两个客户端都有效,但不会响应多个 accept。我的问题是什么?

服务器

struct Users {

int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;

};

int main () {

//Create users
Users client[2];

client[0].PortNumber = 20000;
client[0].online = false;
sprintf(&client[0].userName[0], "Albert");
sprintf(&client[0].ipAddress[0], "127.0.0.1");

client[1].PortNumber = 20000;
client[1].online = false;
sprintf(&client[1].userName[0], "Monique");
sprintf(&client[1].ipAddress[0], "127.0.0.1");


//Set up all the connection stuff
struct sockaddr_in SvrAddr;
SOCKET WelcomeSocket, ConnectionSocket;
int PortNumber = 20000;
int result;
char IPAddress[] = "127.0.0.1";
char RxBuffer[128];
char TxBuffer[128];


WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(2,2); //For Server


if(WSAStartup(wVersionRequested, &wsaData) != 0)
return -1;


//Setting up the welcome socket
WelcomeSocket = socket(AF_INET, SOCK_STREAM, 0);

//Setting up the sockaddr svraddr structure
SvrAddr.sin_family = AF_INET;
SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
SvrAddr.sin_port = htons(PortNumber);


//Bind
bind(WelcomeSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));

//listening
//listen(WelcomeSocket, 5);

//temp users
Users temp;
//ConnectionSocket = accept(WelcomeSocket, NULL, NULL);
ConnectionSocket = SOCKET_ERROR;

//while loop for the connection
while (1) {

listen(WelcomeSocket, 5);

if ((ConnectionSocket = accept(WelcomeSocket, NULL, NULL)) == SOCKET_ERROR)
{
return -1;
}

else {

int n = recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0);

if (n == 0)
break;


memcpy(&temp, RxBuffer, sizeof(struct Users));

//cout << temp.message << temp.userName << endl << endl;

//check which message type is being sent
switch(temp.message) {

//if message type 1
case 1 :
for (int i = 0; i < 2; i++) {

//if receieved username matches with any username in the database
if (strcmp(temp.userName, client[i].userName) == 0) {

//assign the recieved users information to the matched one in database
strcpy(client[i].userName, temp.userName);
client[i].online = true;
client[i].message = 2;

cout << "Username: " << client[i].userName << endl << "Online status: " << client[i].online << endl << endl;

//send the acknowledgement packet
send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
//closesocket(ConnectionSocket);
}

}
break;

//if message type 3
case 3 :

cout << "User being searched for: " << temp.userName << endl << endl;
for (int i = 0; i < 2; i++) {

//if receieved username matches with any username in the database
if (strcmp(temp.userName, client[i].userName) == 0) {
client[i].message = 4;
//send the acknowledgement packet
send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
//closesocket(ConnectionSocket);
}

}
break;

default :
break;

}

}

}

closesocket(ConnectionSocket);
WSACleanup();
}

客户端(两者相同)

struct Users {

int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;

};


int main () {

struct sockaddr_in SvrAddr;
SOCKET ClientSocket;
int PortNumber = 20000;
char IPAddress[] = "127.0.0.1";
//char message[] = "Hello this is the client.";
char RxBuffer[128];

WORD wVersionRequested;
WSADATA wsaData;

wVersionRequested = MAKEWORD(2,3); //For Client

if(WSAStartup(wVersionRequested, &wsaData) != 0)
return -1;


ClientSocket = socket(AF_INET, SOCK_STREAM, 0);

SvrAddr.sin_family = AF_INET;
SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
SvrAddr.sin_port = htons(PortNumber);

connect(ClientSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));



//cout << "Name: ";
//cin >> login;

//Send request to login
int log;
char * name = new char[128];
char * request = new char[128];
Users client;
Users talkto;


do {

cout << "To login press (1) to end press (2). ";
cin >> log;
flushall();

if (log == 1) {

cout << "Username : ";
cin.getline(name, 128, '\n');
cout << endl;
flushall();

//Set client login info
strcpy(client.userName, name);
client.message = 1;



send(ClientSocket, (char *)&client, sizeof(struct Users), 0);


//Recieve acknowledgement
recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
//create temp users
Users temp;

memcpy(&temp, RxBuffer, sizeof(struct Users));

//If logged in and received a message of type 2 (acknowledgement)
if (temp.message == 2) {

cout << "You have logged in." << endl << endl;

cout << "Enter user to request user information: ";
cin.getline(talkto.userName, 128, '\n');
flushall();
cout << endl;

talkto.message = 3;

//send request for user information packet
send(ClientSocket, (char *)&talkto, sizeof(struct Users), 0);

recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);

memcpy(&temp, RxBuffer, sizeof(struct Users));

//if message received is of type 4 (acknowledgement of user request info)
if (temp.message == 4) {

cout << "Requested User: " << temp.userName << endl << "Online status: " << temp.online << endl << endl;
}
}

//cout << temp.userName << endl << temp.online << endl << temp.message;
}

} while (log != 2);


closesocket(ClientSocket);


WSACleanup();



}

最佳答案

您不要在循环内使用 listen()。只有 accept()。此外,您不使用非阻塞套接字,但这是您的偏好。另一件事,每次完成 accept 时都必须使用 closesocket。此外,您的 recv() 必须在循环中处理,因为与发送不同,它可能会在接收到所有数据之前返回。

服务器的基本结构:

struct sockaddr_in inAddr; //incoming address. Use it to distinguise between incoming clients
struct sockaddr_in address;
unsigned int inLen;
int recvMsgSize;
unsigned short port = 20000;
Socket socket_handle, acceptSocket;

socket_handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket_handle == INVALID_SOCKET){
//error
}

memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY); //All incoming addresses
address.sin_port = htons(port); //port must be unsigned short not int!
if(bind(socket_handle, (struct sockaddr *)&address, sizeof(address)) < 0){
//error
}

if(listen(socket_handle, MAXPENDING) < 0){
//error
}

while(true){
acceptSocket = accept(socket_handle, (struct sockaddr *) &inAddr, &inLen));
if(acceptSocket == INVALID_SOCKET){
//error
}

while(true){
recvMsgSize = recv(acceptSocket, received_data, strlen(received_data), 0);
if(recvMsgSize < 0){/*error*/}
else if(recvMsgSize == 0){break;}
else{/*receive your data*/}
}

(do other stuff...)
closesocket(acceptSocket);
}

closesocket(socket_handle);
WSACleanup();

瓦尔特

关于c - 在一台服务器上接收多个客户端数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com