如何进行阻止recv调用

时间:2015-10-26 14:31:58

标签: c++ sockets tcp recv

这可能是一个奇怪的请求,但实际上,我想调用recv()阻止

它似乎应默认阻止 ,但对我来说,它不是,我找不到任何方法来制作它。

我有一个非常简单的服务器应用程序,“打开套接字 - 绑定 - 等待连接 - 接收”一方面,

,另一方面,一个简单的“ open socket - connect - send ”客户端应用程序。

他们在LocalHost上进行交流。

服务器:

int clTcpServer::openSocket(void)
{

  this->__iLocalSocketId = socket(AF_INET,      //stand for IPV4
                                  SOCK_STREAM,  //using TCP protocol
                                  0);           //no specified / ip protocol
  return 0;
}

int clTcpServer::openServer(uint16_t u16Port)
{
  this->__strServerInfo.sin_family = AF_INET; // stand for IPV4
  this->__strServerInfo.sin_addr.s_addr = INADDR_ANY; // Listen to any address (no filter)
  this->__strServerInfo.sin_port = htons(u16Port);

  if(bind(this->__iLocalSocketId, (sockaddr *)&this->__strServerInfo, sizeof(this->__strServerInfo)) < 0)
  {
    std::cout << "TCP server - ERROR - Can't bind socket to port " << std::dec << (int)u16Port << std::endl;
    return -1;
  }

  std::cout << "TCP server - Server opened" << std::endl;

  listen(this->__iLocalSocketId, 3);

  return 0;
}

int clTcpServer::waitForConnection(void)
{
  int iClientIpInfo;

  char* pcClientIp;

  std::cout << "Waiting for connection ..." << std::endl;

  iClientIpInfo = sizeof(struct sockaddr_in);
  this->__iDistantSocketId = accept(this->__iLocalSocketId,
                                    (sockaddr *)&this->__strClientInfo,
                                    (socklen_t*)&iClientIpInfo);

  if(this->__iDistantSocketId < 0)
  {
    std::cout << "TCP server - ERROR - Connection refused" << std::endl;
    return -1;
  }

  pcClientIp = inet_ntoa(this->__strClientInfo.sin_addr);
  std::cout << "Connection accepted from " << pcClientIp << std::endl;

  return 0;
}

int clTcpServer::receiveDataBuffer(void * pvidBuffer, size_t sizeMaxBufferSize)
{
  int iRecvSize;

  iRecvSize = recv(this->__iDistantSocketId, pvidBuffer, sizeMaxBufferSize, (int)0);

  if (iRecvSize < (int)0 )
  {
    std::cout << "TCP server - ERROR - Recv failed" << std::endl;
    return -1;
  }

  return iSentSize;
}

客户:

int clTcpClient::openSocket(void)
{
  /* Create a new socket for client comm. */
  this->__iSocketId = socket( AF_INET,      //stand for IPV4
                              SOCK_STREAM,  //using TCP protocol
                              0);           //no specified / ip protocol

  if (this->__iSocketId == (int)-1)
  {
    std::cout << "TCP client - ERROR - Can't open Socket" << std::endl;
    return -1;
  }

  return 0;
}

int clTcpClient::connectToServer(std::string stgIpAddress, uint16_t u16Port)
{
  /* configure server info according to parameters */
  this->__strServerInfo.sin_family = AF_INET; //stand for IPV4
  this->__strServerInfo.sin_port = htons(u16Port);

  /* Retrieve IP addr*/
  this->__strServerInfo.sin_addr.s_addr = inet_addr(stgIpAddress.c_str());

  /* Try to connect to the server */
  if (connect(this->__iSocketId,
              (sockaddr*)&this->__strServerInfo,
              sizeof(this->__strServerInfo)) < 0)
  {
    std::cout << "TCP client - ERROR - Can't connect to " << stgIpAddress << ":" << std::dec << (int)u16Port << std::endl;
    return -1;
  }

  std::cout << "TCP client - Connected" << std::endl;

  return 0;
}

int clTcpClient::sendDataBuffer(const void *kpvidData, size_t sizeDataLength)
{
  ssize_t sizeSentSize;

  /* Send the buffer */
  sizeSentSize = send(this->__iSocketId, kpvidData, sizeDataLength, (int)0);

  if (sizeSentSize < (ssize_t)0 )
  {
    std::cout << "TCP client - ERROR - Send failed" << std::endl;
    return -1;
  }

  std::cout << "TCP client - " << (int)sizeSentSize << " byte(s) sent." << std::endl;

  return 0;
}

这工作正常,但这里的recv()没有阻塞。如果我调用它(当然,在初始化了所有的东西之后),如果没有任何东西要接收,函数返回0,如“收到0字节”,这就是全部。

这意味着如果我想将这个rec​​v函数放在一个循环中,CPU正在100%工作以接收......没有。

这里有什么问题?如何才能阻止

谢谢

0 个答案:

没有答案