From 644cb60988ea393b62e1e6fc23588ef660c8272d Mon Sep 17 00:00:00 2001 From: limeboy Date: Fri, 25 Nov 2016 14:53:20 +0000 Subject: Windows support and minor touchups to the documentation. --- doc/socket.n | 2 +- win/tclWinSock.c | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/socket.n b/doc/socket.n index 532ea2e..823dbd5 100644 --- a/doc/socket.n +++ b/doc/socket.n @@ -140,7 +140,7 @@ actively listening on it. This is the default on Windows. \fB\-reuseport\fI boolean\fR . Tells the kernel whether to allow the binding of multiple sockets to the same -address and port as long as all of them specify the \fB-reuseport\fR option. +address and port. .PP Server channels cannot be used for input or output; their sole use is to accept new client connections. The channels created for each incoming diff --git a/win/tclWinSock.c b/win/tclWinSock.c index b228730..af22cf8 100644 --- a/win/tclWinSock.c +++ b/win/tclWinSock.c @@ -2039,7 +2039,7 @@ Tcl_OpenTcpServerEx( Tcl_Interp *interp, /* For error reporting - may be NULL. */ int port, /* Port number to open. */ const char *myHost, /* Name of local host. */ - unsigned int flags, /* Flags (not used) */ + unsigned int flags, /* Flags. */ Tcl_TcpAcceptProc *acceptProc, /* Callback for accepting connections from new * clients. */ @@ -2053,6 +2053,7 @@ Tcl_OpenTcpServerEx( char channelName[SOCK_CHAN_LENGTH]; u_long flag = 1; /* Indicates nonblocking mode. */ const char *errorMsg = NULL; + int optvalue; if (TclpHasSockets(interp) != TCL_OK) { return NULL; @@ -2111,9 +2112,17 @@ Tcl_OpenTcpServerEx( } /* - * Bind to the specified port. Note that we must not call - * setsockopt with SO_REUSEADDR or SO_REUSEPORT because Microsoft - * allows addresses and ports to be reused even if they are still in use. + * The SO_REUSEADDR option on Windows behaves like SO_REUSEPORT on unix + * systems. + */ + if (flags & TCL_TCPSERVER_REUSEPORT) { + optvalue = 1; + (void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, + (char *) &optvalue, sizeof(optvalue)); + } + + /* + * Bind to the specified port. * * Bind should not be affected by the socket having already been * set into nonblocking mode. If there is trouble, this is one -- cgit v0.12