[Dev-C++] UDP Client Socket - Retrieve Local Ip Address
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Rafael M. <so...@gm...> - 2007-11-22 19:30:52
|
Hi List I have a client udp socket connection and wish to retrieve the client ip address. I will print now the code structure and further information's will come below. SOCKET sock; SOCKADDR_IN sins; int sinsize=sizeof(sins); WSADATA wsa; WSAStartup(MAKEWORD(2,0),&wsa); sins.sin_family=AF_INET; sins.sin_addr.s_addr=inet_addr(ip); sins.sin_port=htons(port); sock=socket(AF_INET,SOCK_DGRAM,0); struct sockaddr_in stLclAddr; int addrLen = sizeof(struct sockaddr_in); int rVal = getsockname(sock,(struct sockaddr FAR *)&stLclAddr,&addrLen); //int rVal = getsockname(sock,(sockaddr *)&stLclAddr,&addrLen); char szIP[MAX_PATH] = "\0"; sprintf(szIP,"%s",inet_ntoa(stLclAddr.sin_addr)); WriteLog(_T(szIP));//writelog just dump the value to a file or somewhere else rVal = getpeername(sock,(struct sockaddr FAR *)&stLclAddr,&addrLen); //rVal = getpeername(sock,(sockaddr *)&stLclAddr,&addrLen); sprintf(szIP,"%s",inet_ntoa(stLclAddr.sin_addr)); WriteLog(_T(szIP)); When i run the follow code, the output sin_addr is always 204.204.204.204, I don't know why. I cant bind the socket because i like the sysop api give-me the in port (to recvfrom), and i just don't know witch interface on the client machine have Internet connection (and don't like to search. let the sysop do their jobs alone). After know that ip and port, i need to sniff this interface(that job is already done, and need the ip input information to watch the inbound packets and pick, from the packet header, my outside ip:port. with that, I can have a listener on the client and set up ip and port in the server sending a command by UPD). Thanks in advance. -- Regards Rafael Menezes |