Socket Server in Windows Service

可紊 提交于 2019-12-25 13:16:15

问题


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);
        }

Here is DebugManager

public class DebugManager : BaseDebug
    {
        private AsyncServer s;

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

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

       }

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?


回答1:


Already found out the problem. It was

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());
            }
        }

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.



来源:https://stackoverflow.com/questions/15570309/socket-server-in-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!