Beispiel #1
0
        //jnjk
        //    lmkl

        private void StopServer_Click(object sender, EventArgs e)
        {
            serverIsRunning     = false;
            StartServer.Enabled = true;
            StopServer.Enabled  = false;
            StartServer.Focus();
            ServerLog.Clear();
        }
 public static void Clear()
 {
     ServerLog.Clear();
 }
Beispiel #3
0
 private void ServerListening()
 {
     try
     {
         Socket listener = new Socket(SocketType.Stream, ProtocolType.Tcp);
         listener.Bind(new IPEndPoint(IPAddress.Any, serverPort));
         listener.Listen(0);
         serverIsRunning = true;
         Thread serverProcessingThread = new Thread(ServerProcessing);
         serverProcessingThread.Start();
         while (serverIsRunning)
         {
             if (listener.Poll(0, SelectMode.SelectRead))
             {
                 Socket  socket = listener.Accept();
                 bool    authorizedConnection = false;
                 JObject response             = new JObject();
                 response.Add("result", "error");
                 string request = "";
                 //чтение запроса
                 if ((request = ReceiveRequest(socket)).Length > 0)
                 {
                     JObject data   = JObject.Parse(request);
                     string  action = data.GetValue("action").ToString();
                     //обработка запроса
                     if (action == "authorization")
                     {
                         //чтение и проверка пароля
                         string name     = data.GetValue("name").ToString();
                         string password = data.GetValue("password").ToString();
                         if (password.Equals(serverPassword))
                         {
                             //добавление клиента в очередь
                             Client client = null;
                             if ((client = clients.Find(item => item.Name == name)) == null)
                             {
                                 if (clients.Count < maximumClients)
                                 {
                                     response["result"] = "success";
                                     string text = string.Format("[{0}]: {1} connected", DateTime.Now.ToString("H:m:ss"), name);
                                     Invoke((MethodInvoker)(() =>
                                     {
                                         ServerLog.AppendText(text + Environment.NewLine);
                                     }));
                                     //уведомление других клиентов
                                     JObject notification = new JObject();
                                     notification.Add("action", "message");
                                     notification.Add("message", text);
                                     for (int j = 0; j < clients.Count; ++j)
                                     {
                                         SendRequest(clients[j].Socket, notification);
                                     }
                                     //добавление клиента в очередь
                                     clients.Add(new Client(DateTime.Now, socket, name));
                                     authorizedConnection = true;
                                 }
                                 else
                                 {
                                     response.Add("message", "Server is full!");
                                 }
                             }
                             else
                             {
                                 response.Add("message", "User with this name already exists!");
                             }
                         }
                         else
                         {
                             response.Add("message", "Invalid password!");
                         }
                     }
                     else
                     {
                         response.Add("message", "You are not authorized!");
                     }
                 }
                 //формирование и отправка ответа
                 SendRequest(socket, response);
                 //если поключенный клиент не авторизовался
                 if (!authorizedConnection)
                 {
                     socket.Close();
                 }
             }
             else
             {
                 Thread.Sleep(sleepTime);
             }
         }
         listener.Close();
         while (serverProcessingThread.IsAlive)
         {
             Thread.Sleep(sleepTime);
         }
     }
     catch (ThreadAbortException)
     {
     }
     catch (Exception exception)
     {
         Invoke((MethodInvoker)(() =>
         {
             StartServer.Enabled = true;
             StopServer.Enabled = false;
             ServerLog.Clear();
         }));
         MessageBox.Show(exception.Message);
     }
 }