简体   繁体   English

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

[英]request/reply server using select(). Can't write back to client

I got all of this code from the beej guide so all of the accepting can be seen from there. 我从beej指南中获得了所有这些代码,因此可以从那里看到所有接受内容。 In the Beej's code, he gets a message from a client, and then sends the message to all the other clients. 在Beej的代码中,他从客户端获取一条消息,然后将该消息发送给所有其他客户端。 This can be seen from this snippet here: 这可以从以下代码段中看到:

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

Instead of sending the same message to all the clients, i would like to adapt it into a request/reply feature and send a reply to the same client I received data from. 我不想将相同的消息发送给所有客户端,而是希望将其改编为请求/回复功能,然后向接收数据的同一客户端发送回复。

here is what I have so far: 这是我到目前为止所拥有的:

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

I receive all data from the client at the server. 我从服务器上的客户端接收所有数据。 I then am not sure if the problem is writing the data back on the server, or reading from the client. 然后,我不确定问题是否出在服务器上,或者从客户端读取数据。 I assume it is writing to the client from the server. 我假设它正在从服务器写入客户端。 As I hinted in the comments, I think I know where I went wrong, but I have removed this if statement, and I still wasn't able to read anything on the client side. 正如我在评论中所暗示的那样,我想我知道哪里出了问题,但是我删除了此if语句,但是我仍然无法在客户端读取任何内容。 Do I need to set a writable flag at the very beginning? 我是否需要在开始时设置可写标志? Please let me know if you need anything else. 如果您还有其他需要,请告诉我。 Sorry this post was so long. 抱歉,这篇文章太长了。

Just do the write. 只是写。 If it returns -1/EWOULDBLOCK, or a value indicating that it didn't write the full response, then you add the FD to the writefds, and continue the write when the FD becomes writable. 如果它返回-1 / 1 / EWOULDBLOCK,或者指示它未写入完整响应的值, 则将 FD添加到writefds中,并在FD变为可写状态时继续写入。 You normally don't have any writefds, as FDs are normally writable, that is to say they normally have space in their socket send buffers. 您通常没有任何writefds,因为FD通常是可写的,也就是说它们通常在其套接字发送缓冲区中有空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM