【发布时间】:2012-03-21 04:34:48
【问题描述】:
在服务器 -(多)客户端应用程序 [TCP] 中。我为我在服务器中接受的每个客户端使用 Socket、NetworkStream、StreamReader 和 StreamWriter .. 所以我有几个问题:
Thread thAccept = new Thread(acceptClient);
Thread thJob;
private void acceptClient()
{
while (true)
{
Socket client = server.Accept();
Console.WriteLine(client.RemoteEndPoint+" has connected");
StreamReader reader = new StreamReader(new NetworkStream(client));
//is it ok to create an instance NetworkStream like this or i will have to dispose it later?
thJob = new Thread(Job);
thJob.Start(reader);
}
}
private void Job(object o)
{
StreamReader reader = (Socket)o;
try
{
string cmd = null;
while ((cmd = reader.ReadLine()) != null)
{
//(BLA BLA..)
}
}
catch
{
Console.WriteLine("Disconnected by catch");
}
finally
{
Console.WriteLine("Finally Done.");
reader.Dispose();
}
}
该代码可以处理所有(需要处理的)对象吗?
【问题讨论】:
-
请在发布问题之前进行一些搜索。这个问题已经被问了十几次了。
-
@JohnSaunders 好吧,我还是 c# 的初学者 .. 我无法理解您链接的所有答案.. 但我认为它不能回答我的问题 .. 它有点不同..我部门你读到了最后..你只是读了这个主题!有一些对我很重要的子问题..如果您不想帮助,请..让其他人帮助我,不要关闭我的问题。
-
好吧,你几乎是对的。我阅读了标题和您问题的文本。我没有阅读代码。这不是重复的。
标签: c# stream client-server dispose idisposable