示例#1
0
        public static void Connect(Peer Peer, string Host, int Port, int Ms, bool isSecure, bool isLocal, EventHandler <SocketAsyncEventArgs> Completed)
        {
            IPAddress IP = MemoryUtils.Host2IP(Host);

            if (IP == null)
            {
                MemoryUtils.Error("Could not resolve host " + Host);
                return;
            }

            SocketAsyncEventArgs e = new SocketAsyncEventArgs();

            e.RemoteEndPoint = new IPEndPoint(IP, Port);
            e.Completed     += Completed;

            Socket Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Socket.NoDelay             = true;
            Socket.Blocking            = false;
            Socket.ExclusiveAddressUse = true;
            Socket.LingerState         = new LingerOption(true, 0);

            e.UserToken = Create(Socket, Peer, isSecure, isLocal);

            if (!Socket.ConnectAsync(e))
            {
                Completed(null, e);
            }
        }
示例#2
0
        public void Listen(int Port, bool isSecure, bool isLocal)
        {
            this.isLocal  = isLocal;
            this.isSecure = isSecure;

            IPEndPoint localEndPoint = null;

            if (isLocal)
            {
                localEndPoint = new IPEndPoint(IPAddress.Loopback, Port);
            }
            else
            {
                localEndPoint = new IPEndPoint(IPAddress.Any, Port);
            }

            if (Socket != null)
            {
                MemoryUtils.Error("ss");
            }

            // Create a TCP/IP socket.
            Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Socket.NoDelay             = true;
            Socket.Blocking            = false;
            Socket.ExclusiveAddressUse = true;
            Socket.LingerState         = new LingerOption(true, 0);

            try
            {
                // Bind the socket to the local endpoint and listen for incoming connections.
                Socket.Bind(localEndPoint);
                Socket.Listen(100);

                Socket.BeginAccept(new AsyncCallback(AcceptCallback), Socket);
            }
            catch (Exception ex)
            {
                MemoryUtils.Error("Listen: " + ex.Message);
            }
        }