Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[net] Non portable options for "listen()" and "bind()"? #75

Open
daokoder opened this issue Sep 19, 2015 · 9 comments
Open

[net] Non portable options for "listen()" and "bind()"? #75

daokoder opened this issue Sep 19, 2015 · 9 comments

Comments

@daokoder
Copy link
Owner

I notice that the socket option SO_REUSEADDR seems to be available on both Windows and Unix, yet the recent change in DaoNetwork_SetSocketOptions() made it impossible to use that option in a portable way.

@Night-walker
Copy link
Collaborator

If I'm not mistaken, they're supported in consistent and portable way. Exactly as in Qt. Both on Windows and Unix reusing a socket does not automatically mean sharing it further. If that is to be changed, it should affect both variants.

@daokoder
Copy link
Owner Author

If I'm not mistaken, they're supported in consistent and portable way.

But one has to use the $reused option on windows and $shared option on unix to enable SO_REUSEADDR.

Both on Windows and Unix reusing a socket does not automatically mean sharing it further. If that is to be changed, it should affect both variants.

This SO_REUSEADDR option may not mean exactly the same thing on windows and unix, but I think it would allow restarting a program that binds to the same port immediately after it is terminated. So there should be a portable way to enable something like this.

@Night-walker
Copy link
Collaborator

But one has to use the $reused option on windows and $shared option on unix to enable SO_REUSEADDR.

Certainly, as these are different option on Windows and *nix.

This SO_REUSEADDR option may not mean exactly the same thing on windows and unix, but I think it would allow restarting a program that binds to the same port immediately after it is terminated. So there should be a portable way to enable something like this.

$shared + $reused. Reusing address does not 'prolong' its reusability for other binders, which is why separate option exists. Of course, the two options could be merged if no practical use for either of them without the other is envisaged.

@daokoder
Copy link
Owner Author

$shared + $reused

Right.

Reusing address does not 'prolong' its reusability for other binders, which is why separate option exists. Of course, the two options could be merged if no practical use for either of them without the other is envisaged.

I think, it would be nice to merge them. $reuse requires that the port is not created with $exclusive option, without this option the port is potentially shared. So it makes a lot sense to support just two options: shared and exclusive. Also, I think it is better to let the first user of a port to determine if the port can be shared or not.

@Night-walker
Copy link
Collaborator

Made it all simpler now.

@daokoder
Copy link
Owner Author

Made it all simpler now.

Good.

BTW, is there any good way to detect disconnection or determine if a socket has become disconnected?

@Night-walker
Copy link
Collaborator

BTW, is there any good way to detect disconnection or determine if a socket has become disconnected?

Reading from such socket returns 0 bytes, writing should produce error; select() indicates that the socket is readable, poll() should also report an event. The actual moment when disconnect is registered depends on the acknowledgement mechanics of TCP protocol and may vary. Finally, the keep-alive option can be employed to monitor a connection for being active.

@daokoder
Copy link
Owner Author

Reading from such socket returns 0 bytes, writing should produce error;

Currently I am exploiting this for the detection. Not very convenient, I am afraid.

Finally, the keep-alive option can be employed to monitor a connection for being active.

I haven't tried. But there are only two methods for setting and getting the keep-alive option, I don't see exactly how to do that with these two methods.

@Night-walker
Copy link
Collaborator

Reading from such socket returns 0 bytes, writing should produce error;

Currently I am exploiting this for the detection. Not very convenient, I am afraid.

This is basically the only portable way, optionally coupled with select() or poll(). poll() is expected to report actual disconnects (POLLHUP), but of course it cannot detect them when no traffic occurs between the endpoints for some time. Same applies to select().

Finally, the keep-alive option can be employed to monitor a connection for being active.

I haven't tried. But there are only two methods for setting and getting the keep-alive option, I don't see exactly how to do that with these two methods.

Well, you just enable the keep-alive on socket -- the method to track disconnects doesn't change. The option just makes inactive connection to periodically ping the remote end to see if it's alive, which may help to detect abnormal connection termination without explicit I/O. It is useful mainly for long-living connections which may stay idle for significant amount of time but nevertheless require to be notified about disconnects proactively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants