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;
}
}
}
何か案は?