C# 控制台线程出错
我一直在编写我的一个应用程序。我需要它来打开5个不同的TcpListener和5个异步侦听器。我的服务器代码如下:C# 控制台线程出错,c#,multithreading,console,C#,Multithreading,Console,我一直在编写我的一个应用程序。我需要它来打开5个不同的TcpListener和5个异步侦听器。我的服务器代码如下: private static void StartServer(int number) { while (true) { var listener = new TcpListener(WebVars.LocalIp, WebVars.ServerPort + number); listener.Start(); Con
private static void StartServer(int number)
{
while (true)
{
var listener = new TcpListener(WebVars.LocalIp, WebVars.ServerPort + number);
listener.Start();
Console.WriteLine("Server started on port {0}", WebVars.ServerPort + number);
var client = listener.AcceptTcpClient();
Console.WriteLine("** Client connected !");
var netStream = client.GetStream();
byte[] bytes = new byte[client.ReceiveBufferSize];
netStream.Read(bytes, 0, client.ReceiveBufferSize);
string query = Encoding.ASCII.GetString(bytes);
// example query: IP-command-subject-year (last two only if command is "get")
var command = query.Split('-');
Console.WriteLine("Connected client from IP {0}\nCommand: {1}", command[0], command[1]);
if (command[1] == "query") // if the client wants a list of the available subjects
{
var docToSend = XDocument.Load("BagrutSubjects.xml");
byte[] bytesToSend = Encoding.ASCII.GetBytes(docToSend.ToString());
netStream.Write(bytesToSend, 0, bytesToSend.Length); // write the xml in string form
netStream.Close();
client.Close();
continue;
}
if (command[1] == "get")
{
// example of year: 2012a, 2012b (a = summer, b = winter)
string subject = command[2], year = command[3];
if (File.Exists(Path.Combine("subjects", subject, year, "subject.pdf")))
{
// if the pdf exists
}
}
listener.Stop();
}
}
if (Variables.IsClient)
{
Console.WriteLine(" -- Bagrut Client -- ");
Console.Write("Enter IP of server to connect to: ");
// ReSharper disable once AssignNullToNotNullAttribute
var ip = IPAddress.Parse(Console.ReadLine());
var client = new TcpClient();
client.Connect(ip, 1002); // the third server.
Console.WriteLine("** Connected to server!");
var bytes = Encoding.ASCII.GetBytes("127.0.0.1-query");
var stream = client.GetStream();
stream.Write(bytes, 0, bytes.Length);
stream.Read(bytes, 0, bytes.Length);
Console.WriteLine("Recieved {0}", Encoding.ASCII.GetString(bytes));
}
鉴于客户机代码如下:
private static void StartServer(int number)
{
while (true)
{
var listener = new TcpListener(WebVars.LocalIp, WebVars.ServerPort + number);
listener.Start();
Console.WriteLine("Server started on port {0}", WebVars.ServerPort + number);
var client = listener.AcceptTcpClient();
Console.WriteLine("** Client connected !");
var netStream = client.GetStream();
byte[] bytes = new byte[client.ReceiveBufferSize];
netStream.Read(bytes, 0, client.ReceiveBufferSize);
string query = Encoding.ASCII.GetString(bytes);
// example query: IP-command-subject-year (last two only if command is "get")
var command = query.Split('-');
Console.WriteLine("Connected client from IP {0}\nCommand: {1}", command[0], command[1]);
if (command[1] == "query") // if the client wants a list of the available subjects
{
var docToSend = XDocument.Load("BagrutSubjects.xml");
byte[] bytesToSend = Encoding.ASCII.GetBytes(docToSend.ToString());
netStream.Write(bytesToSend, 0, bytesToSend.Length); // write the xml in string form
netStream.Close();
client.Close();
continue;
}
if (command[1] == "get")
{
// example of year: 2012a, 2012b (a = summer, b = winter)
string subject = command[2], year = command[3];
if (File.Exists(Path.Combine("subjects", subject, year, "subject.pdf")))
{
// if the pdf exists
}
}
listener.Stop();
}
}
if (Variables.IsClient)
{
Console.WriteLine(" -- Bagrut Client -- ");
Console.Write("Enter IP of server to connect to: ");
// ReSharper disable once AssignNullToNotNullAttribute
var ip = IPAddress.Parse(Console.ReadLine());
var client = new TcpClient();
client.Connect(ip, 1002); // the third server.
Console.WriteLine("** Connected to server!");
var bytes = Encoding.ASCII.GetBytes("127.0.0.1-query");
var stream = client.GetStream();
stream.Write(bytes, 0, bytes.Length);
stream.Read(bytes, 0, bytes.Length);
Console.WriteLine("Recieved {0}", Encoding.ASCII.GetString(bytes));
}
我使用静态线程对象来启动它们,声明如下:
public static Thread serverThread1 = new Thread(() => StartServer(1));
public static Thread serverThread2 = new Thread(() => StartServer(2));
public static Thread serverThread3 = new Thread(() => StartServer(3));
public static Thread serverThread4 = new Thread(() => StartServer(4));
public static Thread serverThread5 = new Thread(() => StartServer(5));
当我启动输出如下所示的服务器时,一切都很顺利:
服务器决定出错并执行以下操作:
. 在我的代码中,没有任何地方说要在整个控制台中添加换行符。我不知道为什么会发生这种情况,服务器也没有响应。客户没有收到任何应该收到的东西。如何解决此问题?等待输入连接和服务器在端口1002上启动的消息来自何处?我需要它打开5个不同的TcpListeners为什么?这不是常见的情况。@Ilan321那么您认为web服务器如何仅通过侦听https的端口803或443来处理所有请求?@Ilan321那么这是一种常见的情况。与其问您真正的问题,不如问您认为解决方案是什么。您应该调用异步BeginAcceptCpcClient,或者更好的是,调用AcceptCpclientAsync方法,并在异步连接到来时处理异步连接,而不是使用五个单独的线程,每个线程都有一个同步TcpListener对AcceptCpcClient进行阻塞调用。这样,您可以拥有任意数量的并发客户端,或者您的计算机可以处理的并发客户端。有关简单示例,请参见。