public bool MakeSV() { ID = -1; if (!sl.MakeServer()) { return(false); } clientList = new List <ClientStruct>(); ConnectThread = new Thread(p => { while (true) { ClientStruct cs = new ClientStruct(); cs.client = sl.MySocket.Accept(); cs.clientdata.ID = sttid++; clientList.Add(cs); ClientStatus?.Invoke(clientList.Count); try { Send(cs.client, cs.clientdata.ID.ToString()); cs.client.BeginReceive(cs.socketdata.Buffer, 0, SocketData.BufferLength, 0, new AsyncCallback(ReceiveCallbackSV), cs); } catch (Exception e) { //MessageBox.Show(e.ToString() + "\nClient bi xoa"); //client bi xoa cs.Destructor(); clientList.Remove(cs); ClientStatus?.Invoke(clientList.Count); } } }); ConnectThread.Start(); return(true); }
private void ReceiveCallbackSV(IAsyncResult ar) { ClientStruct cs = ar.AsyncState as ClientStruct; try { string gettingdata = UnicodeEncoding.ASCII.GetString(cs.socketdata.Buffer, 0, cs.client.EndReceive(ar)); ReceiveData?.Invoke(gettingdata); cs.client.BeginReceive(cs.socketdata.Buffer, 0, SocketData.BufferLength, 0, new AsyncCallback(ReceiveCallbackSV), cs); } catch (Exception e) { //MessageBox.Show(e.ToString() + "\nClient bi xoa"); //client bi xoa cs.Destructor(); clientList.Remove(cs); ExitDraw?.Invoke(cs.clientdata.ID); ClientStatus?.Invoke(clientList.Count); } }