Exemplo n.º 1
0
        public void ListenForClients(object serverSocket)
        {
            TcpListener tcpListener = (TcpListener)serverSocket;

            tcpListener.Start();
            PrintReceivedData("Server started Listening...");
            while (started)
            {
                try
                {
                    TcpClient client = tcpListener.AcceptTcpClient();
                    clientTable.Add(clientCount, client);
                    Thread clientListShow = new Thread(AddClientToList);
                    clientListShow.Start(clientCount);
                    LogServerForm.Message($"Client - ClientID_{clientCount} connected!");
                    PrintReceivedData($"Client - ClientID_{clientCount} connected!");
                    Thread clientThread = new Thread(HandleClients);
                    clientThread.Start(clientCount);
                    clientCount++;
                }
                catch (Exception ex)
                {
                    LogServerForm.Error(ex);
                }
            }
        }
Exemplo n.º 2
0
 private void btnServerStart_Click(object sender, EventArgs e)
 {
     started = !started;
     if (started)
     {
         txtMessageDisplay.Clear();
         ServerSocket = new TcpListener(IPAddress.Any, port);
         Thread serverListenThread = new Thread(ListenForClients);
         serverListenThread.Start(ServerSocket);
         ToggleStartButton();
     }
     else
     {
         foreach (var item in clientTable)
         {
             item.Value.GetStream().Close();
         }
         clientTable.Clear();    // remove all connections
         clientCount = 1;
         LogServerForm.Message("Server Stopped...");
         PrintReceivedData("Server Stopped...");
         ServerSocket.Stop();
         ToggleStartButton();
     }
 }
Exemplo n.º 3
0
        public void HandleClients(object obj)
        {
            try
            {
                int       clientID = (int)obj;
                TcpClient client;
                client = clientTable[clientID];

                while (started)
                {
                    try
                    {
                        NetworkStream stream          = client.GetStream();
                        byte[]        buffer          = new byte[1024];
                        int           bufferNoOfBytes = stream.Read(buffer, 0, buffer.Length);
                        if (bufferNoOfBytes == 0)
                        {
                            break;
                        }
                        string data = Encoding.ASCII.GetString(buffer, 0, bufferNoOfBytes);
                        Broadcast(data);
                        LogServerForm.Message(data);
                        PrintReceivedData(data);
                    }
                    catch (Exception ex)
                    {
                        LogServerForm.Error(ex);
                    }
                }
                LogServerForm.Message($"Client - ClientID_{clientID} disconnected!");
                PrintReceivedData($"Client - Client ID_{clientID} disconnected!");
                clientTable.Remove(clientID);
                ClientAppendToList(clientID.ToString());
                client.GetStream().Close();
                client.Close();
                client.Close();
            }
            catch (Exception ex)
            {
                LogServerForm.Error(ex);
            }
        }