当前位置:编程学习 > C#/ASP.NET >>

UDP广播的问题


   private static bool sendChatMsg(string remoteUser,IPEndPoint remoteUEP, string chatMsgStr)
        {
            
          
            byte[] msgbytes = Encoding.Unicode.GetBytes("31" + username + "|" + chatMsgStr);
            byte[] holdbytes = Encoding.Unicode.GetBytes("13" + username + "|" + remoteUser);
            chatDone = new ManualResetEvent(false);
            ClientSocket.SendTo(msgbytes, remoteUEP);
            chatDone.WaitOne(10000, true);
            if (testChat)
            {
                testChat = false;

                return true;

            }
            else
            {
                testHold = false;
                int tryIndex = 0;
                while (!testHold&&tryIndex<2)
                {
                    //Console.WriteLine("打洞ing..");
                    holdDone = new ManualResetEvent(false);
                    ClientSocket.SendTo(holdbytes, remoteUEP);
                    ClientSocket.SendTo(holdbytes, ServerEP);
                    holdDone.WaitOne(10000, true);
                    if (testHold)
                    {
                        break;
                    }
                    tryIndex++;
                    //Console.WriteLine("打洞超时,发送消息失败.");
                    //Console.Write("是否重试,按Y重试,按任意值结束发送:");
                    //if (Console.ReadLine().ToUpper() == "Y")
                    //{
                    //    testHold = false;
                    //}
                    //else
                    //{
                    //    return ;
                    //}
                }
                tryIndex = 0;

                while (!testChat&&testHold&&tryIndex<2)
                {
                    //Console.WriteLine("打洞成功,正在准备发送..");
                    chatDone = new ManualResetEvent(false);
                    ClientSocket.SendTo(msgbytes, remoteUEP);
                    chatDone.WaitOne(0x2710, true);
                    if (testChat)
                    {
                        //Console.WriteLine("消息发送成功!!");
                       testHold=false;
                       testChat = false;
                       return true;
                    }
                    //else
                    //{
                    //    return false;
                    //}
                    //Console.WriteLine("发送超时,发送消息失败.");
                    //Console.Write("是否重试,按Y重试,按任意值结束发送:");
                    //if (Console.ReadLine().ToUpper() == "Y")
                    //{
                    //    testChat = false;
                    //}
                    //else
                    //{
                    //    //return;
                    //}
                }
                testHold = false;
                testChat = false;
            }
            return false;
        }

public static void listen()
{
while (true)
{
try
{
byte[] data = new byte[1025];
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = sender;
int recv = Module1.ClientSocket.ReceiveFrom(data, ref tempRemoteEP);
string msgHead = Encoding.Unicode.GetString(data, 0, 4);
string left = msgHead;
bool flag = Operators.CompareString(left, "25", false) == 0;
if (flag)
{
Module1.msgSendEnd = true;
Module1.sendDone.Set();
}
else
{
flag = (Operators.CompareString(left, "10", false) == 0);
if (flag)
{
Module1.addOnLine(data, recv);
}
else
{
flag = (Operators.CompareString(left, "11", false) == 0);
if (flag)
{
Module1.removeOnLine(data, recv);
}
else
{
flag = (Operators.CompareString(left, "25", false) == 0);
if (flag)
{
Module1.msgSendEnd = true;
Module1.sendDone.Set();
}
else
{
flag = (Operators.CompareString(left, "22", false) == 0);
if (flag)
{
Console.WriteLine("\n\r收到打洞消息.");
Module1.makeHold(data, recv);
Console.Write("Client>");
}
else
{
flag = (Operators.CompareString(left, "31", false) == 0);
if (flag)
{
Module1.showChatMsg(data, recv);
}
else
{
flag = (Operators.CompareString(left, "30", false) == 0);
if (flag)
{
Module1.testHold = true;
Module1.holdDone.Set();
}
else
{
flag = (Operators.CompareString(left, "32", false) == 0);
if (flag)
{
Module1.testChat = true;
Module1.chatDone.Set();
}
}
}
}
}
}
}
}
}
catch (Exception arg_19A_0)
{
ProjectData.SetProjectError(arg_19A_0);
ProjectData.ClearProjectError();
}
}
}


测试聊天和测试打洞,都使用了ManualResetEvent,等LISTEN函数收到,这样太慢,如果我想同时发送给多个IP,即广播,怎么改一下呢?使每个发送和接受,互不干扰,而高效!异步吗

--------------------编程问答-------------------- 今天不是周末吧 --------------------编程问答-------------------- 用udp组播吧 --------------------编程问答-------------------- zhui22222,我这个不是对局域网的,是对因特网的IP发送的 --------------------编程问答-------------------- 路由器做了IP段限制的吧,不能随便组播的。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,