UDP通讯组件using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; using System.Diagnostics;namespace UDPComm { public class UDP { public delegate void dlg_UDPMsgHandler(string receivebytes); public static event dlg_UDPMsgHandler UDPMsgArrived; dlg_UDPMsgHandler udpmsghandle = new dlg_UDPMsgHandler(UDPRaiseEvent);
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;namespace UDPComm
{
public class UDP
{
public delegate void dlg_UDPMsgHandler(string receivebytes);
public static event dlg_UDPMsgHandler UDPMsgArrived; dlg_UDPMsgHandler udpmsghandle = new dlg_UDPMsgHandler(UDPRaiseEvent);
public static void Send(string range, string datagram)
{
UdpClient sender = new UdpClient();
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(range),10001); try
{
datagram += (char)3;
byte[] bytes = Encoding.ASCII.GetBytes(datagram);
sender.Send(bytes,bytes.Length,endPoint);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return;
}
finally
{
sender.Close();
}
} public static void Receiver()
{ UdpClient receiveUdpClient = new UdpClient(10001);
IPEndPoint RemoteIPEndPoint = null; try
{
while (true)
{
byte[] receiveBytes = receiveUdpClient.Receive(ref RemoteIPEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
if (returnData.IndexOf((char)3) > -1)
{
UDPRaiseEvent(returnData.Substring(0, returnData.Length - 1));
}
else
return;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return;
}
} public static void UDPRaiseEvent(string recebytes)
{
UDPMsgArrived(recebytes);
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Diagnostics;
using UDPComm;namespace UdpServer
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Char
{
[STAThread]
static void Main(string[] args)
{
try
{
Thread tRec = new Thread(new ThreadStart(UDPComm.UDP.Receiver));
tRec.Start();
UDP.UDPMsgArrived += new UDP.dlg_UDPMsgHandler(UDP_UDPMsgArrived);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
} static void UDP_UDPMsgArrived(string receivebytes)
{
Console.WriteLine(System.DateTime.Now.ToShortTimeString()+" "+receivebytes);
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Diagnostics;
using UDPComm;namespace UdpServer
{
class Char
{
[STAThread]
static void Main(string[] args)
{
try
{
Thread tSend = new Thread(new ThreadStart(s));
tSend.Start();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void s()
{
string D = ((char)22).ToString();
string F = ((char)3).ToString();
string S = ((char)16).ToString(); for (int i=0;i<1000;i++)
{
Console.WriteLine(i.ToString());
UDPComm.UDP.Send("127.255.255.255", "IVIP1"+D+"250"+S+"301"+S+System.DateTime.Now.ToString("yyyyMMdd")+S+System.DateTime.Now.ToString("HHmmss")+S+"1"+S+i.ToString().PadLeft(15));
Thread.Sleep(1000);
}
//How to end the UDP receive thread
UDPComm.UDP.Send("127.255.255.255", F);
}
}
}
发送127.255.255.255是发送到哪里?