C# 在项目中,如何设置;“客户”;控制台应用程序和a";服务器";控制台应用程序

C# 在项目中,如何设置;“客户”;控制台应用程序和a";服务器";控制台应用程序,c#,C#,好吧,这可能是用错了词或者用错了术语。我想知道如何在本地计算机中设置控制台应用程序,该应用程序将作为“服务器”,在该服务器上运行客户端窗口上发生的所有后台任务/事件?对于“服务器”,我只有一个控制台应用程序,最多有四个“客户端”控制台应用程序 每一个都可以做不同的事情。“服务器”应用程序只需执行所有的计算和功能,客户端只需以良好的格式显示我希望它们从服务器的结果中显示的内容 此外,我不知道如何在C类型的项目中设置它。对于完整的教程,我建议阅读 对于某些示例代码,以下客户机/服务器控制台应用程序是

好吧,这可能是用错了词或者用错了术语。我想知道如何在本地计算机中设置控制台应用程序,该应用程序将作为“服务器”,在该服务器上运行客户端窗口上发生的所有后台任务/事件?对于“服务器”,我只有一个控制台应用程序,最多有四个“客户端”控制台应用程序

每一个都可以做不同的事情。“服务器”应用程序只需执行所有的计算和功能,客户端只需以良好的格式显示我希望它们从服务器的结果中显示的内容


此外,我不知道如何在C类型的项目中设置它。

对于完整的教程,我建议阅读

对于某些示例代码,以下客户机/服务器控制台应用程序是从MSDN中提取的

:

使用系统;
Net系统;
使用System.Net.Sockets;
使用系统文本;
公共类SynchronousSocketClient{
公共静态void StartClient(){
//输入数据的数据缓冲区。
字节[]字节=新字节[1024];
//连接到远程设备。
试一试{
//为套接字建立远程端点。
//本例使用本地计算机上的端口11000。
IPHostEntry ipHostInfo=Dns.Resolve(Dns.GetHostName())
IPAddress IPAddress=ipHostInfo.AddressList[0];
IPEndPoint remoteEP=新IPEndPoint(ipAddress,11000);
//创建TCP/IP套接字。
套接字发送器=新套接字(AddressFamily.InterNetwork,
流,ProtocolType.Tcp);
//将套接字连接到远程终结点。捕获所有错误。
试一试{
发送方连接(remoteEP);
WriteLine(“连接到{0}的套接字”,
sender.RemoteEndPoint.ToString());
//将数据字符串编码为字节数组。
byte[]msg=Encoding.ASCII.GetBytes(“这是一个测试”);
//通过套接字发送数据。
int bytesent=sender.Send(msg);
//从远程设备接收响应。
int bytesRec=sender.Receive(字节);
WriteLine(“回显测试={0}”,
Encoding.ASCII.GetString(字节,0,bytesRec));
//松开插座。
发送器关闭(SocketShutdown.Both);
sender.Close();
}捕集物(捕集物){
WriteLine(“ArgumentNullException:{0}”,ane.ToString());
}捕获(SocketException se){
WriteLine(“SocketException:{0}”,se.ToString());
}捕获(例外e){
WriteLine(“意外异常:{0}”,e.ToString());
}
}捕获(例外e){
Console.WriteLine(如ToString());
}
}
公共静态int Main(字符串[]args){
StartClient();
返回0;
}
}
:

