我有一個服務器多客戶端程序。服務器需要(也可以)隨時向客戶端發送消息,因此客戶端需要不斷準備好接收(並在控制檯上顯示)服務器發送的所有消息。客戶端控制檯顯示收到的消息並接受消息
與此同時,客戶端控制檯需要接受要發送到服務器進行處理的輸入(服務器充當客戶端之間的中介)。
我該怎麼做?我正在處理從http://www.codeproject.com/Articles/7785/Single-Server-With-Multiple-Clients-a-Simple-C-Imp取得的一段代碼,客戶端代碼(我將在下面附上)通過接受來自服務器的消息,在將消息發送到服務器之前準備好接收消息。我如何適應這個做我所需要的?
我只包括客戶端代碼的主體,因爲我認爲它可能只是使用循環的問題 - 儘管我無法確定什麼循環 - 所以請讓我知道是否應該附加任何其他代碼。
#include "stdafx.h"
#include "mySocket.h"
#include "myLog.h"
#include "myException.h"
#include "myHostInfo.h"
myLog winLog;
void readServerConfig(string&);
void checkFileExistence(const string&);
int main()
{
//initialize the winsock library
myTcpSocket::initialize();
//get client's information (assume neither the name nor the address is given)
winLog << endl;
winLog << "retrieve the localHost [CLIENT] name and address:" << endl;
myHostInfo clientInfo;
string clientName = clientInfo.getHostName();
string clientIPAddress = clientInfo.getHostIPAddress();
cout << "name: " << clientName << endl;
cout << "address: " << clientIPAddress << endl;
winLog << " ==> name: " << clientName << endl;
winLog << " ==> address: " << clientIPAddress << endl;
//get server's IP address and name
string serverIPAddress = "";
readServerConfig(serverIPAddress);
winLog << endl;
winLog << "retrieve the remoteHost [SERVER] name and address:" << endl;
winLog << " ==> the given address is " << serverIPAddress << endl;
myHostInfo serverInfo(serverIPAddress,ADDRESS);
string serverName = serverInfo.getHostName();
cout << "name: " << serverName << endl;
cout << "address: " << serverIPAddress << endl;
winLog << " ==> name: " << serverName << endl;
winLog << " ==> address: " << serverIPAddress << endl;
//create the socket for client
myTcpSocket myClient(PORTNUM);
cout << myClient;
winLog << "client configuation: " << endl;
winLog << myClient;
// connect to the server.
cout << "connecting to the server [" << serverName << "] ... " << endl;
winLog << "connecting to the server [" << serverName << "] ... " << endl;
myClient.connectToServer(serverIPAddress, ADDRESS);
int recvBytes = 0;
while (1)
{
// send message to server
char messageToServer[MAX_MSG_LEN+1];
memset(messageToServer, 0, sizeof(messageToServer));
cout << "[SEND] ";
cin.getline(messageToServer,MAX_MSG_LEN);
winLog << "[SEND] " << messageToServer << endl;
myClient.sendMessage(string(messageToServer));
if (!string(messageToServer).compare("Quit") || !string(messageToServer).compare("quit"))
break;
// receive message from server
/* string messageFromServer = "";
recvBytes = myClient.receiveMessage(messageFromServer);
if (recvBytes == -99) break;
cout << "[RECV:" << serverName << "]: " << messageFromServer << endl;
winLog << "[RECV:" << serverName << "]: " << messageFromServer << endl;*/
}
return 1;
}
代碼片段通過@Jeremy Friesner: :http://stackoverflow.com/questions/8674491/server-with-multiple-clients-writing-with-select – 2014-01-23 08:38:31