通過大量的學習和研究,我編寫了一個服務器端程序。但是這個程序的問題在於它不接受多個客戶端,我也想知道如何將輸出發送回客戶端,而不是在服務器端顯示。有人可以幫我拿出代碼嗎?這是我試過至今 -我如何接受這個計劃的多個客戶?
類節目 {
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();
}
}
}
您需要爲客戶端產生一個線程。 – leppie 2012-03-28 07:43:02
@leppie你能幫我一下嗎?我仍然是一個新手.. – 3692 2012-03-28 07:46:18