3

私のアプリケーションでは、値を持つ 1 つの ListBox と sendto という名前の 1 つのボタンがある Windows フォームを使用して GUI を作成しました。ユーザーは ListBox から選択し、sendto ボタンをクリックします。このボタンをクリックすると、ListBox から選択した値がコンソール アプリケーションに表示されます。ここでは、Windows 形式で開発された GUI がサーバーとして機能し、コンソール アプリケーションがクライアントとして機能します。Windows フォームから C# のコンソール アプリケーションにデータを送信するにはどうすればよいですか? 私はC#が初めてです。

4

2 に答える 2

2

私はあなたの質問に答えていました: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 :) !");
于 2012-08-28T05:54:11.103 に答える
1

パイプを使用できます。ローカル通信またはネットワーク通信のMSDN記事については、 MSDNの記事をご覧ください。

于 2012-08-27T09:34:14.743 に答える