实验环境:Windows10, Virtual Studio2015
一、UDP
二、控制台程序
1.新建项目
点击Visual C#——》控制台应用程序——》确定
2.代码(含分析)
//client端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace UDPSender
{
class program
{
static void Main(String[] args)
{
int flag = 50;
byte[] data = new byte[1024];
string input,stringData;
Console.WriteLine("this is a Client.host name is {0}",Dns.GetHostName());
//设置服务器IP和端口号,127.0.0.1是本机IP地址,可以填需要连接的IP,需处于同一局域网下,10.60.4.195为室友IP
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("10.60.4.195"),8001);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
string welcome = "这是Client";
data = Encoding.UTF8.GetBytes(welcome);
server.SendTo(data,data.Length,SocketFlags.None,ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any,0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = server.ReceiveFrom(data,ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
while(flag>0)
{
//发送信息
byte[] data2 = new byte[1024];
input = "Hello cqjtu!重交物联18级";
data2 = Encoding.UTF8.GetBytes(input);
server.SendTo(data2,data2.Length,SocketFlags.None,ipep);
//显示服务器返回的信息
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.UTF8.GetString(data, 0, recv);
Console.WriteLine(stringData);
//标志-1
flag--;
}
}
}
}
//server端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace UDPReceive
{
class Program
{
static void Main(String[] args)
{
int recv;
byte[] data = new byte[1024];
//得到本机IP,设置端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,8001);
Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
//绑定网络地址
newsock.Bind(ipep);
Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
//等待客户机连接
Console.WriteLine("Waiting for a client");
//得到客户端IP
IPEndPoint sender= new IPEndPoint(IPAddress.Any,0);
EndPoint Remote = (EndPoint)(sender);
//接受数据
recv = newsock.ReceiveFrom(data,ref Remote);
Console.WriteLine("Message received from {0}",Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data,0,recv));
//客户端连接成功后,发送信息
string welcome = "这里是Server";
data = Encoding.UTF8.GetBytes(welcome);
//发送信息
newsock.SendTo(data,data.Length,SocketFlags.None,Remote);
while(true)
{
data = new byte[1024];
recv = newsock.ReceiveFrom(data,ref Remote);
Console.WriteLine(Encoding.UTF8.GetString(data,0,recv));
//发送接受的信息
newsock.SendTo(data,recv,SocketFlags.None,Remote);
}
}
}
}
3.运行效果
三、Form程序
1.新建项目
点击Visual C#——》Windows窗体应用程序——》确定
2.代码(分析)
服务器端可以用上面的。
其它文件不用改,只需需改Form1.cs(可以双击按钮进入函数)
界面设计
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace clientForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string stringdate;
byte[] data = new byte[1024];
//设置服务器IP和端口号
IPEndPoint ippo = new IPEndPoint(IPAddress.Parse("10.60.4.195"), 8001);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//发送信息
stringdate = textBox1.ToString();
data = Encoding.UTF8.GetBytes(stringdate);
server.SendTo(data,data.Length,SocketFlags.None,ippo);
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
while (true)
{
//发送信息
byte[] data2 = new byte[1024];
string input = "Hello cqjtu!重交物联18级";
data2 = Encoding.UTF8.GetBytes(input);
server.SendTo(data2, data2.Length, SocketFlags.None, ippo);
break;
}
}
}
}
3.运行效果
本机上测试还是可行的。
和室友一起测试,使用的还是刚才的服务器端代码。
四、wireshark抓包
网络通信从上到下分为应用层(表示层和会话层一般含在应用层)、传输层、网络层、数据链路层和物理层。各层的PDU(基本单元)也不一样。
传输层PDU为段(segment)
网络层PDU为包(packet)
数据链路层PDU为帧(frame)
物理层PDU为位(bit)
用wireshark抓出来的包的帧格式为
(8B前导码)|6B目的MAC地址 |6B源MAC地址 |2B类型域 |46~1500B的数据域 |4B校验码(wireshark抓包会自动去掉)
五、参考资料
六、总结
UDP是一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。UDP是与TCP相对应的协议,它是面向非连接的协议,它不与对方连接,而直接把数据包发送过去。“面向非连接”就是正式通信前不必与对方建立连接,不管对方状态就直接发送,比较缺少安全性。