public partial class Form1 : Form
{
private UdpClient receiveUdpClient;
private UdpClient sendUdpClient;
private const int port = 80;
IPAddress ip;
IPAddress remoteIp;
public Form1()
{
InitializeComponent();
//获取本机可用IP地址
ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
remoteIp=ip; //默认远程主机地址
//remoteIp = new IPAddress(new byte[]{222,25,180,15});//已设置好远程主机ip,用于局域网测试
textBoxRemoteIP.Text = remoteIp.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
//创建一个线程接收远程主机发来的信息
Thread myThread = new Thread(ReceiveData);
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
textBoxSend.Focus();
}
private void ReceiveData()
{
IPEndPoint local = new IPEndPoint(ip, port);
receiveUdpClient = new UdpClient(local);
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
byte[] receiveBytes = receiveUdpClient.Receive(ref remote);
string receiveMessage = Encoding.Unicode.GetString(receiveBytes, 0, receiveBytes.Length);
AddItem(listBoxReceive, string.Format("来自{0}:{1}", remote, receiveMessage));
}
catch
{
break;
}
}
}
//发送信息
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(SendMessage);
t.IsBackground = true;
t.Start(textBoxSend.Text);
}
private void SendMessage(object obj)
{
string message = (string)obj;
sendUdpClient = new UdpClient(0);
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(message);
IPEndPoint iep = new IPEndPoint(ip, port);
try
{
sendUdpClient.Send(bytes, bytes.Length, iep); //运行至此处跳转到catch,出错
AddItem(listBoxStatus, string.Format("向{0}发送:{1}", iep, message));
ClearTextBox();
}
catch (Exception ex)
{
AddItem(listBoxStatus, "发送出错:" + ex.Message);
}
}//下面的代码是委托类,封装,应该没问题的
delegate void AddListBoxItemDelegate(ListBox listbox, string text);
private void AddItem(ListBox listbox, string text)
{
if (listbox.InvokeRequired)
{
AddListBoxItemDelegate d = AddItem;
listbox.Invoke(d, new object[] { listbox, text });
}
else
{
listbox.Items.Add(text);
listbox.SelectedIndex = listbox.Items.Count - 1;
listbox.ClearSelected();
}
}
delegate void ClearTextBoxDelegate();
private void ClearTextBox()
{
if (textBoxSend.InvokeRequired)
{
ClearTextBoxDelegate d = ClearTextBox;
textBoxSend.Invoke(d);
}
else
{
textBoxSend.Clear();
textBoxSend.Focus();
}
}
}
}
同一台电脑上运行:
在局域网测试过了,另外一台机器上能接收到我发送的信息,而我不能接收到对面发送的信息
{
private UdpClient receiveUdpClient;
private UdpClient sendUdpClient;
private const int port = 80;
IPAddress ip;
IPAddress remoteIp;
public Form1()
{
InitializeComponent();
//获取本机可用IP地址
ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
remoteIp=ip; //默认远程主机地址
//remoteIp = new IPAddress(new byte[]{222,25,180,15});//已设置好远程主机ip,用于局域网测试
textBoxRemoteIP.Text = remoteIp.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
//创建一个线程接收远程主机发来的信息
Thread myThread = new Thread(ReceiveData);
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
textBoxSend.Focus();
}
private void ReceiveData()
{
IPEndPoint local = new IPEndPoint(ip, port);
receiveUdpClient = new UdpClient(local);
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
byte[] receiveBytes = receiveUdpClient.Receive(ref remote);
string receiveMessage = Encoding.Unicode.GetString(receiveBytes, 0, receiveBytes.Length);
AddItem(listBoxReceive, string.Format("来自{0}:{1}", remote, receiveMessage));
}
catch
{
break;
}
}
}
//发送信息
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(SendMessage);
t.IsBackground = true;
t.Start(textBoxSend.Text);
}
private void SendMessage(object obj)
{
string message = (string)obj;
sendUdpClient = new UdpClient(0);
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(message);
IPEndPoint iep = new IPEndPoint(ip, port);
try
{
sendUdpClient.Send(bytes, bytes.Length, iep); //运行至此处跳转到catch,出错
AddItem(listBoxStatus, string.Format("向{0}发送:{1}", iep, message));
ClearTextBox();
}
catch (Exception ex)
{
AddItem(listBoxStatus, "发送出错:" + ex.Message);
}
}//下面的代码是委托类,封装,应该没问题的
delegate void AddListBoxItemDelegate(ListBox listbox, string text);
private void AddItem(ListBox listbox, string text)
{
if (listbox.InvokeRequired)
{
AddListBoxItemDelegate d = AddItem;
listbox.Invoke(d, new object[] { listbox, text });
}
else
{
listbox.Items.Add(text);
listbox.SelectedIndex = listbox.Items.Count - 1;
listbox.ClearSelected();
}
}
delegate void ClearTextBoxDelegate();
private void ClearTextBox()
{
if (textBoxSend.InvokeRequired)
{
ClearTextBoxDelegate d = ClearTextBox;
textBoxSend.Invoke(d);
}
else
{
textBoxSend.Clear();
textBoxSend.Focus();
}
}
}
}
同一台电脑上运行:
在局域网测试过了,另外一台机器上能接收到我发送的信息,而我不能接收到对面发送的信息
不过我最近也做了这个
UDP打洞把
你要先判断是 局域网内的通讯 还是 局域网外
然后两端都要"监听"和接受!!!!!
同一个NAT下的话,用私有终端IP + 自定义端口打洞
不同NAT的话,用公网IP + 公网PORT打洞!!!