0

私はサーバーマルチクライアントプログラムを持っています。サーバーはいつでもクライアントにメッセージを送信する必要があります (また送信できる) ため、クライアントは、サーバーが送信するすべてのメッセージを受信 (およびコンソールに表示) する準備が常に整っている必要があります。

同時に、クライアント コンソールは、処理のためにサーバーに送信される入力を受け入れる必要があります (サーバーはクライアント間の仲介者として機能します)。

これどうやってするの?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;

}

4

2 に答える 2

0

スレッド プログラミングに精通している場合は、別のスレッドを割り当てて受信トラフィックを処理し、別のスレッドを送信トラフィックを処理することをお勧めします。

もう 1 つのオプションは、非同期ソケットを使用することです。

于 2013-02-21T09:34:16.187 に答える