110,535
社区成员
发帖
与我相关
我的任务
分享
class Program
{
static void Main(string[] args)
{
UdpManager udpManager = new UdpManager();
udpManager.receiveMsg();
}
}
UdpManager类:
private UdpClient udpClient;
private IPAddress remoteIpAddress;
private string remoteIp;
private int localPort;
public UdpManager() : this(Config.REMOTE_IP, Config.LOCAL_PORT) { }
public UdpManager(string remoteIp, int localPort)
{
this.localPort = localPort;
this.remoteIp = remoteIp;
remoteIpAddress = IPAddress.Parse(remoteIp);
IPEndPoint localIpEndPoint = new IPEndPoint(IPAddress.Parse(Config.LOCAT_IP), localPort);
udpClient = new UdpClient(localIpEndPoint);
}
public void receiveMsg()
{
Console.WriteLine("1...");
IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteIpAddress, 0);
while (true)
{
Console.WriteLine("2...");
try
{
byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
interpretingData(receiveBytes);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("3...");
}
}