通过UDP C#广播消息

时间:2014-11-12 00:22:17

标签: c# .net networking network-programming udp

我的Program.cs类和handleClinet.cs类存在通信问题。程序处理来自每个尝试连接的新客户端的连接,handleClinet负责处理每个客户端的个人交互。

我在每个类中都有一个while循环,我认为,问题出在哪里..但我不确定如何做到这一点。服务器响应与他们应该响应的内容不对应。 (即没有进入我的广播功能并在循环中执行程序中的语句)是否有人有解决方案?提前致谢

class Program
{
    public static Hashtable clientsList = new Hashtable();

    static void Main(string[] args)
    {
        UdpClient server = new UdpClient(8888);
        IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
        string data = "";
        Console.WriteLine("Auction Server Started ....");

        while (true)
        {
            try
            {
                //Reads data
                byte[] inStream = server.Receive(ref remoteEndPoint);
                data = Encoding.ASCII.GetString(inStream);
                Console.WriteLine("REGISTER Client at " + remoteEndPoint);
                byte[] sendBytes = Encoding.ASCII.GetBytes("REGISTERED " + remoteEndPoint.ToString());
                server.Send(sendBytes, sendBytes.Length, remoteEndPoint);
                handleClinet client = new handleClinet();
                clientsList.Add(data, server);
                client.startClient(server, data, clientsList, remoteEndPoint);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //break;
            }
        }
    }

    //Broadcast method is used to send message to ALL clients
    public static void broadcast(UdpClient dest, string msg, string uName, bool flag, IPEndPoint sendEP)
    {
        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

}

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
            {
                requestCount = requestCount + 1;
                byte[] received = clientSocket.Receive(ref remoteIPEndPoint);
                dataFromClient = Encoding.ASCII.GetString(received);
                Console.WriteLine(dataFromClient);
                rCount = Convert.ToString(requestCount);

                if (dataFromClient.Contains("DEREGISTER"))
                    Program.broadcast(clientSocket, "DREG-CONF", clNo, true, myEP);
                else
                    Program.broadcast(clientSocket, dataFromClient, clNo, true, myEP);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                break;
            }
        }//end while
    }//end doChat
} //end class handleClinet

}

0 个答案:

没有答案