CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Network Programming Chat about all aspects of network programming, including raw sockets, Winsock API, BSD sockets, MFC-based CSocket, CAsyncSocket, and other network-related topics.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 3rd, 2009, 12:27 PM
    Anchoa23 Anchoa23 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    Anchoa23 is an unknown quantity at this point (<10)
    Why can't I pass a char* to innet_addr?

    Hi there!

    I'm writing a code that will make connections with different servers and I need to create a socket for each of them. The problem arises when I initialize the SOCKADDR_IN sockAddr struct when I try the connect function. More specifically, when I use:

    sockAddr.sin_addr.s_addr = inet_addr(_ipBOARD);

    where char* _ipBOARD is defined in my program when read from a config file. When I try this, inet_addr returns -1, but if I try sockAddr.sin_addr.s_addr = inet_addr("173.20.20.199"); there is no prob. I checked the _ipBOARD and it is initialized to the correct IP.

    Any clue?

    Thanks a lot in advance!
    Reply With Quote
      #2    
    Old November 3rd, 2009, 02:46 PM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,189
    hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)
    Re: Why can't I pass a char* to innet_addr?

    Hard to tell without some code. Is _ipBOARD NULL terminated?
    Reply With Quote
      #3    
    Old November 3rd, 2009, 03:45 PM
    0xC0000005 0xC0000005 is offline
    Senior Member
     
    Join Date: May 2002
    Posts: 1,362
    0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)0xC0000005 is a name known to all (1000+)
    Re: Why can't I pass a char* to innet_addr?

    Do you know how to use Help or MSDN? This is what they say about the return value of inet_addr. INADDR_NONE is defined as 0xffffffff which is most likely what you are reporting as -1:

    Return Value

    If no error occurs, the inet_addr function returns an unsigned long value containing a suitable binary representation of the Internet address given.

    If the string in the cp parameter does not contain a legitimate Internet address, for example if a portion of an "a.b.c.d" address exceeds 255, then inet_addr returns the value INADDR_NONE.

    On Windows Server 2003 and later if the string in the cp parameter is an empty string, then inet_addr returns the value INADDR_NONE. If NULL is passed in the cp parameter, then inet_addr returns the value INADDR_NONE.

    On Windows XP and earlier if the string in the cp parameter is an empty string, then inet_addr returns the value INADDR_ANY. If NULL is passed in the cp parameter, then inet_addr returns the value INADDR_NONE.


    Also, do you know how to use the debugger to stop just before calling inet_addr() and examine the actual value held by your string?
    Reply With Quote
      #4    
    Old November 5th, 2009, 09:05 AM
    Anchoa23 Anchoa23 is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 2
    Anchoa23 is an unknown quantity at this point (<10)
    Re: Why can't I pass a char* to innet_addr?

    Thanks for your help. Yep, I used msdn and everything and then I discovered (I'm not used to programming c++ with pointers) that I wasn't working correctly with my char*.

    At first I declared it in my CSocket constructor as a char* _ipBoard, later on I assigned it as _ipBoard = ipBoard (read from a config file) and when I tried to use it for the connect() I noticed my _ipBoard contained nothing at all. So I changed the declaration to char _ipBoard[50], used the strncpy to copy it to the other array (ipBoard), and now everything works fine.

    Thanks a lot anyway!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:44 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009