Beispiel #1
0
        public void Connect(int localPort, int remotePort, IPAddress remoteAddress)
        {
            IPAddress localhost = Dns.GetHostAddresses("127.0.0.1")[0];
            listener = new TcpListener(localhost, localPort);
            BooleanEventArg arg;
            try
            {
                listener.Start();
            }
            catch (Exception e)
            {
                arg = new BooleanEventArg();
                arg.result = false;
                //localClientConnectedToRemoteServer.Set();
                connected(this, arg);
                return;
            }
            
            listener.BeginAcceptTcpClient(AcceptClient, listener);

            client = new TcpClient();

            Thread.Sleep(1000);
            //localClientConnectedToRemoteServer.Reset();
            client.BeginConnect(remoteAddress, remotePort, ServerConnected, client);

            //localClientConnectedToRemoteServer.WaitOne();
        }
Beispiel #2
0
        public void ServerConnected(IAsyncResult result)
        {
            TcpClient client = (TcpClient)result.AsyncState;
            BooleanEventArg arg;
            try
            {
                client.EndConnect(result);
            }
            catch (Exception e)
            {
                arg = new BooleanEventArg();
                arg.result = false;
                //localClientConnectedToRemoteServer.Set();
                connected(this, arg);
                return;
            }

            //localClientConnectedToRemoteServer.Set();

            arg = new BooleanEventArg();
            arg.result = true;
            connected(this, arg);
        }