我正在使用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
}
你可以使用語句ontop彼此堆棧;您不必像托架一樣嵌套托架。 – Will 2009-04-27 12:40:49
我知道,我只是覺得這在視覺上更具吸引力。 – KdgDev 2009-04-27 12:55:33