interface/httplink.c
int httplink_connect(httplink_t *connection, char *hostname, int port) {
  struct hostent *he;
  struct in_addr **addr_list;
  struct sockaddr_in main_server;
  int i;
  connection->hostname = hostname;

  // url -> ip
  if ((he = gethostbyname(connection->hostname)) == NULL) {
    return -1;
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    strcpy(connection->ipaddr, inet_ntoa(*addr_list[i]));
  }

  // open socket connection
  if ((connection->socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  if (port == -1) {
    port = 80; // default port for http
  }
  main_server.sin_addr.s_addr = inet_addr(connection->ipaddr);
  main_server.sin_family = AF_INET;
  main_server.sin_port = htons(port); // http protocol
  if (connect(connection->socket_fd,
      (struct sockaddr *)&main_server,
      sizeof(main_server)) == -1) {
    return -1;
  }
  connection->connected = 1;
  return 0;
}
/** Connect to the main server for our robot's manual interface.
 *  @param connection
 *    the connection information for the server
 *  @param hostname
 *    the hostname of the server
 *  @param port
 *    optional port to provide
 *  @return 0 on success, -1 otherwise
 */
Connect to the main server for our robot's manual interface.
@param connection
the connection information for the server
@param hostname
the hostname of the server
@param port
optional port to provide
@return 0 on success, -1 otherwise
Connect to the main server for our robot ' s manual interface . @param connection the connection information for the server @param hostname the hostname of the server @param port optional port to provide @return 0 on success - 1 otherwise
int httplink_connect(httplink_t *connection, char *hostname, int port) {
  struct hostent *he;
  struct in_addr **addr_list;
  struct sockaddr_in main_server;
  int i;
  connection->hostname = hostname;
  if ((he = gethostbyname(connection->hostname)) == NULL) {
    return -1;
  }
  addr_list = (struct in_addr **)he->h_addr_list;
  for (i = 0; addr_list[i] != NULL; i++) {
    strcpy(connection->ipaddr, inet_ntoa(*addr_list[i]));
  }
  if ((connection->socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    return -1;
  }
  if (port == -1) {
    port = 80; 
  }
  main_server.sin_addr.s_addr = inet_addr(connection->ipaddr);
  main_server.sin_family = AF_INET;
  main_server.sin_port = htons(port); 
  if (connect(connection->socket_fd,
      (struct sockaddr *)&main_server,
      sizeof(main_server)) == -1) {
    return -1;
  }
  connection->connected = 1;
  return 0;
}
int httplink_connect ( httplink_t * connection , char * hostname , int port ) { struct hostent * he ; struct in_addr * * addr_list ; struct sockaddr_in main_server ; int i ; connection -> hostname = hostname ; if ( ( he = gethostbyname ( connection -> hostname ) ) == NULL ) { return -1 ; } addr_list = ( struct in_addr * * ) he -> h_addr_list ; for ( i = 0 ; addr_list [ i ] != NULL ; i ++ ) { strcpy ( connection -> ipaddr , inet_ntoa ( * addr_list [ i ] ) ) ; } if ( ( connection -> socket_fd = socket ( AF_INET , SOCK_STREAM , 0 ) ) == -1 ) { return -1 ; } if ( port == -1 ) { port = 80 ; } main_server . sin_addr . s_addr = inet_addr ( connection -> ipaddr ) ; main_server . sin_family = AF_INET ; main_server . sin_port = htons ( port ) ; if ( connect ( connection -> socket_fd , ( struct sockaddr * ) & main_server , sizeof ( main_server ) ) == -1 ) { return -1 ; } connection -> connected = 1 ; return 0 ; }
Connect to the main server for our robot's manual interface.
Connect to the main server for our robot ' s manual interface .
// url -> ip // open socket connection // default port for http // http protocol