2013-03-22 156 views
0

我有部署幾個窗口服務套接字服務器

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

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

這裏服務應用是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); 
     } 

     } 

而且套接字服務器本身

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

當我部署我的服務似乎我的套接字服務器沒有啓動,或者立即啓動然後關閉。我的設計中是否有任何問題,或者可能是im代碼?

回答

0

已經發現問題所在。它是

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並運行。我認爲它以前工作,但我的客戶端不能連接到服務器。