Exemple #1
0
        public remoteServer(int port)
        {
            debug = new StreamWriter("server.log", true);

            SharedData.serverOutBuffer.Clear();
            SharedData.executeBuffer.Clear();

            debugLog("Listening to port " + port);

            TcpListener serverSocket = new TcpListener(IPAddress.Any, port);
            TcpClient clientSocket = default(TcpClient);
            Thread servermessages = new Thread(selfcast);
            int counter = 0;

            try
            {
                serverSocket.Start();
                debugLog("Socket created");
            }
            catch
            {
                MessageBox.Show("Port already in use!");
                SharedData.serverThreadRun = false;
                return;
            }
            counter = 0;

            servermessages.Start();

            while (SharedData.serverThreadRun)
            {
                if (serverSocket.Pending())
                {
                    counter += 1;
                    clientSocket = serverSocket.AcceptTcpClient();

                    byte[] bytesFrom = new byte[(int)clientSocket.ReceiveBufferSize];
                    string dataFromClient = null;

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                    Int32 msglen = dataFromClient.IndexOf("$");
                    if (msglen > 0)
                    {
                        debugLog("Client connected");
                        dataFromClient = dataFromClient.Substring(0, msglen);

                        string[] cmd = dataFromClient.Split(';');

                        // clean up duplicates
                        if (clientsList.ContainsKey(cmd[0]))
                            clientsList.Remove(cmd[0]);

                        clientsList.Add(cmd[0], clientSocket);
                        debugLog("Added client " + cmd[0]);

                        if (cmd[1] == SharedData.settings.RemoteControlServerPassword)
                        {
                            authorizedClients.Add(cmd[0]);
                            debugLog("Authorized client " + cmd[0]);
                        }

                        handleClient client = new handleClient();
                        client.startClient(clientSocket, cmd[0], clientsList);
                        debugLog("Client thread started for " + cmd[0]);
                    }
                    else
                    {
                        networkStream.Close();
                        debugLog("Invalid connection attempt");
                    }
                }
                Thread.Sleep(1000); // wait
            }

            serverSocket.Stop();
        }
Exemple #2
0
        public remoteServer(int port)
        {
            debug = new StreamWriter("server.log", true);

            SharedData.serverOutBuffer.Clear();
            SharedData.executeBuffer.Clear();

            debugLog("Listening to port " + port);

            TcpListener serverSocket   = new TcpListener(IPAddress.Any, port);
            TcpClient   clientSocket   = default(TcpClient);
            Thread      servermessages = new Thread(selfcast);
            int         counter        = 0;

            try
            {
                serverSocket.Start();
                debugLog("Socket created");
            }
            catch
            {
                MessageBox.Show("Port already in use!");
                SharedData.serverThreadRun = false;
                return;
            }
            counter = 0;

            servermessages.Start();

            while (SharedData.serverThreadRun)
            {
                if (serverSocket.Pending())
                {
                    counter     += 1;
                    clientSocket = serverSocket.AcceptTcpClient();

                    byte[] bytesFrom      = new byte[(int)clientSocket.ReceiveBufferSize];
                    string dataFromClient = null;

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);


                    Int32 msglen = dataFromClient.IndexOf("$");
                    if (msglen > 0)
                    {
                        debugLog("Client connected");
                        dataFromClient = dataFromClient.Substring(0, msglen);

                        string[] cmd = dataFromClient.Split(';');

                        // clean up duplicates
                        if (clientsList.ContainsKey(cmd[0]))
                        {
                            clientsList.Remove(cmd[0]);
                        }

                        clientsList.Add(cmd[0], clientSocket);
                        debugLog("Added client " + cmd[0]);

                        if (cmd[1] == SharedData.settings.RemoteControlServerPassword)
                        {
                            authorizedClients.Add(cmd[0]);
                            debugLog("Authorized client " + cmd[0]);
                        }

                        handleClient client = new handleClient();
                        client.startClient(clientSocket, cmd[0], clientsList);
                        debugLog("Client thread started for " + cmd[0]);
                    }
                    else
                    {
                        networkStream.Close();
                        debugLog("Invalid connection attempt");
                    }
                }
                Thread.Sleep(1000); // wait
            }

            serverSocket.Stop();
        }