Пример #1
0
 public void DisconnectClientWithIPAddr(String ipAddr)
 {
     try
     {
         if (_hashTable.ContainsKey(ipAddr))
         {
             TcpConnention conn = (TcpConnention)_hashTable[ipAddr];
             conn.client.Close();
         }
     }
     catch
     {
     }
 }
Пример #2
0
        private void LoopClients()
        {
            while (_isRunning)
            {
                // wait for client connection
                TcpClient newClient = _server.AcceptTcpClient();

                // client found.
                // create a thread to handle communication
                TcpConnention conn = new TcpConnention(newClient);
                Thread        t    = new Thread(new ParameterizedThreadStart(HandleClient));
                _hashTable[conn.ipAddr] = conn;
                t.Start(conn);
            }
        }
Пример #3
0
 public bool SendMessageTo(String ipAddr, String message)
 {
     try
     {
         if (_hashTable.ContainsKey(ipAddr))
         {
             TcpConnention conn = (TcpConnention)_hashTable[ipAddr];
             conn.sWriter.WriteLine(message);
             conn.sWriter.Flush();
             return(true);
         }
     }
     catch {
         return(false);
     }
     return(false);
 }
Пример #4
0
        private void HandleClient(object obj)
        {
            // retrieve client from parameter passed to thread
            TcpConnention conn   = (TcpConnention)obj;
            TcpClient     client = conn.client;

            // sets two streams
            // you could use the NetworkStream to read and write,
            // but there is no forcing flush, even when requested

            OnWelcomeMessage?.Invoke(conn);
            while (client.Connected && _isRunning)
            {
                Thread.Sleep(1000);
            }

            _hashTable.Remove(conn.ipAddr);
        }
Пример #5
0
 public bool SendBytesTo(String ipAddr, byte[] bytes)
 {
     try
     {
         if (_hashTable.ContainsKey(ipAddr))
         {
             TcpConnention conn = (TcpConnention)_hashTable[ipAddr];
             foreach (byte abyte in bytes)
             {
                 conn.client.GetStream().WriteByte(abyte);
             }
             conn.client.GetStream().Flush();
             return(true);
         }
     }
     catch
     {
         return(false);
     }
     return(false);
 }
Пример #6
0
        private void HandleClient(object obj)
        {
            // retrieve client from parameter passed to thread
            TcpConnention conn   = (TcpConnention)obj;
            TcpClient     client = conn.client;
            // sets two streams
            // you could use the NetworkStream to read and write,
            // but there is no forcing flush, even when requested

            String sData = null;

            OnWelcomeMessage?.Invoke(conn);
            while (client.Connected)
            {
                // reads from stream
                sData = conn.sReader.ReadLine();

                // shows content on the console.
                Console.WriteLine("Client > " + sData);
            }
            _hashTable.Remove(conn.ipAddr);
        }