在C ++代码中重新启动连接时出现“Broken pipe”错误

时间:2014-12-12 20:34:08

标签: c++ sockets tcp pipe

我的服务器代码有点问题,当客户端关闭连接时再次重新启动它并继续监听另一个客户端,而不会杀死/重启整个程序。当我在重新启动后尝试发送数据包时,出现错误消息“Broken Pipe”。如果我杀死整个程序并立即重新启动它,一切正常。为什么?我只关闭插座;有其他命令给予适当的连接关闭? 按照我的代码:

重新启动连接部分:

if(command[14]==END_TRANS)
{
  command[14] = 0x00;

  // Close serial and TCP communications and Opencv processes
  close_capture = 1;
  serial->~UARTcom();
  tcp_server->~TCPServer();
  std::cout<<""<<std::endl;
  std::cout<<"Restarting connections..."<<std::endl;
  sleep(2);

  // Re-Start serial and TCP communications
  UARTcom *serial = new UARTcom(serial_device);
  TCPServer *tcp_server = new TCPServer(tcp_port);
  memset(size_imudata_buff, 0, sizeof(int)+DATA_SIZE);
  memset(ack_imucomm_buff, 0, sizeof(int)+COMMAND_SIZE);
  if(pthread_create(&th_capture, NULL, capture, NULL))
    {
      perror("main() Create th_stream failed");
      exit(1);
    }
  continue;
}

TCPServer类:

/**********************************************************************
 *CONSTRUCTOR: Create a new socket, establishing a default destination
 *to the port.
 *********************************************************************/
TCPServer::TCPServer(int serv_port)
{  
  // Adress initialization
  memset((char *)&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = PF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(serv_port);

  // Create socket
  if((serverSock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
      std::perror("TCPServer() Create socket failed");
      exit(1);
    }
  std::cout<<"TCPServer() Created socket sd="<<serverSock<<std::endl;

  int optval = 1;
  if(setsockopt(serverSock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0)
    {
      std::perror("TCPServer() Set option failed");
      exit(1);
    }

  // Bind socket
  if(bind(serverSock,(struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
      std::perror("TCPServer() Bind failed"); 
      exit(1);
    }
  std::cout<<"TCPServer() Bind socket"<<std::endl;

  // Listen for incoming connection
  if(listen(serverSock,5) < 0)
    {
      std::perror("TCPServer() Listen failed"); 
      exit(1);
    }
  std::cout<<"TCPServer() --> Waiting for TCP connection on port "<<serv_port<<std::endl;

  // Accept connection and receive client address
  if((connectSock = accept(serverSock,(sockaddr*)&clientaddr,&clientaddrlen))<0)
    {
      std::perror("TCPServer() Accept failed"); 
      exit(1);
    }
  std::cout<<"TCPServer() --> TCP connection estabilished with "<<inet_ntoa(clientaddr.sin_addr)<<std::endl;
}


/**********************************************************************
 *DESTRUCTOR: Close the socket
 *********************************************************************/
TCPServer::~TCPServer(void)
{
  if(close(serverSock) < 0)
    {
      std::perror("TCPServer() Close socket failed");
      exit(1);
    }
  std::cout<<"TCPServer() Socket closed"<<std::endl;
}


/**********************************************************************
 *TCPServer::receiveData() receive data from socket defined
 *through connect() function 
 *********************************************************************/
int TCPServer::receiveData(char* data, int len)
{ 
  return recv(connectSock, data, len, 0);
} 


/**********************************************************************
 *TCPServer::sendData() send data to socket defined through
 *connect() function 
 *********************************************************************/
int TCPServer::sendData(char* data, int len)
{
  return send(connectSock, data, len, 0);
}

我运行程序然后断开连接重新连接客户端时得到的输出:

UARTcom() Serial port open on /dev/ttyUSB0
TCPServer() Created socket sd=13
TCPServer() Bind socket
TCPServer() --> Waiting for TCP connection on port 8080
TCPServer() --> TCP connection estabilished with 192.168.1.56
UARTcom() Serial connection closed
TCPServer() Socket closed

Restarting connections...
UARTcom() Serial port open on /dev/ttyUSB0
TCPServer() Created socket sd=13
TCPServer() Bind socket
TCPServer() --> Waiting for TCP connection on port 8080
TCPServer() --> TCP connection estabilished with 192.168.1.56
main() Failed to send size of frame and IMU data: Broken pipe
main() Failed to send size of frame and IMU data: Broken pipe
main() Failed to send size of frame and IMU data: Broken pipe
main() Failed to send size of frame and IMU data: Broken pipe

任何人都可以解释我为什么?

1 个答案:

答案 0 :(得分:1)

你在这做什么::

serial->~UARTcom();

这不是删除对象的方法(如果那是你想要做的)。你应该使用:

delete serial;

也许get a book and learn from the ground up - ?