hahacjh
既然选择了远方 便只顾风雨兼程

 

代码
namespace UDPServer
{
public partial class FrmUdpServer : Form
{
//创建一个thread对象
private Thread thread1;
private UdpClient udpReceive;
private UdpClient udpSend;


public FrmUdpServer()
{
InitializeComponent();
}

private void FrmUdpServer_Load(object sender, EventArgs e)
{
thread1
= new Thread(new ThreadStart(this.ReceiveMessage));
thread1.Start();
}

private void ReceiveMessage()
{
byte[] receiveBytes = null;
//在本机指定的端口连接
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 8002);
udpReceive
= new UdpClient(remoteIpEndPoint);
IPEndPoint iep
= new IPEndPoint(IPAddress.Any,0);
//接收从远程发送过来的信息
while (true)
{
//ref表示引用类型 IPPoint 实例接收消息
try
{
receiveBytes
= udpReceive.Receive(ref iep);
}
//显示错误信息
catch (Exception ex)
{
MessageBox.Show(
"接收消息错误!" + ex.ToString());
return;
}
finally
{
}
string returnData = Encoding.UTF8.GetString(receiveBytes);
string msg = "来自" + iep.ToString() + "的消息";
if (returnData == "")
{
//显示错误消息并返回
MessageBox.Show("接收到空消息");
return;
}
//显示消息并以msg为消息的标题
DialogResult result = MessageBox.Show(returnData, msg);

}
}

private void btnSend_Click(object sender, EventArgs e)
{
if (tbxMessage.Text == "")
{
MessageBox.Show(
"消息不能为空!");
return;
}
//初始化UdpClient
udpSend = new UdpClient();
//允许发送和接受广播数据报
udpSend.EnableBroadcast = true;
//必须使用组播地址范围内的地址
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.10"), 8001);
//将发送内容转换为字节数组
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(tbxMessage.Text);

//设置重试次数
int retry = 0;
//进入消息循环
while (true)
{
//发送组播信息
try
{
//发送组广播信息
udpSend.Send(bytes, bytes.Length, iep);
//若成功则退出循环
break;
}
//如果发送消息失败
catch (Exception ex)
{
if (retry < 3)
{
//重试次数加1,继续尝试发送
retry++;
continue;
}
else
{
//如果重试次数大于3次,提示发送失败并跳出循环
DialogResult result = MessageBox.Show("发送失败!"+ex.ToString ());
break;
}
}
finally
{
//释放资源
}

}



tbxMessage.Clear();
//光标还原
tbxMessage.Focus();
udpSend.Close();
}

private void FrmUdpServer_FormClosing(object sender, FormClosingEventArgs e)
{
//关闭updSend连接
if (udpSend != null)
{
udpSend.Close();
}
udpReceive.Close();
thread1.Abort();
}



}
}

 

代码
namespace MyUdpClient
{
public partial class FrmUdpClient : Form
{
private Thread thread1;
private UdpClient udpSend;
private UdpClient udpReceive;

public FrmUdpClient()
{
InitializeComponent();
}

private void FrmUdpClient_Load(object sender, EventArgs e)
{
thread1
= new Thread(new ThreadStart(ReceiveMessage));
thread1.Start();
}

private void ReceiveMessage()
{
byte[] bytes = null;
//在本机制定和的端口连接
udpReceive = new UdpClient(8001);
//将套接字加入广播组
udpReceive.JoinMulticastGroup(IPAddress.Parse("224.100.0.10"), 50);
//接收从远程发送来的消息
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
//ref表示引用类型的IPPoint实例接受消息
try
{
bytes
= udpReceive.Receive(ref iep);
}
catch (Exception ex)
{
MessageBox.Show(
"接收消息错误!");
return;
}
finally
{
}
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
if (str == "")
{
MessageBox.Show(
"接收消息错误!");
return;
}

string msg = "来自" + iep.ToString() + "的消息";
//显示消息并以message 为消息的标题
DialogResult result = MessageBox.Show(str, msg);
}
}

private void FrmUdpClient_FormClosed(object sender, FormClosedEventArgs e)
{
if (udpSend != null)
{
udpSend.Close();
}
udpReceive.Close();
thread1.Abort();
}

private void btnSend_Click(object sender, EventArgs e)
{
byte[] bytes = null;
//如果发送消息为空,则不发送
if (tbxMessage.Text == "")
{
MessageBox.Show(
"消息不能为空");
return;
}
//初始化UdpClient
udpSend = new UdpClient();
//实际使用时应将127.0.0.1改为服务器的远程IP
IPAddress remoteIPAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteIPEndPoint
= new IPEndPoint(remoteIPAddress, 8002);

bytes
= System.Text.Encoding.UTF8.GetBytes(tbxMessage.Text);
//设置重发次数
int retry = 0;
//进入发送消息循环
while (true)
{
//将发送内容转换为字节数组
try
{
udpSend.Send(bytes, bytes.Length, remoteIPEndPoint);
//若成功则退出循环
break;

}
catch (Exception ex)
{
if (retry < 3)
{
//重试次数加1,继续尝试发送
retry++;
continue;
}
else
{
//如果重试次数大于3次,提示发送失败并跳出循环
DialogResult result = MessageBox.Show("发送失败!" + ex.ToString());
break;
}
}
finally
{
}

}


//清空TbxMessage中的内容
tbxMessage.Clear();
//光标还原
tbxMessage.Focus();
udpSend.Close();

}



}

}

 

 

 

posted on 2010-04-27 15:01  hahacjh  阅读(802)  评论(0编辑  收藏  举报