Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » How to obtain IP/port information out of a socket
How to obtain IP/port information out of a socket [message #13721] Thu, 24 January 2008 12:07 Go to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
Hi!

I have a server program which can accept multiple clients. I need to extract the IP information from the client socket (the one returned by accept), and then to send a message to the client so that it can become a server on another port. I couldn't find anything in sources/docs that gives this information.
Re: How to obtain IP/port information out of a socket [message #13725 is a reply to message #13721] Thu, 24 January 2008 17:55 Go to previous messageGo to next message
zaurus is currently offline  zaurus
Messages: 42
Registered: May 2006
Member
Hi!

You can get the IP from the Accept method. I do it like this.

Socket m_Socket, m_Connection;
dword m_ipaddr;
String sIP;

m_Socket.Accept(m_Connection, &m_ipaddr, true, 100);
sIP = FormatIP(m_ipaddr);


Hope this helps.

Happy coding.

Zaurus
Re: How to obtain IP/port information out of a socket [message #13748 is a reply to message #13725] Fri, 25 January 2008 20:16 Go to previous messageGo to next message
captainc is currently offline  captainc
Messages: 278
Registered: December 2006
Location: New Jersey, USA
Experienced Member
Thanks zaurus.

Although, when I call FormatIP(), I get
168.192.2.5
when it should be
192.168.5.2

What is going on?

[Updated on: Fri, 25 January 2008 20:20]

Report message to a moderator

Re: How to obtain IP/port information out of a socket [message #13762 is a reply to message #13748] Sat, 26 January 2008 05:22 Go to previous messageGo to next message
zaurus is currently offline  zaurus
Messages: 42
Registered: May 2006
Member
I think this is a 'bug' in FormatIP on Windows platform.

After having a look at the FormatIP function, I think it doesn't take care of the different order of 'Low Byte' & 'High Byte' on Windows compared to Linux. On Linux the function returns the correct IP address.

Maybe one of the developers can have look and fix this.

Zaurus
Re: How to obtain IP/port information out of a socket [message #13830 is a reply to message #13762] Tue, 29 January 2008 14:22 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Are you sure about FormatIP being wrong ?

String FormatIP(dword _ip)
{
	byte ip[4];
	Poke32be(ip, _ip);
	return Format("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}


I guess there is a little room for error there...

Mirek
Re: How to obtain IP/port information out of a socket [message #13835 is a reply to message #13830] Tue, 29 January 2008 14:43 Go to previous messageGo to next message
captainc is currently offline  captainc
Messages: 278
Registered: December 2006
Location: New Jersey, USA
Experienced Member
Yes, I am using this on Windows Server 2003 running in VirtualBox virtual machine (as development platform). I'm not sure if that could be the issues as I have yet to test it with 32-bit Windows running directly on hardware.

Would the virtual machine be the issue?
Can someone else please test this?

For the time being, I am using:
String FormatIPStr(dword _ip)
{
    byte ip[4];
    Poke32be(ip, _ip);
    #ifdef PLATFORM_WIN32
    return Format("%d.%d.%d.%d", ip[1], ip[0], ip[3], ip[2]);
    #else
    return Format("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
    #endif	
}

[Updated on: Tue, 29 January 2008 14:48]

Report message to a moderator

Re: How to obtain IP/port information out of a socket [message #13839 is a reply to message #13835] Tue, 29 January 2008 16:31 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
I can also confirm. The result from Accept, after beeing passed through FormatIP from a localhost connection is: 0.127.1.0. (Windows, 2008.1beta)

captainc's fix works.
Re: How to obtain IP/port information out of a socket [message #13848 is a reply to message #13839] Tue, 29 January 2008 22:41 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Confirmed as bug. Thanks.

Please check this fix:

SOCKET Socket::Data::AcceptRaw(dword *ipaddr, int timeout_msec)
{
	ASSERT(IsOpen());
	if(!Peek(timeout_msec, false))
		return INVALID_SOCKET;
	sockaddr_in addr;
	Zero(addr);
	socklen_t addr_len = sizeof(addr);
//	puts("Socket::Accept: accepting socket...");
	SOCKET connection = accept(socket, (sockaddr *)&addr, &addr_len);
	if(connection == INVALID_SOCKET) {
		SetSockError("accept");
		return INVALID_SOCKET;
	}
//	puts("Socket::Accept: socket accepted...");
	dword ip = ntohl(addr.sin_addr.s_addr);
	if(ipaddr)
		*ipaddr = ip;
	SLOG("Socket::Accept() -> " << (int)connection << " &" << FormatIP(ip));
	return connection;
}


Mirek
Re: How to obtain IP/port information out of a socket [message #13873 is a reply to message #13848] Wed, 30 January 2008 17:42 Go to previous message
captainc is currently offline  captainc
Messages: 278
Registered: December 2006
Location: New Jersey, USA
Experienced Member
I confirm this is now working for me on Windows. I replaced the Socket::Data::AcceptRaw function in socket.cpp, recompiled, and tested.

Can someone test on Linux? I am not set up for it at the moment.

Good stuff Mirek, thanks for the quick fix to this.

[Updated on: Wed, 30 January 2008 17:43]

Report message to a moderator

Previous Topic: MT Documentation
Next Topic: Conditional Variables
Goto Forum:
  


Current Time: Thu Apr 18 21:09:12 CEST 2024

Total time taken to generate the page: 0.02439 seconds