private static void receiveCallback(IAsyncResult ar) { TCPState state = (TCPState)ar.AsyncState; int readbytes = 0; try { readbytes = state.Stream.EndRead(ar); } catch { readbytes = 0; } if (readbytes == 0) { Console.WriteLine($"\r\nClient {state.ID} have already disconnect."); clients.Remove(state.ID); return; } byte[] buf = new byte[readbytes]; Array.Copy(state.Buffer, 0, buf, 0, readbytes); state.Stream.BeginRead(state.Buffer, 0, state.BufferSize, new AsyncCallback(receiveCallback), state); string cmd = Encoding.ASCII.GetString(buf); handle(cmd, state); }
private static void sendCallback(IAsyncResult ar) { TCPState state = (TCPState)ar.AsyncState; try { state.Stream.EndWrite(ar); } catch { } }
private static void connectCallback(IAsyncResult ar) { TcpListener listener = (TcpListener)ar.AsyncState; TcpClient client = listener.EndAcceptTcpClient(ar); listener.BeginAcceptTcpClient(new AsyncCallback(connectCallback), listener); if (client != null) { TCPState state = new TCPState(client); state.Stream.BeginRead(state.Buffer, 0, state.BufferSize, new AsyncCallback(receiveCallback), state); Console.WriteLine($"Client {state.ID} have already connect success,waiting for receive data."); } }
private static void handle(string cmd, TCPState state) { string key = state.ID; switch (cmd) { case "LON\r": if (!clients.ContainsKey(key)) { clients.Add(key, state); } break; case "LOFF\r": clients.Remove(key); break; } }