Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why can't I get a client host's IP through using AcceptEx?

3 views
Skip to first unread message

Z.Smith

unread,
Sep 8, 2005, 6:28:39 AM9/8/05
to
Hi:
I use AcceptEx to accept a client connection.When a client connect the
server and send a data packet.I use getpeername
to get it's IP and PORT.But the result value of the getpeername function
is -1.Then use the WSAGetLastError function get a
10057 result value.If using the accept function accept a client
connection,then I can get it's IP and PORT correctly.
How to get IP using AcceptEx?
Thanks!


Len Holgate

unread,
Sep 8, 2005, 6:59:35 AM9/8/05
to
> I use AcceptEx to accept a client connection.When a client connect the
> server and send a data packet.I use getpeername
> to get it's IP and PORT.But the result value of the getpeername function
> is -1.Then use the WSAGetLastError function get a

I assume you've read the docs? and called setsockopt() on the socket
after the accept completes? and you're still having problems?

If not:

See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/acceptex_2.asp

The key part of that page is "On Windows XP or later, once the AcceptEx
function completes and the SO_UPDATE_ACCEPT_CONTEXT option is set on the
accepted socket, the local address associated with the accepted socket
can also be retrieved using the getsockname function. Likewise, the
remote address associated with the accepted socket can be retrieved
using the getpeername function."

The code required is something like this:

if (SOCKET_ERROR == ::setsockopt(
acceptedSocket,
SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT,
(char *)&m_listeningSocket,
sizeof(m_listeningSocket)))
{
// handle error
}

and this should be done in the code that deals with the acceptex
completion packet, before you do much else with the socket...

--
Len Holgate - http://www.lenholgate.com
JetByte Limited - http://www.jetbyte.com
The right code, right now.
Contract Programming and Consulting Services.

Kellie Fitton

unread,
Sep 8, 2005, 12:22:16 PM9/8/05
to

Z.Smith

unread,
Sep 9, 2005, 12:04:26 AM9/9/05
to
Thank you!
I have an outher problem. If a client had connected,But it didn't
send a byte's data.Then how to get tt's ip and port?


"Len Holgate" <Len.H...@jetbyte.com> wrote in message
news:43201905$0$12920$cc9e...@news.dial.pipex.com...

Len Holgate

unread,
Sep 9, 2005, 3:39:40 AM9/9/05
to
Z.Smith wrote:
> Thank you!
> I have an outher problem. If a client had connected,But it didn't
> send a byte's data.Then how to get tt's ip and port?

I assume you're using AcceptEx in 'accept and read' rather than just in
'overlapped accept' mode and the client is not sending data when it
should...

I don't think there IS a documented way to determine the peer address
before the connection completes. However you could try using setsockopt
(SO_UPDATE_ACCEPT_CONTEXT) as detailed in the previous reply on the
socket after you have determined that it has accepted using getsockopt
(SO_CONNECT_TIME) but before the acceptex has completed... It's not
documented and I've never tried it and I don't expect it to work but you
could give it a go...

Why do you need to do this?

Kellie Fitton

unread,
Sep 9, 2005, 4:15:42 AM9/9/05
to
0 new messages