【问题标题】:Client console to display received messages and accept messages客户端控制台显示收到的消息并接受消息
【发布时间】:2013-02-21 09:11:31
【问题描述】:

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

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

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

}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      1.客户端连接到服务器client1client2client3后不要关闭连接,等待read()

      2.在服务器端你可能想看看pselect&amp;timeout

      3.建立连接后将 session_fd 存入数组clientId[3]

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

      逻辑--

      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
        }
       }
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 2020-07-10
      • 2017-02-13
      • 2010-12-03
      • 2018-08-06
      • 1970-01-01
      • 2020-10-09
      • 2012-11-02
      相关资源
      最近更新 更多