0

我有一个用 c# 编写的客户端-服务器应用程序。我的服务器监视客户端发送给它的命令。我希望在进程完成后向客户端发送“完成”消息,我只是不知道该怎么做......

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Management;
using Microsoft.Win32;

namespace RM
{
    public partial class RMFORM : Form
    {
        private Socket clientSock;
        private string userName = "";
        private string testName = "";
        private string testNameString = "";
        private string targetPath = "";
        private bool isConnected = false;
        private TestForm testForm;
        private List<string> listOfFilesToCopy = new List<string>();
        private List<string> listOfPathToCopy = new List<string>();
        private RegistryKey registryKey;

        public RMFORM ()
        {
            InitializeComponent();
            userName = RemoteUtils.getConnectedUser();
            targetPath = RemoteUtils.targetPath + userName;
            testForm = new TestForm(RemoteUtils.remoteIpAddress, userName);
        }

        private void createNewCommand(string executable, string selectedPath, bool isDirectory)
        {
            string selectedOutDir = "";

            testForm.enableFinishedButton();
            testForm.setNumOfProcessesTest();
            testForm.ShowDialog();

            while (!testForm.getIsFinished())
            {
                if (testForm.getIsFormClosed())
                {
                    testForm.Hide();
                    return;
                }
            }

            testName = testForm.getTestName();
            testNameString = testForm.getSelectedTestType();
            selectedOutDir = targetPath + "\\" + testName;

            try
            {
                if (Directory.Exists(selectedOutDir))
                {
                    DialogResult dialogResult = MessageBox.Show("Test named " + testName + " already exists.\nDo You Wish To Continue?", "Warning!", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.No)
                    {
                        return;
                    }
                }
                else
                    Directory.CreateDirectory(selectedOutDir);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
                this.Close();
            }

            if (testNameString.CompareTo("Mek") == 0)
            {
                addSingleTest(executable, selectedPath, selectedOutDir, "mek", isDirectory);
            }
            if (testNameString.CompareTo("Mel") == 0)
            {
                addSingleTest(executable, selectedPath, selectedOutDir, "mel", isDirectory);
            }
            if (testNameString.CompareTo("Mem") == 0)
            {
                addSingleTest(executable, selectedPath, selectedOutDir, "mem", isDirectory);
            }


        }
        private void addSingleTest(string executable, string selectedPath, string outputDir, string testType, bool isDirectory)
        {
            string commandToRun = "";

            commandToRun = targetPath + RemoteUtils.batchRunPath + executable + testForm.getTestPerformance() + " " + selectedPath + " " + outputDir;
            if (!isDirectory)
            {
                commandToRun += "\\" + Path.GetFileNameWithoutExtension(selectedPath) + "." + testType + ".pos " + testType;
            }
            else
            {
                commandToRun += " " + testType + " " + testForm.getNumOfProcess();
            }

            removeOne.Enabled = true;
            removeAll.Enabled = true;
            sendBtn.Enabled = true;

            listORequestedCommands.Items.Add(commandToRun);
            listORequestedCommands.Refresh();
        }
        private bool searchForFiles(string parentDirectory)
        {
            bool found = false;

            if (Directory.GetFiles(parentDirectory, "*" + RemoteUtils.sequenceExtenssion).Length > 0)
                return true;

            try
            {
                foreach (string subDir in Directory.GetDirectories(parentDirectory))
                {
                    if (Directory.GetFiles(subDir, "*" + RemoteUtils.sequenceExtenssion).Length > 0)
                        return true;
                    found = searchForFiles(subDir);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
                this.Close();
            }

            return found;

        }
        private void connectBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!isConnected)
                {
                    registryKey = Registry.CurrentUser.CreateSubKey(RemoteUtils.registrySubKey);
                    clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    connectBtn.Text = "Disconnect From Server";
                    connectBtn.Refresh();
                    clientSock.Connect(RemoteUtils.remoteIpAddress, RemoteUtils.remotePort);
                    statusColor.BackColor = Color.Green;
                    statusColor.Refresh();
                    isConnected = true;
                    buttonAddDirectory.Enabled = true;
                    buttonAddFile.Enabled = true;
                    var backgroundWorker = new BackgroundWorker();
                    backgroundWorker.DoWork += (sender1, e1) => RemoteUtils.copyDllsToServer(targetPath);
                    backgroundWorker.RunWorkerAsync();
                }
                else
                {
                    registryKey.Close();
                    connectBtn.Text = "Connect To Server";
                    isConnected = false;
                    statusColor.BackColor = Color.Red;
                    buttonAddDirectory.Enabled = false;
                    buttonAddFile.Enabled = false;
                    clientSock.Close();
                    clientSock.Dispose();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
                this.Close();
            }

        }
        private void sendBtn_Click(object sender, EventArgs e)
        {
            try
            {
                string [] commandsInRegistry = registryKey.GetValueNames();

                for (int i = 0; i < commandsInRegistry.Length; i++)
                {
                    registryKey.DeleteValue(commandsInRegistry[i]);
                }

                for (int i = 0; i < listORequestedCommands.Items.Count; i++)
                {
                    clientSock.Send(Encoding.Default.GetBytes(listORequestedCommands.Items[i].ToString() + " <eom> "));
                    registryKey.SetValue("Command " + (i + 1).ToString(), listORequestedCommands.Items[i].ToString());
                }

                removeAll_Click(sender, e);
                sendBtn.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!");
                this.Close();
            }

        }
        private void buttonAddDirectory_Click(object sender, EventArgs e)
        {
            folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = "C:\\";

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedPath = folderBrowserDialog.SelectedPath;

                if (!searchForFiles(selectedPath))
                {
                    MessageBox.Show("The directory: " + selectedPath + " doesn't contain sequences.", "Error!");
                    return;
                }

                testForm.enableNumOfProcesses();
                createNewCommand(RemoteUtils.runBatchScript, selectedPath, true);
            }
        }
        private void buttonAddFile_Click(object sender, EventArgs e)
        {
            openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.Filter = "PMD files (*" + RemoteUtils.sequenceExtenssion + ")|*" + RemoteUtils.sequenceExtenssion + "|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFile = openFileDialog.FileName;
                if (Path.GetExtension(selectedFile).CompareTo(RemoteUtils.sequenceExtenssion) != 0)
                {
                    MessageBox.Show("The file: " + selectedFile + " is not a sequence file.", "Error!");
                    return;
                }
                createNewCommand(RemoteUtils.batchRunExe, selectedFile, false);
            }
        }
        private void removeOne_Click(object sender, EventArgs e)
        {
            int iRemoveIndex = listORequestedCommands.SelectedIndex;
            if (iRemoveIndex > -1)
            {
                listORequestedCommands.Items.RemoveAt(iRemoveIndex);
                listORequestedCommands.Refresh();
                if (listORequestedCommands.Items.Count == 0)
                {
                    removeOne.Enabled = false;
                    removeAll.Enabled = false;
                    sendBtn.Enabled = false;
                }
            }
        }
        private void removeAll_Click(object sender, EventArgs e)
        {
            listORequestedCommands.Items.Clear();
            listORequestedCommands.Refresh();
            buttonAddDirectory.Enabled = true;
            buttonAddFile.Enabled = true;
            removeOne.Enabled = false;
            removeAll.Enabled = false;
            sendBtn.Enabled = false;
        }
    }
}

有任何想法吗?

4

1 回答 1

0

你有没有看过

http://www.thecodeproject.com/csharp/ZaSocks5Proxy.asp

我们在我们的软件中使用基于此的代码,以便我们的“服务器”监视输入以将消息广播给连接到“服务器”的所有其他用户

本质上,每个工作站都监控传入的字符串。我们将字符串的第一个单词用作“命令”并进行相应处理。“完成”可能是这样的命令。

于 2013-02-06T14:41:42.690 回答