private static Boolean TryConfigureBindAndStartListen(SOCKET serverSocket, TcpServerSettings settings) { // try disable use of the Nagle algorithm if requested if (settings.UseNagleAlgorithm == false) { var optionValue = -1; unsafe { var tryDisableNagle = WinsockInterop.setsockopt(serverSocket, WinsockInterop.IPPROTO_TCP, WinsockInterop.TCP_NODELAY, (Byte*) &optionValue, sizeof(Int32)); // check if attempt has succeed if (tryDisableNagle == WinsockInterop.SOCKET_ERROR) { return false; } } } // try enable faster operations on the loopback if requested if (settings.UseFastLoopback) { unsafe { UInt32 optionValue = 1; UInt32 dwBytes; var tryEnableFastLoopbackResult = WinsockInterop.WSAIoctl(serverSocket, WinsockInterop.SIO_LOOPBACK_FAST_PATH, &optionValue, sizeof(UInt32), null, 0, out dwBytes, IntPtr.Zero, IntPtr.Zero); // check if attempt has succeed if (tryEnableFastLoopbackResult == WinsockInterop.SOCKET_ERROR) { return false; } } } // try bind { // compose address var address = new IN_ADDR { s_addr = 0 }; // compose socket address var socketAddress = new SOCKADDR_IN { sin_family = WinsockInterop.AF_INET, sin_port = WinsockInterop.htons(settings.Port), sin_addr = address }; // try associate address with socket var tryBindResult = WinsockInterop.bind(serverSocket, ref socketAddress, SOCKADDR_IN.Size); if (tryBindResult == WinsockInterop.SOCKET_ERROR) { return false; } } // try start listen { var tryStartListen = WinsockInterop.listen(serverSocket, settings.AcceptBacklogLength); if (tryStartListen == WinsockInterop.SOCKET_ERROR) { return false; } } return true; }
public static extern Int32 bind([In] SOCKET s, [In] ref SOCKADDR_IN name, [In] Int32 namelen);