コード例 #1
0
ファイル: Socket.cs プロジェクト: vzolotov/WebSharp
        void Connect(string host, bool tcp)
        {
            if (IsConnected)
            {
                PostMessage("Already connected.");
                return;
            }

            if (tcp)
            {
                tcp_socket_ = PPBTCPSocket.Create(this);
                if (PPBTCPSocket.IsTCPSocket(tcp_socket_) == PPBool.False)
                {
                    PostMessage("Error creating TCPSocket.");
                    return;
                }
            }
            else
            {
                udp_socket_ = PPBUDPSocket.Create(this);
                if (PPBUDPSocket.IsUDPSocket(udp_socket_) == PPBool.False)
                {
                    PostMessage("Error creating UDPSocket.");
                    return;
                }
            }


            resolver_ = PPBHostResolver.Create(this);
            if (PPBHostResolver.IsHostResolver(resolver_) == PPBool.False)
            {
                PostMessage("Error creating HostResolver.");
                return;
            }

            ushort port     = 80;
            var    hostname = host;
            int    pos      = host.IndexOf(':');

            if (pos > 0)
            {
                hostname = host.Substring(0, pos);
                port     = ushort.Parse(host.Substring(pos + 1));
            }

            var hint = new PPHostResolverHint()
            {
                family = PPNetAddressFamily.Unspecified, flags = 0
            };

            PPBHostResolver.Resolve(resolver_,
                                    hostname,
                                    port,
                                    hint,
                                    new CompletionCallback(OnResolveCompletion));

            PostMessage("Resolving ...");
        }
コード例 #2
0
        void Start(short port)
        {
            listeningSocket = PPBTCPSocket.Create(instance);
            if (PPBTCPSocket.IsTCPSocket(listeningSocket) == PPBool.False)
            {
                instance.PostMessage("Error creating TCPSocket.");
                return;
            }

            instance.PostMessage($"Starting server on port: {port}");

            // Attempt to listen on all interfaces (0.0.0.0)
            // on the given port number.
            //var ipv4_addr = new PPNetAddressIPv4((ushort)IPAddress.HostToNetworkOrder(port));
            var addr = new NetAddress(instance, (ushort)IPAddress.HostToNetworkOrder(port));
            var rtn  = (PPError)PPBTCPSocket.Bind(listeningSocket, addr, new CompletionCallback(OnBindCompletion));

            if (rtn != PPError.OkCompletionpending)
            {
                instance.PostMessage("Error binding listening socket.");
                return;
            }
        }