2012-03-28 49 views
0

通過大量的學習和研究,我編寫了一個服務器端程序。但是這個程序的問題在於它不接受多個客戶端,我也想知道如何將輸出發送回客戶端,而不是在服務器端顯示。有人可以幫我拿出代碼嗎?這是我試過至今 -我如何接受這個計劃的多個客戶?

類節目 {

private static Regex _regex = new Regex("not|http|console|application", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); 


    static void Main(string[] args) 
    { 
     { 

      TcpListener serversocket = new TcpListener(8888); 
      TcpClient clientsocket = default(TcpClient); 
      serversocket.Start(); 

      Console.WriteLine(">> Server Started"); 
      clientsocket = serversocket.AcceptTcpClient(); 
      Console.WriteLine("Accept Connection From Client"); 

      try 
      { 
       using (var reader = new StreamReader(clientsocket.GetStream())) 
       { 
        string line; 
        int lineNumber = 0; 
        while (null != (line = reader.ReadLine())) 
        { 
         lineNumber += 1; 
         foreach (Match match in _regex.Matches(line)) 
         { 

          Console.WriteLine("Line {0} matches {1}", lineNumber, match.Value); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.Error.WriteLine(ex.ToString()); 
      } 
      clientsocket.Close(); 
      serversocket.Stop(); 
      Console.WriteLine(" >> exit"); 
      Console.ReadLine(); 
     } 
    } 
    } 
+0

您需要爲客戶端產生一個線程。 – leppie 2012-03-28 07:43:02

+0

@leppie你能幫我一下嗎?我仍然是一個新手.. – 3692 2012-03-28 07:46:18

回答

0

你可以將其更改爲這樣的事情。

static void Main(string[] args) 
{ 
    TcpListener serversocket = new TcpListener(8888); 
    TcpClient clientsocket = default(TcpClient); 
    serversocket.Start(); 

    Console.WriteLine(">> Server Started"); 
    while(true) 
    { 
     clientsocket = serversocket.AcceptTcpClient(); 
     Console.WriteLine("Accept Connection From Client"); 
     LineMatcher lm = new LineMatcher(clientsocket); 
     Thread thread = new Thread(new ThreadStart(lm.Run)); 
     thread.Start(); 
     Console.WriteLine("Client connected"); 
    } 

    serversocket.Stop(); 
    Console.WriteLine(" >> exit"); 
    Console.ReadLine(); 
} 

再有這種獨立的類處理linematching

public class LineMatcher{ 
    private static Regex _regex = new Regex("not|http|console|application", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); 

    private TcpClient _client; 

    public LineMatcher(TcpClient client) 
    { 
     _client = client; 
    } 

    public void Run() 
    { 
     try 
     { 
      using (var reader = new StreamReader(_client.GetStream())) 
      { 
       string line; 
       int lineNumber = 0; 
       while (null != (line = reader.ReadLine())) 
       { 
        lineNumber += 1; 
        foreach (Match match in _regex.Matches(line)) 
        { 

         Console.WriteLine("Line {0} matches {1}", lineNumber, match.Value); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.Error.WriteLine(ex.ToString()); 
     } 
     Console.WriteLine("Closing client"); 
     _client.Close(); 
    } 
} 

這純粹是一個概念證明,是沒有辦法穩定的代碼;)
請注意,這不管理子任何方式的線程。
還有沒有正確的方式關閉聆聽者,因爲(true)

+0

非常感謝幫助.. :)我如何即興創作呢? :) – 3692 2012-03-28 09:08:40

+0

如果我想檢查文件中的字符串,它不會發生..爲什麼?它會立即斷開連接。而不是在程序本身給出單詞,我想讓客戶端搜索它..我怎麼能這樣做? Colud你能幫我解決這個問題嗎? – 3692 2012-03-28 09:30:56

相關問題