与多个客户端TCP服务器 [英] TCP server with multiple Clients
问题描述
我工作的TCP服务器/客户端应用程序。
我的问题是:
我的服务器应用程序启动一个新的线程,并阻止它,直到连接被接受
该listenforClient方法
但我怎么能管理连接当多个客户端连接到我的服务器,他们要求不同的东西的同时如何管理客户端1获取信息的需求,并同客户端2。
它的多线程,因此多个客户端可以连接,但我怎么可以处理该请求。
我不想把一切都在1比的方法会是什么。
由于提前
私人无效serverstart()
{
this.tcplistener =新的TcpListener(IPAddress.Any,49151);
this.listenThread =新主题(新的ThreadStart(ListenForClients));
this.listenThread.Start();
} 私人无效ListenForClients()
{
this.tcplistener.Start(); 而(真)
{
//块,直到客户端已连接到服务器
TcpClient的客户= this.tcplistener.AcceptTcpClient();
//这里是:第一,发送hello客户端的消息
//
////////////////////////////////////////////////// / //创建一个线程来处理通信
//与连接的客户端
螺纹clientThread =新主题(新ParameterizedThreadStart(HandleClientComm)); clientThread.Start(客户端);
}
} 私人无效HandleClientComm(对象客户端)
{
的TcpClient的TcpClient =(TcpClient的)客户端;
的NetworkStream clientStream = tcpClient.GetStream(); 字节[]消息=新的字节[4096];
INT读取动作; 而(真)
{
读取动作= 0; 尝试
{
//阻塞,直到客户端发送一个消息
读取动作= clientStream.Read(消息,0,4096);
}
抓住
{
//套接字错误发生
打破;
} 如果(读取动作== 0)
{
//客户端已与服务器断开连接
打破;
} //消息已被成功地接收到
ASCIIEncoding EN codeR =新ASCIIEncoding(); bufferincmessage = EN coder.GetString(消息0,读取动作);
如果(System.Text.RegularEx pressions.Regex.IsMatch(bufferincmessage,Properties.Settings.Default.REQLogin,System.Text.RegularEx pressions.RegexOptions.IgnoreCase))
{
bufferincmessageresult = bufferincmessage.Split('^');
nickname_Cl = bufferincmessageresult [1];
password_Cl = bufferincmessageresult [2];
getuserdata_db();
登录(); 字节[]缓冲= EN coder.GetBytes(inlogmessage); clientStream.Write(缓冲液,0,buffer.Length);
clientStream.Flush();
}
}
}
您客户端将在不同的线程调度,所以他们不会相交。
你只需要添加类似您的信息将被处理DispatchMethod。
使用System.Text.RegularEx pressions;
...如果(Regex.IsMatch(bufferincmessage,Properties.Settings.Default.REQLogin,RegexOptions.IgnoreCase))
{
...
}
否则,如果(Regex.IsMatch(bufferincmessage,/ *你的一些的Command * / RegexOptions.IgnoreCase))
{
...
}
否则,如果(Regex.IsMatch(bufferincmessage,/ *你的一些的Command * / RegexOptions.IgnoreCase))
{
...
}
I am working on TCP server/client application.
My question is:
My server application starts a new thread and blocks it until connection is accepted the listenforClient method
But how can I manage the connections when multiple Clients are connected to my server and they request different things at the same time how can i manage that client 1 gets info its need and same for client 2.
It's multithreaded, so multiple Clients can connect but how can i process the request. I don't want to put everything in 1 method what than would.
Thanks in Advance
private void serverstart()
{
this.tcplistener = new TcpListener(IPAddress.Any, 49151);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcplistener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcplistener.AcceptTcpClient();
// here was first an message that send hello client
//
///////////////////////////////////////////////////
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
bufferincmessage = encoder.GetString(message, 0, bytesRead);
if (System.Text.RegularExpressions.Regex.IsMatch(bufferincmessage, Properties.Settings.Default.REQLogin, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
bufferincmessageresult = bufferincmessage.Split('^');
nickname_Cl = bufferincmessageresult[1];
password_Cl = bufferincmessageresult[2];
getuserdata_db();
login();
byte[] buffer = encoder.GetBytes(inlogmessage);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
}
}
Your client will be dispatched in different threads, so they will not intersect. You just need to add something like "DispatchMethod" where your messages will be processed.
using System.Text.RegularExpressions;
...
if (Regex.IsMatch(bufferincmessage, Properties.Settings.Default.REQLogin, RegexOptions.IgnoreCase))
{
...
}
else if (Regex.IsMatch(bufferincmessage, /*some of your command1*/, RegexOptions.IgnoreCase))
{
...
}
else if (Regex.IsMatch(bufferincmessage, /*some of your command1*/, RegexOptions.IgnoreCase))
{
...
}
这篇关于与多个客户端TCP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!