TCP

2016-08-23 2 views
0

내가 백그라운드에서 실행하고 들어오는 트래픽을 수신해야하는 Windows 서비스를 만들려고 해요 Windows 서비스에서 IP 리스너 (정상 및 일반 TCP 리스너)TCP

내 코드는 다음과 같습니다

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

지금은 모두 내가 내 그것을 시작하려고 할 때마다 얼음장 내 서비스를 시작할 때마다 같은 작업 방법 보지 않는 경우 : 내 서비스가의 Thr를 사용하여 얻을 수 없습니다 의미

var client = server.AcceptTcpClient(); 

ead 또는 내 작업 방법 .. 이전 로깅에서 내 while 루프를 입력 한 다음 서비스가 시간 초과됨을 알 수 있습니다.

+0

'startServer'가 무엇인지 불분명합니다. 'OnStart' 메서드에 의해 직접 호출되는 경우 문제입니다. 이미 보았 듯이 클라이언트가 나타날 때까지'AcceptTcpClient'는 블로킹을합니다. 그러나 OnStart가 호출 된 쓰레드는 당신에게 속하지 않습니다. –

+0

내 시작 방법이 "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

관련 문제