使用系统;
Net系统;
使用System.Net.Sockets;
使用系统文本;
公共类SynchronousSocketListener{
//来自客户端的传入数据。
公共静态字符串数据=null;
公共静态侦听(){
//输入数据的数据缓冲区。
字节[]字节=新字节[1024];
//为套接字建立本地端点。
//Dns.GetHostName返回
//运行应用程序的主机。
IPHostEntry ipHostInfo=Dns.Resolve(Dns.GetHostName());
IPAddress IPAddress=ipHostInfo.AddressList[0];
IPEndPoint localEndPoint=新IPEndPoint(ipAddress,11000);
//创建TCP/IP套接字。
套接字侦听器=新套接字(AddressFamily.InterNetwork,
流,ProtocolType.Tcp);
//将套接字绑定到本地端点并
//侦听传入的连接。
试一试{
Bind(localEndPoint);
听(10);
//开始监听连接。
while(true){
Console.WriteLine(“等待连接…”);
//程序在等待传入连接时挂起。
套接字处理程序=listener.Accept();
数据=空;
//需要处理传入连接。
while(true){
字节=新字节[1024];
int bytesRec=handler.Receive(字节);
data+=Encoding.ASCII.GetString(字节,0,bytesRec);
if(data.IndexOf(“”>-1){
打破
}
}
//在控制台上显示数据。
WriteLine(“接收到的文本:{0}”,数据);
//将数据回显到客户端。
byte[]msg=Encoding.ASCII.GetBytes(数据);
handler.Send(msg);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}捕获(例外e){
Console.WriteLine(如ToString());
}
Console.WriteLine(“\n按ENTER继续…”);
Console.Read();
}
公共静态int Main(字符串[]args){
惊人的倾听();
返回0;
}
}
要进行设置,您需要在VisualStudio中创建一个新的解决方案,然后向该解决方案添加两个项目,一个用于客户端,一个用于服务器。两个项目完成后,可以复制并安装上面提供的代码。构建解决方案以为客户端和服务器生成
.exe
文件。找到
.exe
文件,首先运行服务器,然后再运行客户端。您应该可以在服务器和客户端控制台上看到一些输出
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketClient {

    public static void StartClient() {
        // Data buffer for incoming data.
        byte[] bytes = new byte[1024];

        // Connect to a remote device.
        try {
            // Establish the remote endpoint for the socket.
            // This example uses port 11000 on the local computer.
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);

            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork, 
                SocketType.Stream, ProtocolType.Tcp );

            // Connect the socket to the remote endpoint. Catch any errors.
            try {
                sender.Connect(remoteEP);

                Console.WriteLine("Socket connected to {0}",
                    sender.RemoteEndPoint.ToString());

                // Encode the data string into a byte array.
                byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");

                // Send the data through the socket.
                int bytesSent = sender.Send(msg);

                // Receive the response from the remote device.
                int bytesRec = sender.Receive(bytes);
                Console.WriteLine("Echoed test = {0}",
                    Encoding.ASCII.GetString(bytes,0,bytesRec));

                // Release the socket.
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();

            } catch (ArgumentNullException ane) {
                Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
            } catch (SocketException se) {
                Console.WriteLine("SocketException : {0}",se.ToString());
            } catch (Exception e) {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

        } catch (Exception e) {
            Console.WriteLine( e.ToString());
        }
    }

    public static int Main(String[] args) {
        StartClient();
        return 0;
    }
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SynchronousSocketListener {

    // Incoming data from the client.
    public static string data = null;

    public static void StartListening() {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        // Dns.GetHostName returns the name of the 
        // host running the application.
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );

        // Bind the socket to the local endpoint and 
        // listen for incoming connections.
        try {
            listener.Bind(localEndPoint);
            listener.Listen(10);

            // Start listening for connections.
            while (true) {
                Console.WriteLine("Waiting for a connection...");
                // Program is suspended while waiting for an incoming connection.
                Socket handler = listener.Accept();
                data = null;

                // An incoming connection needs to be processed.
                while (true) {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes,0,bytesRec);
                    if (data.IndexOf("<EOF>") > -1) {
                        break;
                    }
                }

                // Show the data on the console.
                Console.WriteLine( "Text received : {0}", data);

                // Echo the data back to the client.
                byte[] msg = Encoding.ASCII.GetBytes(data);

                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        } catch (Exception e) {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }

    public static int Main(String[] args) {
        StartListening();
        return 0;
    }
}