public SOCKET Bind( IEndpointConfiguration configuration )
        {
            SOCKET socket = INVALID_SOCKET;
            "Binding to endpoint {0}:{1}"
                .ToDebug<ISocketServer>( configuration.BindTo ?? "0.0.0.0", configuration.Port );

            try
            {
                WSAPROTOCOL_INFO info = new WSAPROTOCOL_INFO();

                Native.WSASocket(
                        (int) AddressFamily.InterNetworkv4,
                        (int) SocketType.Stream,
                        (int) ProtocolType.Tcp,
                        out info,
                        0,
                        (int) SocketFlags.Overlapped
                    );

                var address = Native.inet_addr( configuration.AnyInterface
                                  ? "0.0.0.0"
                                  : configuration.BindTo );
                var port = Native.htons( (ushort) configuration.Port );
                sockaddr_in socket_address = new sockaddr_in();
                socket_address.sin_addr.S_addr = address;
                socket_address.sin_port = port;
                socket_address.sin_family = (short) AddressFamily.InterNetworkv4;

                Native.bind( socket, &socket_address, sizeof ( sockaddr_in ) );
                Native.listen( socket, 10000 );
                return socket;
            }
            catch (Exception e)
            {
                "Binding to endpoint {0}:{1} FAILED."
                    .ToDebug<ISocketServer>( configuration.BindTo ?? "0.0.0.0", configuration.Port );
            }
            return socket;
        }
Example #2
0
 public static extern int WSASocket( int addressFamily, int socketType, int protocol, out WSAPROTOCOL_INFO info, int group, int flags );