2009-04-27 86 views
0

我正在使用visual studio來編程這個小型TcpServer。程序掛起,等待輸入,我永遠不能給

這真的很具體。服務器偵聽端口1234,位於IP 127.0.0.1 我們的老師給我們提供了一個程序,當您點擊「連接」時,該程序嘗試連接到該IP上的該端口。它適用於其他人,所以它一定是編碼錯誤。

當我單擊連接時,程序通過流發送單詞「GET」,我必須用一個已連接的IP地址列表來響應,然後是一個僅包含a的換行符。

當我拔下,該程序將單詞「REM」,我只是必須從我的列表中,如果刪除(這是一個泛型列表)

我有一個類TCPSERVER(我們不得不使我們自己) ,它有這個作爲主代碼:

this.tl = new TcpListener(IPAddress.Any, PORT); 
tl.Start(); 
while(true) 
{ 
    TcpClient tcl = tl.AcceptTcpClient();//here the server will wait forever untill someone connects, meaning the "new Thread" statement is never reached untill someone connects. 
    TcpHelper th = new TcpHelper(tcl,conf); 
    new Thread(new ThreadStart(th.Start)).Start();//should be multi-threaded, not sure if it is. 
    //t.Start(); 
} 

TcpHelper看起來像這樣(尋找註釋文本「這裏的問題」的usings內):

public class TcpHelper 
{ 
    private TcpClient tc; 
    private IPEndPoint ipe; 
    private string get; 
    private Configuration conf; 

    public TcpHelper(TcpClient tc, Configuration conf) 
    { 
     this.tc = tc; 
     this.conf = conf; 
    } 

    public void Start() 
    { 
     using (NetworkStream nws = this.tc.GetStream()) 
     { 
      using (StreamReader sr = new StreamReader(nws)) 
      { 
       using (StreamWriter sw = new StreamWriter(nws)) 
       { 
        this.ipe = (IPEndPoint)tc.Client.RemoteEndPoint; 
        this.conf.List.Add(this.ipe.Address); 
        bool conn = true; 

        while (conn) 
        { 
         this.get = sr.ReadLine();//here's the problem 
         switch (this.get) 
         { 
          case "GET": 
           foreach (IPAddress address in this.conf.Lijst) 
           { 
            sw.WriteLine(address.ToString()); 
           } 
           sw.WriteLine("."); 
           break; 

          case "REM": 
           this.conf.List.Remove(this.ipe.Address); 
           sw.WriteLine("OK."); 
           conn = false; 
           break; 

          default: 
           break; 
        } 
        } 
       } 
      } 
     } 
    } 

    #region Properties 
    public IPEndPoint Ipe 
    { 
     get 
     { 
      return this.ipe; 
     } 
    } 
    #endregion 
} 
+1

你可以使用語句ontop彼此堆棧;您不必像托架一樣嵌套托架。 – Will 2009-04-27 12:40:49

+0

我知道,我只是覺得這在視覺上更具吸引力。 – KdgDev 2009-04-27 12:55:33

回答

0

對不起,也許我不明白這個......你寫了這段代碼嗎?

this.tl = new TcpListener(IPAddress.Any, PORT); 
tl.Start(); 
while(true) 
{ 
    TcpClient tcl = tl.AcceptTcpClient(); 
    TcpHelper th = new TcpHelper(tcl,conf); 
    new Thread(new ThreadStart(th.Start)).Start(); 
    //t.Start(); 
} 

這將打擊任何電腦****。你無限循環,在每個循環中創建新線程。所以,如果每個循環創建一個新的線程,並且每個循環都需要一毫秒(讓我們說它非常慢!),在五秒鐘內就得到了5,000個線程。每個人都試圖在同一個端口上收聽。

嘗試使用單個線程。如果這是一個控制檯應用程序,使用Console.ReadLine()來阻止主線程,直到有人按下Enter鍵。


使用新信息... AcceptTcpClient塊,但不是創建新線程,而是應該在ThreadPool上排隊工作。

6

我的猜測是,你的問題是,你調用sr.ReadLine(),但是輸入不包含換行符,所以它在那裏等待換行符永遠不會到來。

您可能需要嘗試撥打StreamReader.Read 3次,然後才能對其執行命令字符串(GET/REM)。 (注意:3次是因爲所有命令都是三個字符)。

Read將返回整數,但在檢查它們不是-1(表示文件結束)後,可以將該整數轉換爲char。

+0

呃..?三次?聽起來像是一個可疑的想法給我,仔細闡述?你應該繼續閱讀,直到你得到你想要的東西,假設協議規定了一條新線,ReadLine似乎是一個完美契合。 – falstro 2009-04-27 12:38:36