《网络编程实验.pdf》由会员分享,可在线阅读,更多相关《网络编程实验.pdf(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、网络编程网络编程实验实验 实验实验一一 IPAddress 类、Dns 类、IPHostEntry 类和 IPEndPoint 类的使用方法。1.界面(各控件名称如图所示)2、代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;namespace Wind
2、owsFormsApplication4 public partial class Form1:Form public Form1()textBox1 listBox1 Button1 Button2 InitializeComponent();private void button1_Click(object sender,EventArgs e)listBox1.Items.Clear();string name=Dns.GetHostName();listBox1.Items.Add(本机主机名:+name);IPHostEntry me=Dns.GetHostEntry(name);l
3、istBox1.Items.Add(本机所有的IP地址:);foreach(IPAddress ip in me.AddressList)listBox1.Items.Add(ip);IPAddress localip=IPAddress.Parse(127.0.0.1);IPEndPoint iep=new IPEndPoint(localip,80);listBox1.Items.Add(the IPEndPoint is:+iep.ToString();listBox1.Items.Add(the Address is:+iep.Address );listBox1.Items.Add(
4、the AddressFamily is:+iep.AddressFamily );listBox1.Items.Add(the max port number is:+IPEndPoint.MaxPort );listBox1.Items.Add(the min port number is:+IPEndPoint.MinPort );private void button2_Click(object sender,EventArgs e)listBox1.Items.Clear();IPHostEntry remoteHost=Dns.GetHostEntry(textBox1.Text)
5、;IPAddress remoteIp=remoteHost.AddressList;listBox1.Items.Add(remoteHost.HostName);listBox1.Items.AddRange(remoteIp);实验实验二二 UDP 进程通信,编写简易聊天工具。1、界面各控件名称如图所示。2、代码如下 using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Data;using System.Drawing;using System.Windows.Form
6、s;using System.Threading;using System.Collections.Generic;using System.ComponentModel;namespace _006_UPD 进程通信_Template public partial class frmMain:Form /显示消息回调 private delegate void ShowMsgCallBack(string text);private ShowMsgCallBack showMsgCallBack;txtLocalIP txtSendMsg btnStart txtSendIP txtSend
7、Port txtLocalPort btnSendMsg lstReceiveMsg chkAnony private UdpClient myUdpClient;/UDP 客户端 private UdpClient anonyUdpClient;/匿名 UDP 客户端 /窗构造方法 public frmMain()InitializeComponent();/窗体启动加载事件 private void frmMain_Load(object sender,EventArgs e)/初始化回调 showMsgCallBack=new ShowMsgCallBack(ShowMsg);/启动按钮
8、被单击事件 private void btnStart_Click(object sender,EventArgs e)/创建并实例化 IP 终端结点 IPEndPoint ipEndPoint=new IPEndPoint(IPAddress.Parse(txtLocalIP.Text),Convert.ToInt32(txtLocalPort.Text);/实例化 UDP 客户端(可用于实名发送和接收)myUdpClient=new UdpClient(ipEndPoint);/启动消息接收线程 Thread ReceiveMessageThread=new Thread(ReceiveM
9、essage);ReceiveMessageThread.IsBackground=true;ReceiveMessageThread.Start();/发送消息按钮被单击事件 private void btnSendMsg_Click(object sender,EventArgs e)/判断是否为匿名发送 if(chkAnony.Checked)/匿名发送(套接字绑定的端口由系统自动分配)anonyUdpClient=new UdpClient();/启动消息发送线程 Thread SendMessageThread=new Thread(SendMessage);SendMessageT
10、hread.IsBackground=true;SendMessageThread.Start();/消息发送线程 private void SendMessage()/创建并实例化 IP 终端结点 IPEndPoint ipEndPoint=new IPEndPoint(IPAddress.Parse(txtSendIP.Text),Convert.ToInt32(txtSendPort.Text);/准备发送的数据 byte sendData=Encoding.Default.GetBytes(txtSendMsg.Text);/判断是否为匿名发送 if(chkAnony.Checked)
11、/匿名发送 anonyUdpClient.Send(sendData,sendData.Length,ipEndPoint);/关闭匿名 UDP 客户端 anonyUdpClient.Close();else /实名发送 myUdpClient.Send(sendData,sendData.Length,ipEndPoint);/消息接收线程 private void ReceiveMessage()/创建并实例化 IP 终端结点 IPEndPoint ipEndPoint=new IPEndPoint(IPAddress.Any,0);/消息接收循环 while(true)try /同步阻塞
12、接收消息 byte getData=myUdpClient.Receive(ref ipEndPoint);string msg=Encoding.Default.GetString(getData);/显示消息到列表框中 lstReceiveMsg.Invoke(showMsgCallBack,ipEndPoint.ToString()+:+msg);catch(ThreadAbortException)/人为终止线程 catch(Exception ex)/接收发生异常 MessageBox.Show(ex.Message);/显示消息回调方法 private void ShowMsg(string text)lstReceiveMsg.Items.Add(text);