套接字侦听器只响应一次

时间:2017-02-13 14:46:34

标签: c# .net sockets

我的套接字侦听端口,但只是第一次对消息作出反应,而且不会更多。 怎么了?任何建议如何解决?因为我已经不知道该怎么做了。也许我听端口的方式不合适?

public partial class Listener
{
    private void InitListener()
    {
        Logger.InitializeLogger();
        var listenSocket = new Socket(AddressFamily.InterNetwork,
                                         SocketType.Stream,
                                         ProtocolType.Tcp);
        var ipHostInfo = Dns.Resolve(Dns.GetHostName());
        var ipAddress = ipHostInfo.AddressList[0];
        var endPoint = new IPEndPoint(ipAddress, Settings.Default.Port);
        listenSocket.Bind(endPoint);
        listenSocket.Listen(Settings.Default.listenerMaxConnection);
        Logger.EventLogItem.WriteEntry(
            $"Listen to {ipAddress}:{Settings.Default.Port}");


        Logger.EventLogItem.WriteEntry("Connection Recieved");

        Thread thread = new Thread(o => Listen(listenSocket));
        thread.Start();

    }

    public void Listen(Socket socket)
    {
        var directory = @"C:\\Users\\*****\\Desktop\\bat files";
        var handler = socket.Accept();
        while (true)
        {
            try
            {
                while (handler.Connected)
                {
                    byte[] x = new byte[100000];
                    handler.Receive(x);
                    var command = new string(Encoding.UTF8.GetChars(x.Where(t => t != 0).ToArray()));
                    if (string.IsNullOrEmpty(command))
                    {
                        Logger.EventLogItem.WriteEntry("Warning: Empty string");
                        break;
                    }
                    Logger.EventLogItem.WriteEntry(command);
                    Console.WriteLine(command);
                    switch (command)
                    {
                        case "run":
                            var myFiles = Directory.GetFiles(directory, "*.bat");
                            foreach (var bat in myFiles)
                            {
                                Process.Start(bat);
                            }
                            Logger.EventLogItem.WriteEntry("By command: " + command +
                                                           " was executed an appropriate .bat file.");
                            break;
                        case "getThreads":
                            Logger.EventLogItem.WriteEntry("By command: " + command +
                                                           " was executed an appropriate .bat file.");
                            break;
                        case "stop":
                            Logger.EventLogItem.WriteEntry("By command: " + command +
                                                           " was executed an appropriate .bat file.");
                            break;
                        case "start":
                            Logger.EventLogItem.WriteEntry("By command: " + command +
                                                           " was executed an appropriate .bat file.");
                            break;
                        case "close":
                            Logger.EventLogItem.WriteEntry("By command: " + command +
                                                           " was executed an appropriate .bat file.");
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.EventLogItem.WriteEntry(
                    $"Exception happened in Listener: {ex.Message} - {ex.StackTrace}");
            }
        }
    }
    }

public partial class Listener
{
    public static Listener Handler { get; } = new Listener();

    public Listener()
    {
        var thread = new Thread(InitListener);
        thread.Start();
    }
}

这是我的主要

 Listener listener = Listener.Handler;

1 个答案:

答案 0 :(得分:2)

你的问题在于:

var handler = socket.Accept();
while (true)
{
    try
    {
        while (handler.Connected)

由于您的代码永远不会离开while循环 - 它永远不会接受下一个连接。