.net Windows服务中的套接字服务器

.net Windows服务中的套接字服务器,.net,sockets,windows-services,.net,Sockets,Windows Services,我有一个服务应用程序,它部署了几个windows服务 static void Main() { DebugManager manager = new DebugManager(); ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1(),

我有一个服务应用程序,它部署了几个windows服务

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

       }
和套接字服务器本身

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());
            }
        }
    }
类异步服务器
{
专用套接字_serverSocket;
私人名单客户;
私人国际港口;
字节[]缓冲区=新字节[255];
公共异步服务器(int端口){u端口=端口;}
公开作废开始()
{
尝试
{
_clients=新列表();
SetupServerSocket();
_beginacept(新的异步回调(AcceptCallback),\u serverSocket);
}
捕获(例外情况除外)
{
EventLogManager.LogInformation(例如ToString());
}
}
私有void SetupServerSocket()
{
尝试
{
IPHostEntry localMachineInfo=Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint myEndpoint=新的IPEndPoint(localMachineInfo.AddressList[1],\u端口);
_serverSocket=新套接字(myEndpoint.Address.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
_绑定(myEndpoint);
_侦听((int)SocketOptionName.MaxConnections);
}
捕获(例外情况除外)
{
EventLogManager.LogInformation(例如ToString());
}
}
私有void AcceptCallback(IAsyncResult结果)
{
尝试
{
套接字s=(套接字)result.AsyncState;
套接字套接字=s.EndAccept(结果);
_clients.Add(socket);
_serverSocket.BeginAccept(新的AsyncCallback(AcceptCallback),result.AsyncState);
}
捕获(SocketException例外)
{
EventLogManager.LogInformation(例如ToString());
}
捕获(例外情况除外)
{
EventLogManager.LogInformation(例如ToString());
}
}
公共无效发送消息(字符串消息)
{
尝试
{
字节[]位=Encoding.UTF8.GetBytes(消息);
SocketAsyncEventArgs args=新的SocketAsyncEventArgs();
参数SetBuffer(位、0、位、长度);
foreach(var客户端在_客户端中)
{
如果(!client.Connected)
{
_客户。删除(客户);
继续;
}
尝试
{
client.sendaync(args);
}
捕获(例外情况除外)
{
EventLogManager.LogInformation(例如ToString());
}
}
}
捕获(例外情况除外)
{
EventLogManager.LogInformation(例如ToString());
}
}
}

当我部署我的服务时,我的套接字服务器似乎没有启动,或者启动后立即关闭。我的设计中是否存在任何问题,或者可能是im代码?

已发现问题。是的

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。把它改成了互联网,它成功了。我想它以前工作过,但我的客户端无法连接到服务器