c++ - 使用 select() 请求/回复服务器。无法写回客户端

标签 c++ sockets networking nonblocking

我从 the beej guide 得到了所有这些代码所以所有的接受都可以从那里看到。在 Beej 的代码中,他从一个客户端获取消息,然后将消息发送给所有其他客户端。从此处的代码片段可以看出这一点:

                // handle data from a client
                if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
                    // got error or connection closed by client
                    if (nbytes == 0) {
                    //handle error
                    }
                } 
                else {
                    // we got some data from a client
                    for(j = 0; j <= fdmax; j++) {
                        // send to everyone!
                        if (FD_ISSET(j, &master)) {
                            // except the listener and ourselves
                            if (j != listener && j != i) {
                                if (send(j, buf, nbytes, 0) == -1) {
                                    perror("send");
                                }
                            }
                        }
                    }
                }
            } // END handle data from client

我不想向所有客户端发送相同的消息,而是想将其调整为请求/回复功能,并向我从中接收数据的同一个客户端发送回复。

这是我目前所拥有的:

long length = 0;
string stringRead;
messagebroker broker;
read( i, &length, sizeof( length ) );
length = ntohl( length );
if(length > -1)
while ( 0 < length ) {
     char buffer[1024];
     int cread;
     cread = read( i, buffer, min( sizeof( buffer ), length ) );
     stringRead.append( buffer, cread );
     length -= cread;
 }
cout << "Got Message: " + stringRead << endl;
string response = broker.handleMessage(stringRead.c_str());

cout << "sending response" << response << endl;

//socket ready for writing
if (FD_ISSET(i, &master)) { //should i check to see if write_fds? I have this here
                                //simply because the guide has it, but i am suspicious
                                //it is there so we can not write to the master.
  length = htonl( response.length() );
  cout << "sent length" << endl;
  if(send( i, &length, sizeof(length) , 0) == 0){
     fprintf(stderr, "Error sending data %d\n", errno);
     exit(3);
  }
  if(send( i, response.data(), response.length(),0 )== 0){
          fprintf(stderr, "Error sending data %d\n", errno);
          exit(3);
      }
    }   //end if

我在服务器上接收来自客户端的所有数据。然后我不确定问题是将数据写回服务器,还是从客户端读取。我假设它正在从服务器写入客户端。正如我在评论中暗示的那样,我想我知道哪里出了问题,但我已经删除了这个 if 语句,但我仍然无法在客户端读取任何内容。我是否需要在一开始就设置一个可写标志?如果您还需要什么,请告诉我。抱歉这篇文章太长了。

最佳答案

只写。如果它返回 -1/EWOULDBLOCK,或者一个表明它没有写入完整响应的值,然后您将 FD 添加到 writefds,并在 FD 变得可写时继续写入。您通常没有任何 writefds,因为 FD 通常是可写的,也就是说它们通常在其套接字发送缓冲区中有空间。

关于c++ - 使用 select() 请求/回复服务器。无法写回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240964/

相关文章:

c++ - 如何将 const std::shared_ptr 插入 std::map

python - 带有 Python 3 的 Django 1.10 和 Socket.IO

java - 套接字上的 ClosedChannelException

java - 套接字编程,尝试循环遇到问题

c++ - 如何区分 QNetworkReply 是否中止?

linux - 如何拦截和修改某个进程发送和接收的tcp数据包?

c++ - 如何通过 decltype 声明迭代器的值

c++ - 大 O 符号帮助

c++ - 我的向量化 xorshift+ 不是很随机

networking - 请说明Google Analytics(分析)如何解释内部IP地址范围内的IP地址