0

I am trying to create a client application that will use asynchronous IO using IOCP. I already did similar server application and it works correctly, however I can't find any information on how to extract local endpoint information from socket that is connected via ConnectEx API.

With server sockets, documentation states that info about both local and remote endpoints will be part of buffer sent to the AcceptEx. There is not a similar thing in ConnectEx. I tried also to extract local endpoint information through getsockname, however this returned some garbage values. I also tried to use setsockopt(clientSocket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, ...) prior to calling getsockname, however the result was same as without it. Is there a way to do this or am I misunderstanding something?

Alexander Bily
  • 925
  • 5
  • 18

1 Answers1

0

I also use ConnectEx() function and when the asynchronous Connect operation completes on IOCP, I normally call

::setsockopt(m_Socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, nullptr, 0);

where m_Socket is my connect socket on a client side. It works and then I can get the local name by the ::getsockname() like this:

#define GoClearStruct(Struct)   memset(&Struct, 0, sizeof(Struct))

sockaddr_storage _AddrStorage;
sockaddr *_Addr = (sockaddr *)&_AddrStorage;
int _AddrLen = sizeof(_AddrStorage);
GoClearStruct(_AddrStorage);
if (::getsockname(m_Socket, _Addr, &_AddrLen) != SOCKET_ERROR) 
{
    // extract the local name
}

It is difficult to find out the problem in your code, could you share more from it? Did you check the return values of ::setsockopt() and ::getsockname()?

Vladeno
  • 1
  • 1
  • 2