20070509 c# udp
http://www.yippeesoft.com

Option Explicit

Private Sub Command1_Click()
Winsock1.SendData "11111111"

End Sub

Private Sub Form_Load()
Winsock1.Bind 5577
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s
Debug.Print s
End Sub

class udpNxt
   &leftsign;
       Socket sock;
       int udpport = 5566;
       public udpNxt()
       &leftsign;
           IPHostEntry hostentry = Dns.GetHostEntry(Dns.GetHostName());
           IPEndPoint lpoint_udp = new IPEndPoint(IPAddress.Any, udpport);
           sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
           //sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
           //sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000);
           sock.Bind(lpoint_udp);
       &rightsign;
       public int udpNxtSend(byte[] bs,int len)
       &leftsign;
           return 0;
       &rightsign;

       public int udpNxtRecfrom()
       &leftsign;
           IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
           EndPoint senderRemote = (EndPoint)sender;
           byte[] msg = new Byte[256];
           Trace.WriteLine("Waiting to receive datagrams from client…");

           // This call blocks.
           sock.ReceiveFrom(msg,  ref senderRemote);
           Trace.WriteLine(BitConverter.ToString(msg));
           sock.SendTo(System.Text.Encoding.ASCII.GetBytes("OK"), senderRemote);
           sock.Close();
           return 0;
       &rightsign;
   &rightsign;

   http://www.codeproject.com/cs/internet/UDP_Events.asp
   Introduction

I recently needed to notify multiple WinForms clients (+/- 350) on a LAN that a certain event has happened which affects one of them.

There was also eventually a requirement to dynamically find these client applications and communicate with them. I went through various solutions to this problem, but the one which ended up working and scaling the best was using UDP broadcasts.

This article explains the process that led me to this, and the final solution.

bool done = false;
           int listenPort = 5566;
           UdpClient listener = new UdpClient(listenPort);
           IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

           try
           &leftsign;
               while (!done)
               &leftsign;
                   Console.WriteLine("Waiting for broadcast");
                   byte[] bytes = listener.Receive(ref groupEP);

                   Console.WriteLine("Received broadcast from &leftsign;0&rightsign; :\\n &leftsign;1&rightsign;\\n",
                       groupEP.ToString(),
                       Encoding.ASCII.GetString(bytes, 0, bytes.Length));
               &rightsign;

           &rightsign;
           catch (Exception ex)
           &leftsign;
               Console.WriteLine(ex.ToString());
           &rightsign;
           finally
           &leftsign;
               listener.Close();
           &rightsign;

历史博文

标签:,
十月 11, 2007 at 2:46 下午 by yippee 1,015 次
Category: Dev
Tags: ,