Unity网络通讯学习

---部分截图来自 siki学院Unity网络通讯课程

Socket

网络上的两个程序通过一个双向的通信连接实现数据交换,这个连接的一端称为一个 Socket ,Socket 包含了网络通信必须的五种信息

Socket 例子{

协议: TCP

本地: IP ,端口

远程: IP ,端口

}

可以通过ipconfig,netstat -ano 查看 Ip 和端口

创建客户端连接服务端

客户端代码:

cs 复制代码
using System;
using System.Net.Sockets;

namespace wangluo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Socket 需要using System.Net.Sockets;
            Socket tempClient = new Socket(AddressFamily.InterNetwork,
                  SocketType.Stream, ProtocolType.Tcp);
            tempClient.Connect("127.0.0.1", 8888);
        }
    }
}

服务端代码:

cs 复制代码
using System;
using System.Net.Sockets;
using System.Net;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket tempServer = new Socket(AddressFamily.InterNetwork, 
                 SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint tempEndPoint = 
                  new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  
                   //using System.Net;
            tempServer.Bind(tempEndPoint);  //绑定端口
            tempServer.Listen(0);  //监听,0表示挂起的队列无限长
            Console.WriteLine("服务端启动成功");

            Socket tempConnectClient = tempServer.Accept();
            Console.WriteLine("客户端连接成功:" + tempConnectClient);
        }
    }
}

先开启服务端再开启客户端

客户端服务端互发信息

客户端代码:

cs 复制代码
using System;
using System.Net.Sockets;
using System.Text;

namespace wangluo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Socket 需要using System.Net.Sockets;
            Socket tempClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            tempClient.Connect("127.0.0.1", 8888);

            //客户端向服务端发信息
            string tempSendInfo = Console.ReadLine();
            //将字符串转换成 buffer
            byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);
            //发送信息
            tempClient.Send(tempSendData);
            
            //接收来自服务端的信息
            byte[] tempReadBuffer = new byte[1024];
            tempClient.Receive(tempReadBuffer);
            string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);
            Console.WriteLine("接收到服务端信息:" + tempReadString);
        }
    }
}

服务端代码:

cs 复制代码
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket tempServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;
            tempServer.Bind(tempEndPoint);  //绑定端口
            tempServer.Listen(0);  //监听,0表示挂起的队列无限长
            Console.WriteLine("服务端启动成功");

            Socket tempConnectClient = tempServer.Accept();
            Console.WriteLine("客户端连接成功:" + tempConnectClient);

            byte[] tempReadBuffer = new byte[1024];
            tempConnectClient.Receive(tempReadBuffer);
            //将 byte 转换成字符串
            string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);
            Console.WriteLine("接收到客户端信息:" + tempReadString);

            //服务端向客户端发信息
            string tempSendInfo = Console.ReadLine();
            byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);
            tempConnectClient.Send(tempSendData);
        }
    }
}

封装一下代码:

客户端:

cs 复制代码
using System;
using System.Net.Sockets;
using System.Text;

namespace wangluo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Socket 需要using System.Net.Sockets;
            Socket tempClient = CreateSocket();

            tempClient.Connect("127.0.0.1", 8888);

            Send(tempClient);

            Receive(tempClient);
            
        }

        static Socket CreateSocket()
        {
            return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        static void Send(Socket tempClient)
        {
            //客户端向服务端发信息
            string tempSendInfo = Console.ReadLine();
            //将字符串转换成 buffer
            byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);
            //发送信息
            tempClient.Send(tempSendData);
        }

        static void Receive(Socket tempClient)
        {
            //接收来自服务端的信息
            byte[] tempReadBuffer = new byte[1024];
            tempClient.Receive(tempReadBuffer);
            string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);
            Console.WriteLine("接收到服务端信息:" + tempReadString);
        }
    }
}

服务端:

cs 复制代码
using System;
using System.Net.Sockets;
using System.Net;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket tempServer = CreateSocket();
            BindAndListen(tempServer);

            Socket tempConnectClient = Accept(tempServer);

            Receive(tempConnectClient);

            Send(tempConnectClient);
        }

        static Socket CreateSocket()
        {
            return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        static void BindAndListen(Socket pSocket)
        {
            IPEndPoint tempEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);  //using System.Net;
            pSocket.Bind(tempEndPoint);  //绑定端口
            pSocket.Listen(0);  //监听,0表示挂起的队列无限长
            Console.WriteLine("服务端启动成功");
        }

        static Socket Accept(Socket pSocket)
        {
            Socket tempConnectClient = pSocket.Accept();
            Console.WriteLine("客户端连接成功:" + tempConnectClient);
            return tempConnectClient;
        }

        static void Receive(Socket pSocket)
        {
            byte[] tempReadBuffer = new byte[1024];
            pSocket.Receive(tempReadBuffer);
            //将 byte 转换成字符串
            string tempReadString = Encoding.UTF8.GetString(tempReadBuffer);
            Console.WriteLine("接收到客户端信息:" + tempReadString);
        }

        static void Send(Socket pSocket)
        {
            //服务端向客户端发信息
            string tempSendInfo = Console.ReadLine();
            byte[] tempSendData = Encoding.UTF8.GetBytes(tempSendInfo);
            pSocket.Send(tempSendData);
        }
    }
}

Socket 代码解析:

AddressFamily: InterNetwork 使用IPv4 InterNetworkV6 使用IPv6

SocketType:

相关推荐
小李独爱秋14 分钟前
计算机网络经典问题透视:什么是服务质量QoS?为什么说“互联网根本没有服务质量可言?”
网络·计算机网络·安全·qos·服务质量
sun00770019 分钟前
android 系统中间件和 平台中间件 的区别,Framework等
网络
丁香结^20 分钟前
VLAN详解
网络·智能路由器
llilian_1627 分钟前
gps对时扩展装置 抢险救灾中时间同步精确的重要性分析 电力系统同步时钟
网络·功能测试·单片机·嵌入式硬件
纽格立科技35 分钟前
数字广播内容服务器NGA-101 DRM媒体编码器
网络·音视频·信息与通信·传媒·媒体
qq_4112624239 分钟前
esp32c3的at固件,开启了tcp服务器和透传模式。设备连接tcp后关闭wifi后没有断开tcp连接
服务器·网络·tcp/ip
首席拯救HMI官1 小时前
【拯救HMI】AR技术与HMI融合:工业现场的可视化新范式
网络·stm32·单片机·网络协议·ar·设计规范
NewCarRen1 小时前
AutoSec:面向车载网络的安全汽车数据传输方案
网络·安全·汽车
一条咸鱼_SaltyFish11 小时前
远程鉴权中心设计:HTTP 与 gRPC 的技术决策与实践
开发语言·网络·网络协议·程序人生·http·开源软件·个人开发
上海云盾安全满满15 小时前
选择高防IP时需要重点关注哪些因素
网络·网络协议·tcp/ip