[英]Receiving multiple client packets on one server
我有3個應用程序,一台服務器,2個客戶端。 我正在嘗試對此編程,以便可以將登錄請求從客戶端發送到服務器,並請求查看有關另一個客戶端的信息。
問題:我為所有3個應用程序使用相同的ip和端口號(全部在計算機上的3個Visual Studio應用程序中完成),否則我認為它不會連接到服務器。 這個可以嗎?
當我嘗試在服務器的while循環之外使用listen和accept時,它僅對一個客戶端有效,而對另一個客戶端無效。 當我嘗試將監聽和接受放入while循環中時,它對兩個客戶端均有效,但不會響應多個接受。 我怎么了
服務器
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()
。 您也不要使用nonblocking socket
,但這是您的偏愛。 另一件事,每次完成accept
都必須使用closesocket
。 同樣,您的recv()
必須循環處理,因為與send不同,它可能在接收所有數據之前返回。
服務器的基本結構:
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();
瓦爾特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.