caffe-distri/src/main/cpp/util/socket.cpp
// Real connect call without retries (called by connect above)
Real connect call without retries (called by connect above)
Real connect call without retries ( called by connect above )
int SocketChannel::connect_to_peer(string to_peer, string to_port) {
  struct hostent* serv = gethostbyname(to_peer.c_str());
  if (serv == NULL) {
    LOG(ERROR) << "ERROR: No peer by name [" << to_peer.c_str() << "]";
    return 0;
  }
  int serv_port = atoi(to_port.c_str());
  int client_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (client_fd < 0) {
    LOG(ERROR) << "ERROR: Unable to open client socket to peer ["
               << to_peer.c_str() << "]";
    return 0;
  }
  struct sockaddr_in serv_addr;
  bzero(reinterpret_cast<char *>(&serv_addr), sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(serv_port);
  bcopy(reinterpret_cast<char *>(serv->h_addr),
        reinterpret_cast<char *>(&serv_addr.sin_addr.s_addr),
        serv->h_length);
  char server_hostname[256];
  gethostnamebysinaddr(serv_addr, server_hostname, 256);
  if (connect(client_fd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) {
    LOG(ERROR) << "ERROR: Unable to connect to socket server ["
               << server_hostname <<"]";
    close(client_fd);
    return 0;
  }
  LOG(INFO) << "Connected to server [" << server_hostname
            <<":"<< serv_port<< "] with client_fd ["<< client_fd << "]";
  return client_fd;
}
int SocketChannel :: connect_to_peer ( string to_peer , string to_port ) { struct hostent * serv = gethostbyname ( to_peer . c_str ( ) ) ; if ( serv == NULL ) { LOG ( ERROR ) << " " << to_peer . c_str ( ) << " " ; return 0 ; } int serv_port = atoi ( to_port . c_str ( ) ) ; int client_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ; if ( client_fd < 0 ) { LOG ( ERROR ) << " " << to_peer . c_str ( ) << " " ; return 0 ; } struct sockaddr_in serv_addr ; bzero ( reinterpret_cast < char * > ( & serv_addr ) , sizeof ( serv_addr ) ) ; serv_addr . sin_family = AF_INET ; serv_addr . sin_port = htons ( serv_port ) ; bcopy ( reinterpret_cast < char * > ( serv -> h_addr ) , reinterpret_cast < char * > ( & serv_addr . sin_addr . s_addr ) , serv -> h_length ) ; char server_hostname [ 256 ] ; gethostnamebysinaddr ( serv_addr , server_hostname , 256 ) ; if ( connect ( client_fd , ( struct sockaddr * ) & serv_addr , sizeof ( serv_addr ) ) < 0 ) { LOG ( ERROR ) << " " << server_hostname << " " ; close ( client_fd ) ; return 0 ; } LOG ( INFO ) << " " << server_hostname << " " << serv_port << " " << client_fd << " " ; return client_fd ; }
Real connect call without retries (called by connect above)
Real connect call without retries ( called by connect above )
// Parse socket server details to connect // Create a client socket // Initialize the socket structs // Domain socket for internet // Convert to network byte order // Get the server ip address