Пример #1
0
    public static LocalConnectionStatus initServer(int port)
    {
        try{
            if (server != null)
            {
                server.Stop();
            }
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");
            server = new TcpListener(localAddr, port);
            server.Start();
        }
        catch (SocketException e) {
            if (e.ErrorCode == 10048)
            {
                return(LocalConnectionStatus.portIsBusy);
            }
            return(LocalConnectionStatus.otherPortError);
        }
        catch { return(LocalConnectionStatus.otherPortError); }

        client = new TcpRemoteClient(server, 1);
        return(LocalConnectionStatus.connecting);
    }
        void CallBack(IAsyncResult result)
        {
            TcpRemoteClient client = null;

            try
            {
                client = new TcpRemoteClient(mListener.EndAcceptTcpClient(result)); //Thread stuck until client connects
                Clients.Add(client);
                client.StartClient();
            }
            catch (Exception)
            {
                if (client != null)
                {
                    client.Disconnect();
                }
            }

            if (!_shuttingDown)
                mListener.BeginAcceptTcpClient(CallBack, null);
        }