CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    11

    Help! How could I get a CSocket's IP address correctly?

    I had try these:
    CSocket* pSocket;
    CString IPAdress;
    UINT nPort;
    .
    .
    .
    pSocket = new CSocket();
    pSocket->Create;
    pSocket->GetSockName(IPAdress, nPort);
    .
    .
    .
    I can get nPort correctly, but IPAdress is "0.0.0.0". why? how can I get IPAdress correctly?

    An other question is when I derived a class from CSocket to connect to FTP Server, I find OnReceive can't work, when I write two CSocket to connect each
    other, it's worked good, why?
    Thanks.


  2. #2
    Join Date
    May 1999
    Location
    Atlanta, GA, USA
    Posts
    443

    Re: Do you want to know temporary IP address assigned by DHCP Server?

    Hi.

    This is a Remarks of CAsyncSocket::GetSockName()
    Call this member function to get the local name for a socket. This call is especially useful when a Connect call has been made without doing a Bind
    first:this call provides the only means by which you can determine the local association which has
    been set by the system.
    local association means (host address/port number)
    So first, I think that you must connect - use TCP.
    Or you want to know your IP temporary address assigned by DHCP Server of your ISP?

    Regards.
    -Masaaki Onishi


  3. #3
    Join Date
    Apr 1999
    Posts
    11

    In fact, I want know IP address before the connect be established.

    You know, when I write a FTP client, I must let server know which IP address and which port should be connected for data transfer, and my socket must listen on this port, so I can't establish connect (TCP) at first.
    Of cause, I can get local IP address by using gethostbyname, but I think if CSocket::GetSockName can work, that will be nice.
    Thanks.


  4. #4
    Join Date
    May 1999
    Posts
    25

    Re: In fact, I want know IP address before the connect be established.

    I think there is a lack of understanding here on
    teh working of TCP/IP and FTP, etc....
    If you are using Dial-Up-Networking you don't get
    an IP address until RAS has connected because IP
    addresses under this method are dynamic and
    assigned at the ISP end.
    So the IP address before connect is likely to be 0.0.0.0
    When a client connects to a server, the local socket is connected to a specified server IP and port.
    The client does not (usually) have to tell the
    server where to send data to.

    When FTP client connects to an FTP server, a second port number is automatically returned
    and this is the port over which data is transferred.

    This reply is getting a bit long so I will stop here.




  5. #5
    Join Date
    Apr 1999
    Posts
    11

    Re: In fact, I want know IP address before the connect be established.

    Thank you, I had find method to get IP address correctly. GetSockName can't return correct IP address before it be connected, even I had connect to my ISP though RAS, but I use control connection Socket to get this IP address. You know, I must connect to port 21 to establish control transfer connection, and then establish data transfer connection, so I can get control transfer connection's CSocket's IP address by GetSockName, and it's the same IP address with data transfer conntection.


  6. #6
    Join Date
    May 1999
    Posts
    12

    Re: Help! How could I get a CSocket's IP address correctly?

    Try GetPeerName();


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured