简体   繁体   中英

C++ - client-server application - recv() on server is functional, but recv() on client blocks program

I'm writing client-server application. Until now everything was OK, client sent a request, server recieved it, parse it. But now I want to send back an answer, so I copied those two functions, I put write() from client to server and read() from server to client.
And when I run the program now everything blocks, server waits, client waits too. When I ctrl+c client, server unblocks and parse the right request and waits for another. What could be wrong, please?

Part of code from client:

  params.port = atoi(params.pvalue.c_str());  
  hostent *host;              
  sockaddr_in socketHelper;    
  int clientSocket;           
  char buf[BUFFER_LEN];     
  int size;                   
  string data;                
  string recieved;

  // gets info about server
  host = gethostbyname(params.hvalue.c_str());
  if(host == NULL) {
    printErr(ERR_HOSTNAME);
    return ERR_HOSTNAME;
  }

  // makes a socket
  if((clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    printErr(ERR_SOCKET);
    return ERR_SOCKET;
  }

  socketHelper.sin_family = AF_INET;
  socketHelper.sin_port = htons(params.port);
  memcpy(&(socketHelper.sin_addr), host->h_addr, host->h_length);

  // connects the socket
  if(connect(clientSocket, (sockaddr *)&socketHelper, sizeof(socketHelper)) == -1) {
    printErr(ERR_CONNECTION);
    return ERR_CONNECTION;
  }

  // sends data
  if((size = write(clientSocket, request.c_str(), request.length())) == -1) {
    printErr(ERR_SEND);
    return ERR_SEND;
  } 

  // recieves data
  while ((size = read(clientSocket, buf, BUFFER_LEN)) != 0) {
    recieved.erase();
    recieved.append(buf, size);

    data = data + recieved;
  }

  // closes a connection
  close(clientSocket);

And part of code from server:

while(1) {
    int clientSocket = accept(GodParticle, (struct sockaddr*) &GodAddr, &clientSocketSize);
    if(clientSocket == -1) {
      printErr(ERR_ACCEPT);
      return ERR_ACCEPT;
    }

    if((pid = fork()) == 0) {
      while ((size = read(clientSocket, buf, BUFFER_LEN)) != 0) {
        recieved.erase();
        recieved.append(buf, size);

        request = request + recieved;
      }

      parserInput(request);
      getData();
      parserOutput();

      if((size = write(clientSocket, sendback.c_str(), sendback.length())) == -1) {
        printErr(ERR_SEND);
        return ERR_SEND;
      } 

      close(clientSocket);
      exit(ERR_OK);
    }
  }

Ok, let me answer in this way. recv() is blocking your program by default until it receive some message from server.

And the reason why recv() does not blocks your server program is because you used fork() to create a child process.

So you have to use some other method to avoid this block(maybe like using select or some other things).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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