这是保存线程服务器/客户端的最佳方式吗? [英] Is this the best way to hold a threaded server/client?
本文介绍了这是保存线程服务器/客户端的最佳方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello codeproject,
我目前正在尝试创建一个线程化的TCP客户端和服务器。我想知道是否有人对此有任何建议或改进,因为我在处理套接字连接方面不是很有经验。这是我的尝试:
Hello codeproject,
I am currently attempting to create a threaded TCP client and server. And I am wondering if anyone has any suggestions or improvements for this, as I am not so experienced in handling socket connections. Here is my attempt:
public class Server
{
#region -- Variables
private TcpListener tcpListener;
private Thread listenThread;
public List<TcpClient> Connections = new List<TcpClient>();
#endregion
public Server(int Port)
{
// - Create the variables.
this.tcpListener = new TcpListener(IPAddress.Any, Port);
this.listenThread = new Thread(new ThreadStart(SearchClient));
// -- Start the Thread.
this.listenThread.Start();
}
public void SearchClient()
{
// -- Start the Listener.
this.tcpListener.Start();
// -- Loop.
while (true)
{
// -- Blocks until there is an incoming connection.
try
{
TcpClient Client = this.tcpListener.AcceptTcpClient();
// -- Create a new Thread to handle the connection.
Thread ClientThread = new Thread(new ParameterizedThreadStart(HandleClient));
// -- Start the Thread.
ClientThread.Start(Client);
}
catch { break; }
}
}
public void HandleClient(object Client)
{
// -- Client.
TcpClient TcpClient = (TcpClient)Client;
// -- Add the client to the list.
Connections.Add(TcpClient);
// -- Stream.
NetworkStream ClientStream = TcpClient.GetStream();
// -- Buffer.
byte[] Buffer = new byte[4096];
// -- To-use later.
int bytesRead;
// -- Loop until asked to stop.
while (true)
{
// -- Initialize the integer.
bytesRead = 0;
// -- Attempt to read.
try
{
// -- Blocks until the client sends a message.
bytesRead = ClientStream.Read(Buffer, 0, Buffer.Length);
}
catch { break; /* Socket Exception*/ }
// -- Did the client disconnect?
if (bytesRead == 0)
{
break;
}
// -- Handle buffer here.
}
// -- Close the client.
TcpClient.Close();
// -- Remove the client from the list.
Connections.Remove(TcpClient);
}
public void SendBuffer(TcpClient TcpClient, byte[] Buffer)
{
TcpClient.GetStream().Write(Buffer, 0, Buffer.Length);
}
public void StopServer()
{
foreach (TcpClient Client in Connections)
{
// -- Close the client.
Client.Close();
}
tcpListener.Stop();
}
}
public class Client
{
#region -- Variables
private TcpClient tcpClient;
private Thread listenThread;
public bool GotConnection { get; private set; }
public bool AllowedRead { get; set; }
private string _IP;
private int _Port;
#endregion
public Client(string IP, int Port)
{
// -- Set the variables.
_IP = IP;
_Port = Port;
// - Create the variables.
this.tcpClient = new TcpClient();
this.listenThread = new Thread(new ThreadStart(HandleServer));
// -- Start the Thread.
this.listenThread.Start();
}
public void HandleServer()
{
// -- Stream.
NetworkStream ClientStream = tcpClient.GetStream();
// -- Buffer.
byte[] Buffer = new byte[4096];
// -- To-use later.
int bytesRead;
// -- Connection.
GotConnection = true;
// -- Loop until asked to stop.
while (true)
{
// -- Allowed Read.
if (!AllowedRead)
break;
// -- Initialize the integer.
bytesRead = 0;
// -- Attempt to read.
try
{
// -- Blocks until the client sends a message.
bytesRead = ClientStream.Read(Buffer, 0, Buffer.Length);
}
catch { break; /* Socket Exception*/ }
// -- Did the client disconnect?
if (bytesRead == 0)
{
break;
}
// -- Handle buffer here.
}
// -- Connection.
GotConnection = false;
// -- Close the client.
tcpClient.Close();
}
public void SendBuffer(byte[] Buffer)
{
if (GotConnection)
tcpClient.GetStream().Write(Buffer, 0, Buffer.Length);
}
public void StopClient()
{
if (tcpClient.Connected)
tcpClient.GetStream().Close();
tcpClient.Close();
AllowedRead = false;
}
}
推荐答案
Client.Close
,添加Client.Dispose
tcpClient.Close
之后,添加tcpClient.Dispose
参见 TcpClient.Dispose方法 [ ^ ]
AfterClient.Close
, addClient.Dispose
AftertcpClient.Close
, addtcpClient.Dispose
See TcpClient.Dispose Method [^]
这篇关于这是保存线程服务器/客户端的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文