UdpClient.cs
using System;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Text;
namespace AssemblyCSharp
{
public class UdpClient
{
public UdpClient ()
{
app_id = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
app_id.Bind (new IPEndPoint (IPAddress.Any, 0));
}
public void SendData() {
EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 6000);
while (true)
{
string msg = Console.ReadLine();
app_id.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
}
public void RecvData() {
while (true)
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
byte[] buffer = new byte[1024];
int length = app_id.ReceiveFrom(buffer, ref point);//接收数据报
string message = Encoding.UTF8.GetString(buffer,0,length);
Console.WriteLine(point.ToString()+ message);
}
}
private Socket app_id;
private Thread recv_thrd;
private Thread send_thrd;
}
}