Пример #1
0
 /// <summary>
 /// Thread that listens for incomming connections.
 /// </summary>
 private void listenerThread()
 {
     listener.ExclusiveAddressUse = true;
     listener.Start();
     while (true)
     {
         try
         {
             TcpClient        client = listener.AcceptTcpClient();
             ConnectionWorker worker = new ConnectionWorker(id, client, this);
             worker.Start();
         }
         catch (Exception e) { Console.WriteLine("// {0}", e.Message); };
     }
 }
Пример #2
0
        /// <summary>
        /// Connects to the given port on the localhost.
        /// </summary>
        /// <param name="remoteId"></param>
        public void ConnectToPort(int remoteId)
        {
            TcpClient client = new TcpClient();

            client.ExclusiveAddressUse = true;
            for (int tries = 0; tries < 20 && !client.Connected; tries++)
            {
                client.Connect("localhost", remoteId);
                if (!client.Connected)
                {
                    Thread.Sleep(50);
                }
            }
            ConnectionWorker worker = new ConnectionWorker(id, client, this);

            worker.Start();
        }