本文主要介绍了如何使用udp协议,多线程,让ppc和pc在同一局域网进行简单的文字收发。
我们要做好如下设置,因为是用模拟器来配置网络环境,你还必须参考下面这篇文章进行模拟器网络环境配置:点击察看
说明:我的PPC端ip是192.168.0.102,服务器端为192.168.0.100,请根据实际情况配置。
配置好后,就可以开始我们的编程了。
设计客户端(PPC 端)如下图:
代码如下:
PPC Code
namespace SimpleTcp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBoxCallback = new AddListBoxItemCallback(AddListBoxItem);
}
// string senddate,readdate;
// NetworkStream ns;
delegate void AddListBoxItemCallback(string text);
AddListBoxItemCallback listBoxCallback;
private int port = 8001;
private UdpClient udpClient;
private void AddListBoxItem(string text)
{
//如果listBoxReceive被不同的线程访问则通过委托处理;
if (listBoxReceive.InvokeRequired)
{
this.Invoke(listBoxCallback, text);
}
else
{
listBoxReceive.Items.Add(text);
listBoxReceive.SelectedIndex = listBoxReceive.Items.Count - 1;
}
}
private void ReceiveData()
{
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
AddListBoxItem(string.Format("来自{0}:{1}", remote, str));
}
catch
{
//退出循环,结束线程
break;
}
}
}
/// 发送数据到远程主机
/// </summary>
private void sendData()
{
UdpClient myUdpClient = new UdpClient();
IPAddress remoteIP=IPAddress.Parse(textBoxRemoteIP.Text);
if ( remoteIP== null)
{
MessageBox.Show("远程IP格式不正确");
return;
}
IPEndPoint iep = new IPEndPoint(remoteIP, port);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textBoxSend.Text);
try
{
myUdpClient.Send(bytes, bytes.Length, iep);
myUdpClient.Close();
textBoxSend.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpClient.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
udpClient.Close();
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
sendData();
}
private void Form1_Load(object sender, EventArgs e)
{
//获取本机第一个可用IP地址
IPAddress myIP = IPAddress.Parse("192.168.0.100");
//为了在同一台机器调试,此IP也作为默认远程IP
textBoxRemoteIP.Text = myIP.ToString();
//创建一个线程接收远程主机发来的信息
Thread myThread = new Thread(new ThreadStart(ReceiveData));
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
textBoxSend.Focus();
}
}
}
namespace SimpleTcp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBoxCallback = new AddListBoxItemCallback(AddListBoxItem);
}
// string senddate,readdate;
// NetworkStream ns;
delegate void AddListBoxItemCallback(string text);
AddListBoxItemCallback listBoxCallback;
private int port = 8001;
private UdpClient udpClient;
private void AddListBoxItem(string text)
{
//如果listBoxReceive被不同的线程访问则通过委托处理;
if (listBoxReceive.InvokeRequired)
{
this.Invoke(listBoxCallback, text);
}
else
{
listBoxReceive.Items.Add(text);
listBoxReceive.SelectedIndex = listBoxReceive.Items.Count - 1;
}
}
private void ReceiveData()
{
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
AddListBoxItem(string.Format("来自{0}:{1}", remote, str));
}
catch
{
//退出循环,结束线程
break;
}
}
}
/// 发送数据到远程主机
/// </summary>
private void sendData()
{
UdpClient myUdpClient = new UdpClient();
IPAddress remoteIP=IPAddress.Parse(textBoxRemoteIP.Text);
if ( remoteIP== null)
{
MessageBox.Show("远程IP格式不正确");
return;
}
IPEndPoint iep = new IPEndPoint(remoteIP, port);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(textBoxSend.Text);
try
{
myUdpClient.Send(bytes, bytes.Length, iep);
myUdpClient.Close();
textBoxSend.Focus();
}
catch (Exception err)
{
MessageBox.Show(err.Message, "发送失败");
}
finally
{
myUdpClient.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
udpClient.Close();
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
sendData();
}
private void Form1_Load(object sender, EventArgs e)
{
//获取本机第一个可用IP地址
IPAddress myIP = IPAddress.Parse("192.168.0.100");
//为了在同一台机器调试,此IP也作为默认远程IP
textBoxRemoteIP.Text = myIP.ToString();
//创建一个线程接收远程主机发来的信息
Thread myThread = new Thread(new ThreadStart(ReceiveData));
//将线程设为后台运行
myThread.IsBackground = true;
myThread.Start();
textBoxSend.Focus();
}
}
}
客户端就ok了。
下面我们写服务器端,代码和客户端基本一致,就不赘述了,服务器端设计界面如下:
最终效果如下图:
转载(如果有人转载)请注明出处吧,谢谢。