0

I have a client application that uses IOCP for socket communication. I'm using ConnectEx to make the TCP connection to the remote endpoing (binding the socket to ADDR_ANY and port 0 before calling ConnectEx).

It will be valid to have two connections to the same remote endpoint (same IP address and port number). When I test that condition with my current code, I have two overlapped IO read operations outstanding (one on each connected socket) from calls to WSARecv(). Each WSARecv() is called with the correct socket and overlapped structure. For example: WSARecv(socket1, ... overlapped1) and WSARecv(socket2, ... overlapped2). The problem I've run into is that when I get a response back from either remote, it triggers the completion event for both of the outstanding overlapped operations. My code only produces this result when two remotes have the same ip address and port number, not when there are two unique remote addresses. Is this the expected behavior (hopefully not)? If so, is there another way to accomplish this?

Ken
  • 427
  • 4
  • 20
  • You may *think* your code is correct. but you still have to post it. The bind before connect is pointless. – user207421 Nov 07 '17 at 22:54
  • Right! I'll post it in the morning. I just think it's correct due to the different behavior when using two duplicate remote addresses vs two unique remote addresses. Microsoft docs state the bind is required when using ConnectEx(). I haven't tried to connect without the bind. – Ken Nov 07 '17 at 23:07
  • 3
    "*when I get a response back from either remote, it triggers the completion event for both of the outstanding overlapped operations*" - that is not possible if you have correctly separated the `OVERLAPPED` instances for each read. You clearly have a bug in your IOCP code. Using the same remote IP/Port for 2 sockets has NOTHING to do with this issue. Socket connections are uniquely identified by protocol AND local IP/port AND remote IP/port. Multiple clients on the same machine can safely be connected to the same remote IP/port as long as they are bound to different local IP/ports. – Remy Lebeau Nov 08 '17 at 02:48
  • `ConnectEx()` requires the socket to already be bound (as it can reuse sockets that are closed with `DisconnectEx()`). `connect()` and `WSAConnect...()` do not, they will bind implicitly if not already bound. But you can't use `connect()` with IOCP, use `WSAConnect...()` or `ConnectEx()` for that. – Remy Lebeau Nov 08 '17 at 02:57

1 Answers1

0

I'm posting an answer, even though it is really just an explanation of why the problem happened.

My test involved connecting to and communicating with a remote device that provides data. It turns out that it is on the other side of a digi terminal server. So the connection path was:

my test computer (via TCP) -> Digi terminal server (via Serial) -> remote device.

The digi terminal server basically converts TCP/IP to serial communications, and back. Since the serial side doesn't have a concept of 'connectedness' the digi doesn't know which TCP/IP connection should receive the serial data in response to a TCP/IP request, so it forwards the serial data to all active connections on the TCP/IP side. That's what was producing the IOCP trigger on both of my pending overlapped operations. Every time a request was sent to the digi, it sent the request out of its serial port. When the end device responded, the digit forwarded the response data to each of my TCP/IP connections.

Thanks to everyone who commented on my question, but sorry for taking up your time.

Ken
  • 427
  • 4
  • 20