views:

22

answers:

2

I am trying to connect two of the same app with winsock, but the connecting side has to use ConnectEx() which requires a bound socket. So the problem is that when I try to loop back using ip 127.0.0.1, I get error 10048(WSAEADDRINUSE).

Is there any way around this problem?

Thanks for any help

A: 

Make sure your two app instances are configured up to use different ports.

  • Instance 1 listens on port X, connects to port Y
  • Instance 2 listens on port Y, connects to port X
Steve Townsend
Each app was only listening, or connecting at any given time, but did satisfy your example. Your answer seemed to imply that I should have them in separate processes(I had both sockets in one process for testing), and now the binding problem seems to have gone away. I have an even stranger problem now though.. The call to ConnectEx in the client side properly returns WSA_IO_PENDING like it should, but when I call this:GetOverlappedResult((HANDLE)client.socket, (OVERLAPPED*)op_connect, it returns true right away....even if there is no server waiting to accept the connection...
Nick
If nobody is listening then GetOverlappedResult may indeed return right away, but should indicate connect failure. GetOverlappedResult should only return false if you pass in a bad handle, or the like. You need to make sure that the handle in your OVERLAPPED struct is in signalled state before you can be sure ConnectEx operation is done, and check its status.
Steve Townsend
I'm not sure if thats true..because GetOverlappedResult WOULD block if it was used on AcceptEx, and a connection had not been accepted yet. Originally, I made the test app because I thought there error was on the listening side...but as it turns out, the problem is that ConnectEx is notifying of successful completion immediately without actually connection.. in the test app, a call to WSASend that was after the bogus call to ConnectEx also returned success.. This is really confusing..
Nick
also, callingSleep(5000);getsockopt(client.socket, SOL_SOCKET, SO_CONNECT_TIME, (char *)reports that it has been connected 5 seconds.. but no one is listening ^_-
Nick
+2  A: 

Although ConnectEx() requires a bound socket you don't need to select a port and explicitly bind it you can bind to a wildcard address simply leave the port as 0 and the OS will select one for you as it normally does with outbound connections.

Len Holgate
Awesome! for some reason, this actually fixed the problem. Now ConnectEx() works properly, instead of as described in the comments under Len's post. Thanks, guys for your help =)
Nick
I mean under Steve's post sorry
Nick