Beispiel #1
0
        //
        public bool MakeServer(string IP, int PORT)
        {
            isSV            = true;
            se              = new SocketExt(IP, PORT, true);
            se.ReceiveData  = Reload;
            se.ExitDraw     = ExitDraw;
            se.ClientStatus = (s) => ClientStatus?.Invoke(s);

            if (!se.MakeSV())
            {
                DetectSocket?.Invoke("Disconnect", "0.0.0.0", 0);
                return(false);
            }
            DetectSocket?.Invoke("Listening", IP, PORT);
            ClientStatus?.Invoke(0);
            return(true);
        }
        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);
        }