1
我有3個應用程序,一個服務器,2個客戶端。我試圖對此進行編程,以便我可以將來自客戶端的登錄請求發送到服務器,並請求查看有關其他客戶端的信息。在一臺服務器上接收多個客戶端數據包
問題:我對所有3個應用程序使用相同的ip和端口號(全部在我的計算機上的3個visual studio應用程序上完成),否則我不認爲它連接到服務器。這個可以嗎?
當我嘗試在服務器的while循環之外使用listen和accept時,它適用於一個客戶端,但不適用於其他客戶端。當我嘗試在while循環內部放置listen和accept時,它適用於兩個客戶端,但不響應多個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();
}
我需要while循環,因爲我一直在尋找其他的連接,他們將派出複式請求登錄/視圖的信息。 – user2981393
@ user2981393我更正了關於while循環的答案。我的錯。 –
我刪除了監聽,只有接受,然後關閉我的while循環結束時的套接字,但它不再接受新的數據包,就像我的客戶端將發送第一個之後的另一個數據包,並且服務器不接受它 – user2981393