#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    sockets: connect() failed [urgent]


    Hi All,

    when I tried to use the function connect to make a connection to a previously created socket I get the error no. 2: No Such file or directory , can anyone help me on that, it is urgent.

    here is the code I used to try to make the connection.

    thanks in advance.

    int create_send_connection(int port, char *hostname)
    {
    int sock;
    struct sockaddr_in pin;
    struct hostent *hp;

    /* go find out about the desired host machine */
    if ((hp = gethostbyname(hostname)) == 0) {
    wx_ipc_set_error( wx_get_errno ) , "gethostbyname", hostname);
    return(-1);
    }

    /* fill in the socket structure with host information */
    memset(&pin, 0, sizeof(pin));
    pin.sin_family = AF_INET;
    pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
    pin.sin_port = htons(port); /* convert Windows format number to network format number */

    /* grab an Internet domain socket */
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    wx_ipc_set_error(wx_get_errno(), "socket", "");
    return(-1);
    }

    /* connect to PORT on HOST */
    if (connect(sock, (struct sockaddr *)&pin, sizeof(pin)) == -1) {
    wx_ipc_set_error(wx_get_errno(), "connect", "");
    perror("Unable to connect");
    return(-1);
    }

    return sock;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You should enclose your code in code tags to retain the indentation and keep it readable. Code tags are similar to HTML tags, except that they are placed within square brackets. The first tag contains the keyword code and the second the keyword /code .

    I think your problem may be in your usage of the hostent struct. Here is its declaration:
    Code:
    struct  hostent {
         char *  h_name;     
         char ** h_aliases; 
         int     h_addrtype;  
         int     h_length;    
         char ** h_addr_list;
    };
    
    #define h_addr  h_addr_list[0]
    The h_addr_list contains character strings, not binary addresses (id est, of type struct in_addr, which contains the field s_addr). Therefore h_addr is a char array, not a long that you can plug directly into your sockaddr_in.

    It needs to be converted first, but there is a standard function that does that for you:
    Code:
    /* fill in the socket structure with host information */ 
    memset(&pin, 0, sizeof(pin)); 
    pin.sin_family = AF_INET; 
    pin.sin_addr.s_addr = inet_addr(hp->h_addr); 
    pin.sin_port = htons(port); /* convert Windows format number to network format number */
    inet_addr returns a -1 if there is an error.

    EDIT:
    Hold it one second there! Your comment mentions Windows. Are you doing this in Windows or in a UNIX/Linus/FreeBSD environment? If in Windows, are you familiar enough with Winsock to know what you need to do to make it work? Most UNIX sockets programs should work in Winsock with only a few minor changes, but you need to know what those changes are.
    Last edited by dwise1_aol; September 14th, 2003 at 12:59 PM.

IMN logo majestic logo threadwatch logo seochat tools logo