2016-08-23 63 views
0

我試圖創建一個需要在後臺運行,並偵聽傳入通信窗口服務IP監聽器(一個正常和常規TCP監聽)TCP在Windows服務

我的代碼是:

private TcpListener server; 

public void startServer() 
    { 
     // EventLog.WriteEntry(source, "connected on: " + ipAddress.ToString() + " port: " + Service1.Port.ToString()); 

     server = new TcpListener(IPAddress.Parse("127.0.0.1"), Service1.Port); 

     server.Start(); 
     while (true) 
     { 
      var client = server.AcceptTcpClient(); 


      new Thread(work).Start(client); 

     } 

public void work(object client) 
    { 
     string msg = null; 
     var clientLocal = (TcpClient)client; 


      using (NetworkStream ns = clientLocal.GetStream()) 
      using (StreamReader sr = new StreamReader(ns)) 
      { 
      byte[] msgFullArray = new UTF8Encoding(true).GetBytes(msg); 
      fs.Write(msgFullArray, 0, msg.Length); 
      } 

現在,如果你不看的工作方法,就好象每當我開始我的服務凍結每當我試圖在我啓動它:

var client = server.AcceptTcpClient(); 

意味着我的服務永遠不會用蘇氨酸EAD或我的工作方法.. 我可以從以前的記錄看,它進入我的while循環,然後就超時服務

+0

不清楚什麼叫'startServer'。如果它是由你的'OnStart'方法直接調用的,那就是一個問題。正如你所看到的,'AcceptTcpClient'阻塞直到出現一個客戶端,但OnStart被調用的線程*不屬於你*,你不應該阻塞它。 –

+0

那麼你是正確的,我的onstart方法調用 「server.startServer();」 但我不知道我明白如何解決它呢? – Pilsneren

回答

1

在你OnStart方法,你必須實例化一個服務器類。

protected override void OnStart(string[] args) 
{ 
    // Create the Server Object ans Start it. 
    server = new TCPServer(); 
    server.StartServer(); 
} 

,其負責通過創建處理對服務器的連接的新Thread(因此它是一個無阻塞處理),它將由一個被監聽的每個插座

public void StartServer() 
{ 
    if (m_server!=null) 
    { 
    // Create a ArrayList for storing SocketListeners before 
    // starting the server. 
    m_socketListenersList = new ArrayList(); 

    // Start the Server and start the thread to listen client 
    // requests. 
    m_server.Start(); 
    m_serverThread = new Thread(new ThreadStart(ServerThreadStart)); 
    m_serverThread.Start(); 

    // Create a low priority thread that checks and deletes client 
    // SocktConnection objcts that are marked for deletion. 
    m_purgingThread = new Thread(new ThreadStart(PurgingThreadStart)); 
    m_purgingThread.Priority=ThreadPriority.Lowest; 
    m_purgingThread.Start(); 
    } 
} 

TCPListener

private void ServerThreadStart() 
{ 
    // Client Socket variable; 
    Socket clientSocket = null; 
    TCPSocketListener socketListener = null; 
    while(!m_stopServer) 
    { 
    try 
    { 
     // Wait for any client requests and if there is any 
     // request from any client accept it (Wait indefinitely). 
     clientSocket = m_server.AcceptSocket(); 

     // Create a SocketListener object for the client. 
     socketListener = new TCPSocketListener(clientSocket); 

     // Add the socket listener to an array list in a thread 
     // safe fashon. 
     //Monitor.Enter(m_socketListenersList); 
     lock(m_socketListenersList) 
     { 
     m_socketListenersList.Add(socketListener); 
     } 
     //Monitor.Exit(m_socketListenersList); 

     // Start a communicating with the client in a different 
     // thread. 
     socketListener.StartSocketListener(); 
    } 
    catch (SocketException se) 
    { 
     m_stopServer = true; 
    } 
    } 
} 

這裏是完整的project article

+0

謝謝你,我的好先生! – Pilsneren