Click to See Complete Forum and Search --> : Help! How could I get a CSocket's IP address correctly?
Noodle
April 2nd, 1999, 09:22 PM
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.
Masaaki
April 2nd, 1999, 10:48 PM
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
Noodle
April 2nd, 1999, 11:24 PM
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.
Andy St.Clair
April 3rd, 1999, 12:34 AM
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.
Noodle
April 3rd, 1999, 03:15 PM
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.
Jason Coene
April 21st, 1999, 04:28 PM
Try GetPeerName();
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.