客户端控制台显示收到的消息并接受消息

时间:2013-02-21 09:11:31

标签: c++ loops client client-server client-side

我有一个服务器多客户端程序。服务器需要(并且可以)随时向客户端发送消息,因此客户端需要不断准备接收(并在其控制台上显示)服务器发送的所有消息。

同时,客户端控制台需要接受要发送到服务器进行处理的输入(服务器充当客户端之间的中介)。

我该怎么做?我正在使用从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;

}

2 个答案:

答案 0 :(得分:0)

如果您熟悉线程编程,我建议分配一个单独的线程来处理传入的流量,另一个线程来处理传出的流量。

另一种选择是使用异步套接字。

答案 1 :(得分:0)

1.客户端连接到服务器client1后,client2client3不会关闭连接并等待read()

2.在服务器端您可能想要查看pselect&timeout

3.建立连接后,将session_fd存储到数组clientId[3]

4.现在你可以写到write(client[0-2], , ...)

LOGIC -

int socks[10] = {... some client sockets...}

while(1)
{
   fd_set readSet, writeSet;
   FD_ZERO(&readSet);
   FD_ZERO(&writeSet);

   int maxSock = -1;
   for (int i=0; i<10; i++)
   {
     FD_SET(socks[i], &readSet);
     if (socks[i] > maxSock) maxSock = socks[i];

     if (IHaveDataToSendToThisSocket(i))  // implement this function as appropriate to your program
      {
        FD_SET(socks[i], &writeSet);
        if (socks[i] > maxSock) maxSock = socks[i];
      }
}

int ret = select(maxSock+1, &readSet, &writeSet, NULL, NULL);
if (ret < 0)
{
  perror("select() failed");
  break;
}

// Do I/O for sockets that are ready
for (int i=0; i<10; i++)
{
  if (FD_ISSET(socks[i], &readSet))
  {
     // there is data to read on this socket, so call recv() on it
  }

  if (FD_ISSET(socks[i], &writeSet))
  {
     // this socket has space available to write data to, so call send() on it
  }
 }
}