Home away from home
Joined: 2007/5/19 14:23 Last Login
: 1/2 17:30
From England
Group:
Registered Users
|
I'm trying to do some very basic network stuff: Listening on a localhost port for a web browser to try to connect, and once it does my program quits. But it never seems to free it's port, so the next time my program runs, it fails to Bind() to the same port. And I can't work out why :(
Everything seems to be fine UNLESS I try to Recv() the message sent by the web browser. If I do that, then the port isn't freed, even though my program quits.
This seems to go against all the documentation, which claims that this kind of stuff should be freed by CloseSocket() and will definitely be freed when the library base is closed.
If it helps, the flow of my program is something like this:
OpenLibrary() socketS = Socket() Bind(socketS) Listen(socketS) socketC = Accept(socketS) Recv(socketC) CloseSocket(socketC) CloseSocket(socketS) CloseLibrary()
I've basically followed BSD examples (which often don't do a good job of closing stuff), so I'm wondering if I've missed a vital step when tidying-up.
EDIT: Never mind. Seems this problem is not what I thought it was, and it occurs on other platforms as well. For some strange reason I have to set SO_REUSEADDR to TRUE before I can reuse a localhost port.
EDIT: Using SO_REUSEADDR can apparently have unexpected side-effects, so I'm going to try using SO_REUSEPORT instead.
Edited by ChrisH on 2016/7/29 22:20:44 Edited by ChrisH on 2016/8/7 15:09:54
|