![]() ![]() You are creating multiple sockets that are explicitly bound to random ephemeral ports until you have bound a socket that is within 10 ports less than your target port. Is this a bug in Winsock? Is this a bug in my code?Įdit: Here is a screenshot of TcpView with the offending connection shown:įirst off, only newer versions of Windows use 5000–65534 as ephemeral ports. The resulting socket receives any data that is sent to it. On my Windows 7 machine, a connection is successfully established and it prints Connected to port 49400, used local port 49400. On my Mac machine, this eventually terminates with Unable to connect to port 49400, used local port 49400. Print 'Connected to port %d, used local port %d' (TARGET_PORT, port) Print 'Unable to connect to port %d, used local port %d: %s' % (TARGET_PORT, port, err) If port > TARGET_PORT - 10 and port < TARGET_PORT: Return socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) I first got it to happen in an application that repeatedly tries to connect to a certain port on localhost, and when the service is not listening it very rarely successfully establishes a connection and receives the message that it initially sent (which happens to be a Redis PING command).Īn example, in Python (run with nothing listening to the target port): import socket If I create sockets until the assigned number is just below my target port number, and then repeatedly create a socket and attempt to connect to that port number, I can usually get the socket to connect to itself. Specifically, Windows appears to have a system-wide rolling port number which is the next port that it will try to assign as a local port number for a client socket. I can reliably get a Winsock socket to connect() to itself if I connect to localhost with a port in the range of automatically assigned ephemeral ports (5000–65534).
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |