初识C#网络编程

一、C#编写一个命令行/控制台程序

1.创建项目

在这里插入图片描述

2.编写代码及效果

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

namespace ConsoleApp1
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int i;
            for (i = 1; i <= 50; i++)
            {
    
    
                Console.WriteLine("Hello cqitu!重交物联2018   "+i);
            }
            Console.ReadLine(); //Console.ReadLine()方法从控制台读取任何行。通过输入此行代码,程序将等待并且不会立即退出
        }
    }
}

在这里插入图片描述

二、网络UDP 套接字

1.创建一个控制台程序

1.我们创建一个控制台程序
创建步骤和上面一样,客户端(发送方)代码:

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

namespace UDPClient
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            byte[] data = new byte[1024];
            string input, stringData;

            //构建UDP 服务器
            Console.WriteLine("This is a Client, host name is LZZ");

            //设置服务IP,设置UDP端口号
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.100"), 1316);

            //定义网络类型,数据连接类型和网络协议UDP
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            string welcome = "hello! ";
            data = Encoding.ASCII.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.ASCII.GetString(data, 0, recv));
            while (true)
            {
    
    
                string msg = "hello cqjtu! 重交物联2018级";
                int i = 1;
                while(i <= 50)
                {
    
    
                    server.SendTo(Encoding.ASCII.GetBytes(msg),Remote);
                    i++;
                }
                input = Console.ReadLine();
                if (input == "exit")
                    break;
                server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
                data = new byte[1024];
                recv = server.ReceiveFrom(data, ref Remote);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(stringData);
            }
            Console.WriteLine("Stopping Client.");
            server.Close();
        }

    }
}

服务端也要创建一个控制台程序,服务端代码:

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

namespace UDP
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int recv;
            byte[] data = new byte[1024];

            //得到本机IP,设置TCP端口号         
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 1316);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //绑定网络地址
            newsock.Bind(ip);

            Console.WriteLine("This is a Server, host name is TBM");

            //等待客户机连接
            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.ASCII.GetString(data, 0, recv));

            //客户机连接成功后,发送信息
            string welcome = "hello ! ";

            //字符串与字节数组相互转换
            data = Encoding.ASCII.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.ASCII.GetString(data, 0, recv));
                newsock.SendTo(data, recv, SocketFlags.None, Remote);
            }
        }

    }
}

2.运行及结果

在这里插入图片描述
在这里插入图片描述

三、C#编写一个简单的Form窗口程序

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210114153830174.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2lvc3RyZWFtaHZvaWQ=,size_16,color_FFFFFF,t_70
在这里插入图片描述
Button1_Click代码:

byte[] data = new byte[1024];
            string input;


            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.82"), 1316);

            //定义网络类型,数据连接类型和网络协议UDP
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


            server.SendTo(data, data.Length, SocketFlags.None, ip);
            IPEndPoint sende = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)sende;

            data = new byte[1024];
            //对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
            int recv = server.ReceiveFrom(data, ref Remote);
         


            input = textBox1.Text;
                if (input == "exit")
                    server.Close();
            server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
            server.Close();

CS的main函数代码:

byte[] data = new byte[1024];

            //构建TCP 服务器
            Console.WriteLine("This is a Client, host name is TBM");

            //设置服务IP,设置TCP端口号
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.82"), 1316);

            //定义网络类型,数据连接类型和网络协议UDP
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            string welcome = "success connect! ";
            data = Encoding.ASCII.GetBytes(welcome);
            server.SendTo(data, data.Length, SocketFlags.None, ip);
            IPEndPoint sende = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)sende;

            data = new byte[1024];
            //对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
            int recv = server.ReceiveFrom(data, ref Remote);
            Console.WriteLine("Message received from {0}: ", Remote.ToString());
            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

在这里插入图片描述

四、通过 wireshark抓包,对数据帧进行分析

在这里插入图片描述

五、参考链接

链接: link.

猜你喜欢

转载自blog.csdn.net/iostreamhvoid/article/details/109500712