Thread: winsock problems

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    winsock problems

    HI,
    I have been trying to get sockets to work but for some reason it does not work:
    Code:
    const char *ip_address = "127.0.0.1";
    
    client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(client==INVALID_SOCKET) {
    return -1;
    }
    
    
    hostent *server_ip_address = gethostbyaddr(ip_address,sizeof(ip_address),AF_INET);
    gethostbyaddr returns null for some reason although I have a server program running and listening and I am sure the server program works because I tested it with telnet. I am trying to use part of the example on this website:
    http://www.codeproject.com/internet/winsockintro02.asp

    Hopefully somebody can help me out. Thanks
    Amish

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Can you give us a WSAGetLastError().

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Probably because you are passing sizeof(ip_address) which will return the size of const char* (4 on my system)

    Declare ip_address as an array instead:
    Code:
    const char ip_address[] = "127.0.0.1";
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by axr0284
    HI,
    I have been trying to get sockets to work but for some reason it does not work:
    Code:
    const char *ip_address = "127.0.0.1";
    
    client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(client==INVALID_SOCKET) {
    return -1;
    }
    
    
    hostent *server_ip_address = gethostbyaddr(ip_address,sizeof(ip_address),AF_INET);
    gethostbyaddr returns null for some reason although I have a server program running and listening and I am sure the server program works because I tested it with telnet. I am trying to use part of the example on this website:
    http://www.codeproject.com/internet/winsockintro02.asp

    Hopefully somebody can help me out. Thanks
    Amish
    As, JaWiB pointed out, you need to pass the length of the ip adrress instead of the size of a pointer. However, I would recommend using strnlen (if using C), instead of declaring an array because your code would document itself this way.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Thanks for the reply. I tried everything that you said and checked that the length passed to the function was correct this time but it still returns NULL. Error returned by WSAGetLastError() is as follows and description from msdn:
    WSANO_DATA
    11004

    Valid name, no data record of requested type.
    The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable.

    I am posting the whole code. Maybe the error is elsewhere. I'll read the website where I got this again just in case:
    Code:
    SOCKET client;
    WSADATA wsaData;
    sockaddr_in server;
    int wsaret=WSAStartup(0x101,&wsaData);
    if(wsaret!=0)
        {
            return 0;
        }
    
    
    string ip_address = "127.0.0.1";
    
    const char *ip_address_char= ip_address.c_str();
    
    client=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(client==INVALID_SOCKET) {
    return -1;
    }
    
    
    int length = strlen(ip_address.c_str());
    hostent *server_ip_address = gethostbyaddr(ip_address.c_str(),strlen(ip_address.c_str()),AF_INET);
    int err = WSAGetLastError();
    server.sin_addr.S_un.S_addr /*=*((unsigned long*)server_ip_address->h_addr);*/= (u_long) server_ip_address->h_addr_list[0];
    server.sin_family = AF_INET;
    server.sin_port = htons((u_short)20248);
    
    
    if(connect(client,(struct sockaddr*)&server,sizeof(server)))
    {
    closesocket(client);
    return -1;	
    }
    
    const char *output_message = "test";
    	
    char input_message[512];
    	
    send(client,output_message,sizeof(output_message),0);
    
    int y = 0;
    
    while(y=recv(client,input_message,512,0))
    {
    //f.Write(buff,y);
    cout << input_message << "/n";
    }
    
    closesocket(client);
    WSACleanup();
    
        return 0;
    Thanks for the help everybody. I really appreciate it.
    Amish

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Ok Figured it out.
    I would seem like gethostbyaddr() expects the first parameter to be a binary representation of the Internet address. Therefore the following conversion needs to be used before calling that function:
    Code:
    string ip_address = "127.0.0.1";
    unsigned int addr;
    addr= inet_addr(ip_address.c_str());
    hostent *server_ip_address = gethostbyaddr((char *) &addr, 4, AF_INET);
    It's a little strange to me why gethostbyaddr() would not just take a regular char representation of the ip address. Anyways now server_ip_address structure gets filled correctly. Thanks everybody for helping out.
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. winsock linking problems
    By Calef13 in forum Windows Programming
    Replies: 2
    Last Post: 08-29-2007, 08:54 AM
  2. Winsock Problems
    By Zort in forum C++ Programming
    Replies: 11
    Last Post: 06-22-2005, 05:01 AM
  3. Huge problems in Winsock recv!
    By sirSolarius in forum Networking/Device Communication
    Replies: 0
    Last Post: 09-11-2004, 09:34 AM
  4. Winsock - Asynchrnonous Server problems
    By neandrake in forum Windows Programming
    Replies: 6
    Last Post: 06-24-2003, 06:19 PM
  5. Complete Port I/O and Heap Problems :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2002, 12:45 AM