赞
踩
using System; using System.Text; using System.Net; using System.Net.Sockets; namespace Client { class Program { static void Main(string[] args) { byte[] data = new byte[1024]; //输出客户端信息 Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName()); //设置服务IP(这个IP地址是服务器的IP),设置端口号 IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.61.37.148"), 8001); //定义网络类型,数据连接类型和网络协议UDP Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //发送问候信息嘿服务器 string welcome = "你好,服务器!"; data = Encoding.UTF8.GetBytes(welcome); server.SendTo(data, data.Length, SocketFlags.None, ip); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)sender; data = new byte[1024]; //对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制 int recv = server.ReceiveFrom(data, ref Remote); Console.WriteLine("Message received from {0}: ", Remote.ToString()); Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv)); int i = 0; while (true) { string s = "hello cqjtu!重交物联2019级" + i; Console.WriteLine(s); //发送数据 server.SendTo(Encoding.UTF8.GetBytes(s), Remote); if (i == 50) { break; } i++; } Console.WriteLine("Stopping Client."); server.Close(); } } }
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Sockets; namespace Server { class Program { static void Main(string[] args) { //数据大小 int recv; //存储数据 byte[] data = new byte[1024]; //得到本机IP,设置UDP端口号 IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8001); //新建套接字,基于IPv和4UDP协议 //当 SocketType 为 Dgram 时,ProtocolType 始终为 Udp。当 SocketType 为 Stream 时,ProtocolType 始终为 Tcp Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //绑定本机ip地址 newsock.Bind(ip); //打印服务器ip地址 Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName()); //等待客户机连接 Console.WriteLine("Waiting for a client"); //一直保持运行 while (true) { //一直等待客户端连接 try { while (true) { //得到客户机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 = "你好 ! "; //字符串与字节数组相互转换 data = Encoding.UTF8.GetBytes(welcome); //发送信息 newsock.SendTo(data, data.Length, SocketFlags.None, Remote); //一直接收客户端信息 while (true) { //1000ms无信息直接断开连接 if (!newsock.Poll(1000, SelectMode.SelectRead)) { Console.WriteLine("连接中断!"); break; } data = new byte[1024]; //接收信息 recv = newsock.ReceiveFrom(data, ref Remote); Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv)); } } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } } } }
服务器接收信息
客户端发送信息
通过观察每个接口的流量变化以及发送数据的时间节点可以找到通过哪个接口发送的数据,再进行数据包的抓取
添加端口过滤找到数据包
其中hello cqjtu!重交物联2019级对应编码为
打开其中一个包分析
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 WindowsFormsApp1 { public partial class Form1 : Form { public IPEndPoint sender; public EndPoint Remote; public Form1() { InitializeComponent(); //CheckForIllegalCrossThreadCalls = false; //可以删除 panel5.Hide(); sender= new IPEndPoint(IPAddress.Any, 0); Remote= (EndPoint)sender; } private void button1_Click(object sender, EventArgs e) { byte[] data = new byte[1024]; //获取输入的ip地址以及端口号 IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ipAddressText.Text), int.Parse(beginPortText.Text)); //定义网络类型,数据连接类型和网络协议UDP Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); server.SendTo(data, data.Length, SocketFlags.None, ip); //连接服务器 server.ReceiveFrom(data, ref Remote); messages.Items.Add("成功连接服务器" + Remote.ToString()); //获取信息 messages.Items.Add("发送消息"+endPortText.Text); //发送信息 server.SendTo(Encoding.UTF8.GetBytes(endPortText.Text), Remote); //关闭连接 server.Close(); } } }
服务器可以直接使用原来代码
只针对客户端进行可视化
通过分析可以发现发送的数据
c#网络通信编程很方便,winfrom可视化编程配合c#可以很快的实现界面功能
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。