Exemple #1
0
trComm *commNewAndConnect(char *hostname, char *port, int connectTimeout){
  int fd;
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s;
  int rc;
  int status=1;

  //
  // The following code is an adaptation from the example in man getaddrinfo
  //

  // Obtain address(es) matching host/port
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
  hints.ai_socktype = SOCK_STREAM; /* Stream socket */
  hints.ai_flags = 0;
  hints.ai_protocol = 0;          /* Any protocol */
  
  s = getaddrinfo(hostname, port, &hints, &result);
  if (s != 0) {
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, 
		  "getaddrinfo on hostname \"%s\": %s\n", hostname, gai_strerror(s));
  }
  
  // getaddrinfo() returns a list of address structures.
  // Try each address until we successfully connect(2).
  // If socket(2) (or connect(2)) fails, we (close the socket
  // and) try the next address. */
  
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (fd == -1)
      continue;

    rc = connectWithTimeout(fd , rp->ai_addr, rp->ai_addrlen, connectTimeout);

    if (rc > 0)
      break;                  /* Success */
    
    if (close(fd) < 0)
      ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "close");
  }

  freeaddrinfo(result);           /* No longer needed */

  if (rp == NULL) {               /* No address succeeded */
    return NULL;
  }

  // We set TCP_NODELAY flag so that packets sent on this TCP connection
  // will not be delayed by the system layer
  if (setsockopt(fd,IPPROTO_TCP, TCP_NODELAY, &status,sizeof(status)) < 0){
    //free(aComm);
    ERROR_AT_LINE(EXIT_FAILURE, errno, __FILE__, __LINE__, "setsockopt");
  }

  // Everything went fine: we can return a communication handle.
  return commAlloc(fd);
}
/**
 * @brief Destructor.
 * Stop serving images and destroy this class.
 */
PCVideoServer::~PCVideoServer()
{
    // Stop the images to PC server.
    Stop();
    // Clear the error so that you can use this object to make a connection to
    // the VIDEO_TO_PC_PORT to stop the ImageToPCServer if it is waiting to
    // accept connections from a PC.
    ClearError();
    // Open a socket.
    int camSock = socket(AF_INET, SOCK_STREAM, 0);
    if (camSock == ERROR)
    {
	wpi_setErrnoError();
	return;
    }
    ScopedSocket scopedCamSock(camSock);
    // If successful
    if (!StatusIsFatal())
    {
	//  Create a connection to the localhost.
	struct sockaddr_in selfAddr;
	int sockAddrSize = sizeof(selfAddr);
	bzero ((char *) &selfAddr, sockAddrSize);
	selfAddr.sin_family = AF_INET;
#if defined(__FreeBSD__) || defined(NETBSD)
	selfAddr.sin_len = (u_char) sockAddrSize;
#endif
	selfAddr.sin_port = htons (VIDEO_TO_PC_PORT);

	if (( (int)(selfAddr.sin_addr.s_addr = inet_addr (const_cast<char*>("localhost")) ) != ERROR) ||
	    ( (int)(selfAddr.sin_addr.s_addr = hostGetByName (const_cast<char*>("localhost")) ) != ERROR))
	{
	    struct timeval connectTimeout;
	    connectTimeout.tv_sec = 1;
	    connectTimeout.tv_usec = 0;
	    connectWithTimeout(camSock, (struct sockaddr *) &selfAddr, sockAddrSize, &connectTimeout);
	}
    }
}