多线程UDP客户端/服务器C#

时间:2014-11-12 17:55:06

标签: c# network-programming udp client server

我正在尝试使用多线程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

}

1 个答案:

答案 0 :(得分:1)

这两个循环在不同的线程上运行。因此Main()中的循环与handleClinet类中的循环同时执行。

如果您想切换到handleClinet课程的循环并进行调试,请使用调试器中的Threads窗口Debug菜单,Windows菜单项,然后Threads ...或按Ctrl - DT)切换到该主题。然后你可以看到调用堆栈和该线程的状态。

请注意,这可能不适用于Visual Studio的Express版本。我还没有尝试过最新版本,但过去的版本不支持Threads窗口。 (您仍然可以通过在那里设置断点来调试特定线程......它只是您无法手动切换到线程。)