C#-将数据从Windows应用程序发送到控制台应用程序
在我的应用程序中,我使用Windows窗体创建了一个GUI,其中有一个带有值的ListBox和一个名为sendto的按钮.用户从列表框中选择并单击sendto按钮.单击此按钮后,应在控制台应用程序上显示从列表框选择的值.在此,以Windows形式开发的GUI充当服务器,而控制台应用程序充当客户端.如何将数据从Windows表单发送到C#中的控制台应用程序?我是C#的新手.
解决方法:
我在回答您的问题:使用C#进行套接字编程…但是一些不了解您的人关闭了您的问题…
我知道您可能是新程序员.但是我发现您善于提出问题,以使自己发展成为一个更好的程序员.我将投票给你! :D
请参阅以下代码,它将帮助您体验小型客户端服务器应用程序.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using System.Diagnostics;
using System.IO;
namespace TestCode
{
public class Program
{
public static StreamReader ServerReader;
public static StreamWriter ServerWriter;
static void Main(string[] args)
{
// here are all information to start your mini server
ProcessStartInfo startServerInformation = new ProcessStartInfo(@"c:\path\to\serverApp.exe");
// this value put the server process invisible. put it to false in while debuging to see what happen
startServerInformation.CreateNoWindow = true;
// this avoid you problem
startServerInformation.ErrorDialog = false;
// this tells that you whant to get all connections from the server
startServerInformation.RedirectStandardInput = true;
startServerInformation.RedirectStandardOutput = true;
// this tells that you whant to be able to use special caracter that are not define in ASCII like "é" or "?"
startServerInformation.StandardErrorEncoding = Encoding.UTF8;
startServerInformation.StandardOutputEncoding = Encoding.UTF8;
// start the server app here
Process serverProcess = Process.Start(startServerInformation);
// get the control of the output and input connection
Program.ServerReader = serverProcess.StandardOutput;
Program.ServerWriter = serverProcess.StandardInput;
// write information to the server
Program.ServerWriter.WriteLine("Hi server im the client app :D");
// wait the server responce
string serverResponce = Program.ServerReader.ReadLine();
// close the server application if needed
serverProcess.Kill();
}
}
}
请注意,在服务器应用程序中,您可以使用以下方法接收客户端信息:
string clientRequest = Console.ReadLine();
Console.WriteLine("Hi client i'm the server :) !");