Windows 服务中的套接字服务器 [英] Socket Server in Windows Service

查看:38
本文介绍了Windows 服务中的套接字服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署多个 Windows 服务的服务应用程序

I have service application which deploys several windows services

static void Main()
        {
            DebugManager manager = new DebugManager();

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1(),
                new Service2(),
                new Service3(),
                            };
            ServiceBase.Run(ServicesToRun);
        }

这里是调试管理器

public class DebugManager : BaseDebug
    {
        private AsyncServer s;

        public DebugManager()
        {
            s = new AsyncServer(10000);
            s.Start();
        }

        public override void SendMessage(string message)
        {
            ts.SendMessage(message);
        }

       }

和 Socket Server 本身

And Socket Server itself

class AsyncServer
    {
        private Socket _serverSocket;
        private List<Socket> _clients;

        private int _port;
        byte[] buffer = new byte[255];

        public AsyncServer(int port) { _port = port; }

        public void Start()
        {
            try
            {
                _clients = new List<Socket>();
                SetupServerSocket();
                _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), _serverSocket);
            }
            catch(Exception ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
        }

        private void SetupServerSocket()
        {
            try
            {
                IPHostEntry localMachineInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPEndPoint myEndpoint = new IPEndPoint(localMachineInfo.AddressList[1], _port);
                _serverSocket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _serverSocket.Bind(myEndpoint);
                _serverSocket.Listen((int)SocketOptionName.MaxConnections);
            }
            catch (Exception ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
        }

        private void AcceptCallback(IAsyncResult result)
        {
            try
            {
                Socket s = (Socket)result.AsyncState;
                Socket socket = s.EndAccept(result);
                _clients.Add(socket);

                _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState);
            }
            catch (SocketException ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
            catch (Exception ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
        }

       public void SendMessage(string message)
        {
            try
            {
                byte[] bits = Encoding.UTF8.GetBytes(message);
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.SetBuffer(bits, 0, bits.Length);

                foreach (var client in _clients)
                {
                    if (!client.Connected)
                    {
                        _clients.Remove(client);
                        continue;
                    }

                    try
                    {
                        client.SendAsync(args);
                    }
                    catch (Exception ex)
                    {
                        EventLogManager.LogInformation(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
        }
    }

当我部署我的服务时,我的套接字服务器似乎没有启动或启动然后立即关闭.我的设计有什么问题或者可能是我的代码?

When I deploy my service it seems that my socket server is not starting or is started and then closed immediatly. Is there any problem in my design or may be im code?

推荐答案

已经发现问题.这是

private void SetupServerSocket()
        {
            try
            {
                IPHostEntry localMachineInfo = Dns.GetHostEntry(Dns.GetHostName());
                //Here is problem 
                IPEndPoint myEndpoint = new IPEndPoint(localMachineInfo.AddressList[1], _port);
                _serverSocket = new Socket(myEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _serverSocket.Bind(myEndpoint);
                _serverSocket.Listen((int)SocketOptionName.MaxConnections);
            }
            catch (Exception ex)
            {
                EventLogManager.LogInformation(ex.ToString());
            }
        }

返回了几个地址,[1] 是 IPV6.将其更改为 InterNetwork 并且它起作用了.我认为它以前有效,但我的客户端无法连接到服务器.

Several adresses are returned and [1] was IPV6. Changed it to InterNetwork and it worked. I think it worked before but my client couldn`t connect to server.

这篇关于Windows 服务中的套接字服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