private void BeginReceive(NetUserToken ut) { try { ut.socket.BeginReceive(ut.buffer, 0, ut.buffer.Length, SocketFlags.None, EndReceive, ut); } catch (Exception e) { print(e.Message); } }
public void Start() { server.Listen(maxClient); print("Server OK!"); pools = new Stack <NetUserToken>(); for (int i = 0; i < maxClient; ++i) { NetUserToken ut = new NetUserToken(); pools.Push(ut); } server.BeginAccept(AsyncAceept, null); }
private void EndReceive(IAsyncResult rs) { try { NetUserToken ut = rs.AsyncState as NetUserToken; int len = ut.socket.EndReceive(rs); if (len > 0) { byte[] data = new byte[len]; Buffer.BlockCopy(ut.buffer, 0, data, 0, len); ut.Receive(data); BeginReceive(ut); } } catch (Exception e) { print(e.Message); } }
private void AsyncAceept(IAsyncResult rs) { try { Socket client = server.EndAccept(rs); NetUserToken ut = pools.Pop(); ut.socket = client; BeginReceive(ut); utList.Add(ut); server.BeginAccept(AsyncAceept, null); } catch (Exception e) { print(e.Message); } }