【无标题】

  • Post author:
  • Post category:其他




使用TCP/UDP协议通信并用Wireshark抓包分析数据



一.Socket、TCP、UDP



1. Socket

我们知道两个进程如果需要进行通讯最基本的一个前提能能够唯一的标示一个进程,在本地进程通讯中我们可以使用PID来唯一标示一个进程,但PID只在本地唯一,网络中的两个进程PID冲突几率很大,这时候我们需要另辟它径了,我们知道IP层的ip地址可以唯一标示主机,而TCP层协议和端口号可以唯一标示主机的一个进程,这样我们可以利用ip地址+协议+端口号唯一标示网络中的一个进程。

能够唯一标示网络中的进程后,它们就可以利用socket进行通信了,什么是socket呢?我们经常把socket翻译为套接字,socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信。



2. TCP

TCP协议提供的是端到端服务。TCP协议所提供的端到端的服务是保证信息一定能够到达目的地址。它是一种面向连接的协议。

TCP编程的服务器端一般步骤

①创建一个socket,用函数socket()

②绑定IP地址、端口等信息到socket上,用函数bind()

③开启监听,用函数listen()

④接收客户端上来的连接,用函数accept()

⑤收发数据,用函数send()和recv(),或者read()和write()

⑥关闭网络连接;

⑦关闭监听;

TCP编程的客户端一般步骤

①创建一个socket,用函数socket()

②设置要连接的对方的IP地址和端口等属性

③连接服务器,用函数connect()

④收发数据,用函数send()和recv(),或者read()和write()

⑤关闭网络连接

在这里插入图片描述



3. UDP

UDP 是User Datagram Protocol的简称, 中文名是用户数据包协议,是OSI(Open System Interconnection,开放式系统互联) 参考模型中一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务,IETF RFC 768 是UDP的正式规范。UDP在IP报文的协议号是17。

UDP协议与TCP协议一样用于处理数据包,在OSI模型中,两者都位于传输层,处于IP协议的上一层。UDP有不提供数据包分组、组装和不能对数据包进行排序的缺点,也就是说,当报文发送之后,是无法得知其是否安全完整到达的。UDP用来支持那些需要在计算机之间传输数据的网络应用。包括网络视频会议系统在内的众多的客户/服务器模式的网络应用都需要使用UDP协议。UDP协议从问世至今已经被使用了很多年,虽然其最初的光彩已经被一些类似协议所掩盖,但即使在今天UDP仍然不失为一项非常实用和可行的网络传输层协议。

UDP编程的服务器端一般步骤:

1.创建一个socket,用函数socket()

2.绑定IP地址、端口等信息到socket上,用函数bind()

3.循环接收数据,用函数recvfrom()

4.关闭网络连接

UDP编程的客户端一般步骤:

1.创建一个socket,用函数socket()

2.设置对方的IP地址和端口等属性

3.发送数据,用函数sendto()

4.关闭网络连接



二、UDP套接字发送信息

按照以下步骤在VS2019中新建项目(控制台应用程序):

在这里插入图片描述

保存项目位置:

在这里插入图片描述



1. 显示信息

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Send1
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i = 0; i < 50; i++)
            {
                Console.WriteLine("hello cqjtu!重交物联2019级");
            }

            System.Console.ReadLine();

        }
    }
}


运行代码:

在这里插入图片描述



2. 实现UDP套接字发送信息


服务端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Send1
{
    class MyServer
    {
        static void Main()
        {
            int result;
            string str = "第50行:hello cqjtu!重交物联2019级";
            UdpClient client = new UdpClient(11000);
            string receiveString = null;
            byte[] receiveData = null;
            //实例化一个远程端点,IP和端口可以随意指定,等调用client.Receive(ref remotePoint)时会将该端点改成真正发送端端点 
            IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
            Console.WriteLine("正在准备接收数据...");
            while (true)
            {
                receiveData = client.Receive(ref remotePoint);//接收数据 
                receiveString = Encoding.Default.GetString(receiveData);
                Console.WriteLine(receiveString);
                result = String.Compare(receiveString, str);
                if (result == 0)
                {
                    break;
                }
            }
            client.Close();//关闭连接
            Console.WriteLine("");
            Console.WriteLine("数据接收完毕,按任意键退出...");
            System.Console.ReadKey();
        }
    }
}


客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Send1_C
{
    class Program
    {
        static void Main()
        {
            //提示信息
            Console.WriteLine("按下任意按键开始发送...");
            Console.ReadKey();

            int m;

            //做好链接准备
            UdpClient client = new UdpClient();  //实例一个端口
            IPAddress remoteIP = IPAddress.Parse("127.0.0.1");  //假设发送给这个IP
            int remotePort = 11000;  //设置端口号
            IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);  //实例化一个远程端点 

            for (int i = 0; i < 50; i++)
            {
                //要发送的数据:第n行:hello cqjtu!重交物联2019级
                string sendString = null;
                sendString += "第";
                m = i + 1;
                sendString += m.ToString();
                sendString += "行:hello cqjtu!重交物联2019级";

                //定义发送的字节数组
                //将字符串转化为字节并存储到字节数组中
                byte[] sendData = null;
                sendData = Encoding.Default.GetBytes(sendString);

                client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点 
            }
            client.Close();//关闭连接

            //提示信息
            Console.WriteLine("");
            Console.WriteLine("数据发送成功,按任意键退出...");
            System.Console.ReadKey();
        }
    }
}

运行代码:

客户端运行:

