服务器代码:
Thread thdUDPServer;
private void Server_Load(object sender, EventArgs e)
{
thdUDPServer = new Thread(new ThreadStart(serverThread));
thdUDPServer.Start();
}
UdpClient udpClient;
public void serverThread()
{
udpClient = new UdpClient(53);
while (true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
listBox1.Items.Add(RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString());
}
}
客户代码:
UdpClient udpClient;
private void Client_Load(object sender, EventArgs e)
{
udpClient = new UdpClient();
}
private void button1_Click(object sender, EventArgs e)
{
udpClient.Connect(textBox1.Text, 53);
Byte[] senddata = Encoding.ASCII.GetBytes("Hello World");
udpClient.Send(senddata, senddata.Length);
}
在本地网络上工作,但我需要全球化。
详细信息:
示例:
udpClient.Connect(95.10.xxx.13, 53);
我在哪里做错了? 如何使用其他方法通过全球网络发送数据?