private void SendData(IAsyncResult ar) { try { StateObject1 state = (StateObject1)ar.AsyncState; state.client.EndSend(ar); } catch { return; } }
private void ReceiveData(object obj) { StateObject1 state = obj as StateObject1; try { int lenght = -1; lenght = state.client.Receive(state.buffer); AccountTemplate template = new AccountTemplate(); template = Deserialize(state.buffer); int i = QueryTheDatabase(template); byte[] vs = new byte[1]; vs[0] = Convert.ToByte(i); state.client.BeginSend(vs, 0, 1, SocketFlags.None, new AsyncCallback(SendData), state); } catch { return; } }
private void AcceptClient(object objListener) { Socket listener = objListener as Socket; while (true) { try { Socket socketClient = listener.Accept(); StateObject1 stateObject = new StateObject1(); stateObject.client = socketClient; Task clientTask = new Task(new Action <object>(ReceiveData), stateObject); clientTask.Start(); } catch (Exception e) { break; } } }