在这里插入图片描述

服务端运行:

在这里插入图片描述



3. Wireshark抓包与分析

利用Wireshark抓包可以发现抓取了50个包:

在这里插入图片描述

随机选取一个包,可以发现这条包的信息是

第30行:hello cqjtu!重交物联2019级


在这里插入图片描述



三、TCP使用窗口程序发送信息



1.设计界面

新建项目:

在这里插入图片描述

1.配置显示消息的TextBox的属性

设置多行:

在这里插入图片描述

添加其垂直滚动条:

在这里插入图片描述

设置其边界样式为:

在这里插入图片描述

设置只能读:

在这里插入图片描述

设置name:

在这里插入图片描述

2.配置输入消息的TextBox的属性

设置其name:

在这里插入图片描述

设置字体和大小:

在这里插入图片描述

3.配置Button属性

设置其文本:

在这里插入图片描述

设置其name:

在这里插入图片描述

4.配置窗体Form属性

点击选择该窗体:

在这里插入图片描述

修改后:

在这里插入图片描述

设置窗体的接受按钮,即按下Enter键触发该按钮的点击事件:

在这里插入图片描述

设置自动适应大小:

在这里插入图片描述



2.编写代码

编写按钮点击事件:

在这里插入图片描述

在 button1_Click 函数内复制粘贴如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

         public void button1_Click(object sender, EventArgs e)
        {
            try
            {
                /*
                 * 显示当前时间
                 */
                string str = "The current time: ";
                str += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                textBox2.AppendText(str + Environment.NewLine);
                /*
                 * 做好连接准备
                 */
                int port = 2000;
                string host = "10.60.202.32";//我室友的IP地址
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket
                /*
                 * 开始连接
                 */
                str = "Connect to server...";
                textBox2.AppendText(str + Environment.NewLine);
                c.Connect(ipe);//连接到服务器
                /*
                 *发送消息 
                 */
                string sendStr = textBox1.Text;
                str = "The message content: " + sendStr;
                textBox2.AppendText(str + Environment.NewLine);
                byte[] bs = Encoding.UTF8.GetBytes(sendStr);
                str = "Send the message to the server...";
                textBox2.AppendText(str + Environment.NewLine);
                c.Send(bs, bs.Length, 0);//发送信息
                /*
                 * 接收服务器端的反馈信息
                 */
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
                recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                str = "The server feedback: " + recvStr;//显示服务器返回信息
                textBox2.AppendText(str + Environment.NewLine);
                /*
                 * 关闭socket
                 */
                c.Close();
            }
            catch (ArgumentNullException f)
            {
                string str = "ArgumentNullException: " + f.ToString();
                textBox2.AppendText(str + Environment.NewLine);
            }
            catch (SocketException f)
            {
                string str = "ArgumentNullException: " + f.ToString();
                textBox2.AppendText(str + Environment.NewLine);
            }
            textBox2.AppendText("" + Environment.NewLine);
            textBox1.Text = "";
        }
    }
}

运行效果:

在这里插入图片描述



3. 服务端编写

创建一个新的控制台程序,在main函数中编写如下代码:

static void Main(string[] args)
{
    /*
    * 做好连接准备
    */
    int i = 0;
    int port = 2000;
    string host = "127.0.0.1";
    IPAddress ip = IPAddress.Parse(host);
    IPEndPoint ipe = new IPEndPoint(ip, port);
    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
    s.Bind(ipe);//绑定2000端口
    /*
        * 循环监听并处理消息
        */
    while (true)
    {
        i++;
        try
        {
            Console.Write("Perform operations {0} :", i);
            Console.WriteLine("\t-----------------------------------------------");
            s.Listen(0);//开始监听
            Console.WriteLine("1. Wait for connect...");
            /*
                * 实例一个新的socket端口
                */
            Socket temp = s.Accept();//为新建连接创建新的Socket。
            Console.WriteLine("2. Get a connect");
            /*
                * 接收客户端发的消息并做解码处理
                */
            string recvStr = "";
            byte[] recvBytes = new byte[1024];
            int bytes;
            bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//从客户端接受信息
            recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
            Console.WriteLine("3. Server Get Message:{0}", recvStr);//把客户端传来的信息显示出来
            /*
                * 返回给客户端连接成功的消息
                */
            string sendStr = "Ok!Client send message sucessful!";
            byte[] bs = Encoding.UTF8.GetBytes(sendStr);
            temp.Send(bs, bs.Length, 0);//返回客户端成功信息
            /*
                * 关闭端口
                */
            temp.Close();
            Console.WriteLine("4. Completed...");
            Console.WriteLine("-----------------------------------------------------------------------");
            Console.WriteLine("");
            //s.Close();//关闭socket(由于再死循环中,所以不用写,但如果是单个接收,实例socket并完成任务后需关闭)
        }
        catch (ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: {0}", e);
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
    }
}

在客户端和服务端都编写好以后,运行代码:

客户端:

在这里插入图片描述

服务端:

在这里插入图片描述



4.抓包
在这里插入图片描述



四、总结

学会了如何使用 UDP/TCP 套接字进行网络通信,这都是基于 C/S 模式,一个客户端,一个服务器端,在使用套接字的时候,端口号、IP地址是必不可缺的,缺一不可,使用套接字发送信息十分方便。



五、参考


https://blog.csdn.net/weixin_46628481/article/details/121388939



https://blog.csdn.net/ssj925319/article/details/109336123



版权声明:本文为weixin_50909683原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。