public void DisconnectClientWithIPAddr(String ipAddr) { try { if (_hashTable.ContainsKey(ipAddr)) { TcpConnention conn = (TcpConnention)_hashTable[ipAddr]; conn.client.Close(); } } catch { } }
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); } }
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); }
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); }
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); }
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); }