多线程UDP客户端/服务器C#
本文关键字:服务器 客户端 UDP 多线程 | 更新日期: 2023-09-27 18:30:13
我正试图让一个多线程UDP客户端/服务器运行,但我在服务器端遇到了问题。当客户端尝试注册时,会创建一个线程,并在该线程中处理该客户端的所有交互,但由于某种原因,它只进入该线程一次,然后立即退出。。有人能帮我弄清楚为什么会发生这种事吗-提前谢谢。。
namespace AuctionServer
{
class Program
{
public static Hashtable clientsList = new Hashtable();
static void Main(string[] args)
{
//IPAddress ipAd = IPAddress.Parse("255.255.255.255");
UdpClient server = new UdpClient(8888);
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
string data = "";
int listSize = 0;
Console.WriteLine("Auction Server Started ....");
listSize = clientsList.Count;
while (true)
{
//Reads data
byte[] inStream = server.Receive(ref remoteEndPoint);
data = Encoding.ASCII.GetString(inStream);
//Console.WriteLine("REGISTER " + remoteEndPoint);
if (!data.Contains("DEREGISTER "))
{
byte[] sendBytes = Encoding.ASCII.GetBytes(data + remoteEndPoint.ToString());
server.Send(sendBytes, sendBytes.Length, remoteEndPoint);
handleClinet client = new handleClinet();
Console.WriteLine(data);
clientsList.Add(data, server);
client.startClient(server, data, clientsList, remoteEndPoint);
data = "";
}
}
}
//Broadcast method is used to send message to ALL clients
public static void broadcast(UdpClient dest, string msg, string uName, bool flag, IPEndPoint sendEP, Hashtable clientsList)
{
foreach (DictionaryEntry Item in clientsList)
{
Byte[] broadcastBytes = null;
if (flag == true)
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
else
{
broadcastBytes = Encoding.ASCII.GetBytes(msg);
}
dest.Send(broadcastBytes, broadcastBytes.Length, sendEP);
}
}
}//end Main class
}
namespace AuctionServer
{
public class handleClinet
{
UdpClient clientSocket;
string clNo;
Hashtable clientsList;
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);
IPEndPoint myEP = new IPEndPoint(IPAddress.Any, 0);
public void startClient(UdpClient inClientSocket, string clineNo, Hashtable cList, IPEndPoint tempEP)
{
this.myEP = tempEP;
this.clientSocket = inClientSocket;
this.clNo = clineNo;
this.clientsList = cList;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10025];
string dataFromClient = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
//Thread.Sleep(1000);
if (requestCount == 0)
{
Console.WriteLine("Thread Created");
requestCount++;
}
byte[] received = clientSocket.Receive(ref remoteIPEndPoint);
dataFromClient = Encoding.ASCII.GetString(received);
Console.WriteLine(dataFromClient);
if (dataFromClient.Contains("DEREGISTER"))
clientSocket.Send(received, received.Length, remoteIPEndPoint);
//Program.broadcast(clientSocket, "DREG-CONF", clNo, true, myEP, clientsList);
//else
// Program.broadcast(clientSocket, dataFromClient, clNo, true, myEP, clientsList);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
break;
}
}//end while
}//end doChat
}
这两个循环在不同的线程上运行。因此,Main()
中的循环与handleClinet
类中的循环同时执行。
如果要切换到handleClinet
类的循环并对其进行调试,请使用调试器中的Threads
窗口(Debug
菜单、Windows
菜单项,然后是Threads
…或按Ctrl
-D
、T
)切换到该线程。然后您可以看到该线程的调用堆栈和状态。
请注意,这可能不适用于Visual Studio的速成版。我还没有尝试过最新的版本,但以前的版本不支持Threads
窗口。(您仍然可以通过在那里设置断点来调试特定线程…只是您不能手动切换到该线程)。