【问题标题】:How can I listen between TCP sockets and stdinput in C#如何在 C# 中侦听 TCP 套接字和 stdinput
【发布时间】:2020-11-22 16:12:15
【问题描述】:

如何在 TCP 套接字和从服务器端的 STDIN 接收的命令之间进行侦听(例如“退出”以关闭所有连接和服务器)。

我记得在 C 中我只需要设置 STDIN 套接字,但是如何为 STDIN 创建一个套接字并将这个套接字添加到数组列表中。

我想要这样的功能,我评论了选择STDIN套接字从键盘接收数据的部分。

using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SelectTcpSrvr
{
    public const int PORT = 9050;

    public static void Main()
    {
        ArrayList sockList = new ArrayList(2);
        ArrayList copyList = new ArrayList(2);
        Socket main = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        IPEndPoint iep = new IPEndPoint(IPAddress.Any, PORT);

        main.Bind(iep);
        main.Listen(2);
        sockList.Add(main);

        while (true)
        {
            copyList = new ArrayList(sockList);
            Console.WriteLine("Monitoring {0} sockets...", copyList.Count);
            Socket.Select(copyList, null, null, 1000000);

            foreach (Socket client in copyList)
            {
                if (client == main)
                {
                    Socket client1 = main.Accept();
                    IPEndPoint iep1 = (IPEndPoint)client1.RemoteEndPoint;
                    client1.Send(Encoding.ASCII.GetBytes("Welcome to the server"));
                    Console.WriteLine("Connected to {0}", iep1.ToString());
                    sockList.Add(client1);
                }

                //else if (I recive data from STDIN) {
                //    string command = data from SDTIN;
                //    switch (command)
                //    {
                //        // TODO
                //    }
                //}

                else
                {
                    byte[] data = new byte[1024];
                    int recv = client.Receive(data);
                    string stringData = Encoding.ASCII.GetString(data, 0, recv);
                    Console.WriteLine("Received: {0}", stringData);
                    if (recv == 0)
                    {
                        iep = (IPEndPoint)client.RemoteEndPoint;
                        Console.WriteLine("Client {0} disconnected.", iep.ToString());
                        client.Close();
                        sockList.Remove(client);
                        if (sockList.Count == 0)
                        {
                            Console.WriteLine("Last client disconnected, bye");
                            return;
                        }
                    }
                    else
                        client.Send(data, recv, SocketFlags.None);
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# sockets server


    【解决方案1】:

    很久以前我用c#开发了交互式CMD程序,你可以在this链接中找到它,在链接中你可以看到:

    ...
    var process = new Process()
    {
        StartInfo = new ProcessStartInfo()
        {
            FileName = "your process path.exe", //in code it was cmd.exe
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            ErrorDialog = false,
            CreateNoWindow = true,
        }
    };
    process.Start();
    var writer = process.StandardInput;
    var reader = process.StandardOutput;
    var error = process.StandardError;
    ...
    

    通过获取您的 SocketStream,您可以在您的进程中读取或写入,例如:

    using (var socketStream = new NetworkStream(main))
    {
        //here is your read/write 
    }
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2019-11-09
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多