发送端代码:
public static void SendMessage(string message)
{
byte[] sendbytes = Encoding.Unicode.GetBytes(message);
UdpClient sendUdpClient = new UdpClient(0);
IPAddress localIp = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteIpEndPoint = new IPEndPoint(localIp, 5000);
sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpEndPoint);
sendUdpClient.Close();
}
接收端代码:
private void PayComplete()
{
new Thread(() =>
{
IPAddress localIp = IPAddress.Parse("127.0.0.1");
IPEndPoint localIpEndPoint = new IPEndPoint(localIp, 5000);
UdpClient receiveUpdClient = new UdpClient(localIpEndPoint);
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
payResult = new PayResult(showResult);
while (true)
{
byte[] receiveBytes = receiveUpdClient.Receive(ref remoteIpEndPoint);
string message = Encoding.Unicode.GetString(receiveBytes);
this.Dispatcher.BeginInvoke(payResult, message);
}
}).Start();
}