【基于Unity3D实现TCP网络通信】
1、优缺点
(1)优点:可靠、稳定,TCP的可靠体现在TCP在传输数据之前,会有三次握手来建立连接,而且在数据传输之前,会有三次握手来建立连接,而且在数据传输时,有确认、窗口、重传、拥塞控制机制,在数据传完后还会断开连接用来节约系统资源。
(2)缺点:TCP有确认机制、三次握手机制,每次连接都会占用硬件资源导致数据传输慢,效率低,占用系统资源高。
2、TCP实现通信流程(三次握手、四次挥手)
(1)建立连接(三次握手):发送方和接收方通过三次握手建立TCP连接。首先,发送方向接收方发送SYN(同步)报文,表示请求建立连接;接收方收到SYN报文后,向发送方回送ACK(确认)和SYN报文,表示接收方已经准备好建立连接;发送方再回送ACK报文,表示连接建立成功。
(2)数据传输:连接建立成功后,发送方可以向接收方发送数据。TCP协议会将数据分成若干个数据段,每个数据段有编号,并且会保证按序传输,确保数据的可靠性。
(3)确认接收:接收方收到数据后,会向发送方发送ACK确认报文,表示成功接收数据。
(4)关闭连接(四次挥手):一旦数据传输完成,发送方和接收方都可以向对方发送FIN(结束)报文,表示要关闭连接。当一方收到FIN报文后,会回送ACK报文确认收到,同时关闭自己的连接。当另一方收到ACK报文后,也关闭自己的连接,这样双方的连接就完全关闭了。
3、基于Unity3D实现TCP实现代码
(1)Server
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets; //调用socket
using System.Text;
using System.Threading; //调用线程
using UnityEngine;
using System.IO;
public class TCPServer : MonoBehaviour
{
//定义变量(与GUI一致)
private string info = "NULL"; //状态信息
private string recMes = "NULL"; //接收到的信息
private int recTimes = 0; //接收到的信息次数
private string inputIp = "127.0.0.1"; //ip地址(本地)
private string inputPort = "8080"; //端口值
private string inputMessage = "NULL"; //用以发送的信息
private Socket socketWatch; //用以监听的套接字
private Socket socketSend; //用以和客户端通信的套接字
private bool isSendData = false; //是否点击发送数据按钮
private bool clickConnectBtn = false; //是否点击监听按钮
private bool IsConnect = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//建立tcp通信链接
private void ClickConnect()
{
try
{
int _port = Convert.ToInt32(inputPort); //获取端口号(32位,4个字节)
string _ip = inputIp; //获取ip地址
Debug.Log(" ip 地址是 :" + _ip);
Debug.Log(" 端口号是 :" + _port);
clickConnectBtn = true; //点击了监听按钮,更改状态
info = "ip地址是 : " + _ip + "端口号是 : " + _port;
//点击开始监听时 在服务端创建一个负责监听IP和端口号的Socket
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(_ip);
IPEndPoint point = new IPEndPoint(ip, _port); //创建对象端口
socketWatch.Bind(point); //绑定端口号
Debug.Log("监听成功!");
info = "监听成功";
socketWatch.Listen(10); //设置监听,最大同时连接10台
//创建监听线程
Thread thread = new Thread(Listen);
thread.IsBackground = true;
thread.Start(socketWatch);
}
catch { }
}
/// <summary>
/// 等待客户端的连接 并且创建与之通信的Socket
/// </summary>
void Listen(object o)
{
try
{
Socket socketWatch = o as Socket;
while (IsConnect)
{
socketSend = socketWatch.Accept(); //等待接收客户端连接
Debug.Log(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!");
info = socketSend.RemoteEndPoint.ToString() + " 连接成功!";
Thread r_thread = new Thread(Received); //开启一个新线程,执行接收消息方法
r_thread.IsBackground = true;
r_thread.Start(socketSend);
Thread s_thread = new Thread(SendMessage); //开启一个新线程,执行发送消息方法
s_thread.IsBackground = true;
s_thread.Start(socketSend);
}
}
catch { }
}
// 服务器端不停的接收客户端发来的消息
void Received(object o)
{
try
{
Socket socketSend = o as Socket;
while (IsConnect)
{
byte[] buffer = new byte[1024 * 6]; //客户端连接服务器成功后,服务器接收客户端发送的消息
int len = socketSend.Receive(buffer); //实际接收到的有效字节数
if (len == 0)
{
break;
}
string str = Encoding.UTF8.GetString(buffer, 0, len);
Debug.Log("接收到的消息:" + socketSend.RemoteEndPoint + ":" + str);
recMes = str;
recTimes++;
info = "接收到一次数据,接收次数为:" + recTimes;
Debug.Log("接收数据次数:" + recTimes);
}
}
catch { }
}
// 服务器端不停的向客户端发送消息
void SendMessage(object o)
{
try
{
Socket socketSend = o as Socket;
while (IsConnect)
{
if (isSendData)
{
isSendData = false;
byte[] sendByte = Encoding.UTF8.GetBytes(inputMessage);
Debug.Log("发送的数据为 :" + inputMessage);
Debug.Log("发送的数据字节长度 :" + sendByte.Length);
socketSend.Send(sendByte);
}
}
}
catch { }
}
// 关闭连接,释放资源
private void OnDisable()
{
Debug.Log("begin OnDisable()");
if (clickConnectBtn)
{
try
{
IsConnect = false;
socketWatch.Shutdown(SocketShutdown.Both); //禁用Socket的发送和接收功能
socketWatch.Close(); //关闭Socket连接并释放所有相关资源
socketSend.Shutdown(SocketShutdown.Both); //禁用Socket的发送和接收功能
socketSend.Close(); //关闭Socket连接并释放所有相关资源
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
Debug.Log("end OnDisable()");
}
//交互界面(代码创建)
void OnGUI()
{
GUI.color = Color.black; //字体颜色
GUI.Label(new Rect(65, 10, 80, 20), "状态信息");
GUI.Label(new Rect(155, 10, 80, 70), info);
GUI.Label(new Rect(65, 80, 80, 20), "接收到消息:");
GUI.Label(new Rect(155, 80, 80, 20), recMes);
GUI.Label(new Rect(65, 120, 80, 20), "发送的消息:");
inputMessage = GUI.TextField(new Rect(155, 120, 100, 20), inputMessage, 20);
GUI.Label(new Rect(65, 160, 80, 20), "本机ip地址:");
inputIp = GUI.TextField(new Rect(155, 160, 100, 20), inputIp, 20);
GUI.Label(new Rect(65, 200, 80, 20), "本机端口号:");
inputPort = GUI.TextField(new Rect(155, 200, 100, 20), inputPort, 20);
if (GUI.Button(new Rect(65, 240, 60, 20), "开始监听"))
{
ClickConnect(); //点击开始
}
if (GUI.Button(new Rect(65, 280, 60, 20), "发送数据"))
{
isSendData = true; //发送数据
}
}
}
(2)Client
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
public class TCPClient : MonoBehaviour
{
private string staInfo = "NULL"; //状态信息
private string inputIp = "127.0.0.1"; //输入ip地址
private string inputPort = "8080"; //输入端口号
public string inputMes = "NULL"; //发送的消息
private int recTimes = 0; //接收到信息的次数
private string recMes = "NULL"; //接收到的消息
private Socket socketSend; //客户端套接字,用来链接远端服务器
private bool clickSend = false; //是否点击发送按钮
private bool IsConnect = true;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
//建立链接
private void ClickConnect()
{
try
{
int _port = Convert.ToInt32(inputPort); //获取端口号
string _ip = inputIp; //获取ip地址
//创建客户端Socket,获得远程ip和端口号
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(_ip);
IPEndPoint point = new IPEndPoint(ip, _port);
socketSend.Connect(point);
Debug.Log("连接成功 , " + " ip = " + ip + " port = " + _port);
staInfo = ip + ":" + _port + " 连接成功";
Thread r_thread = new Thread(Received); //开启新的线程,不停的接收服务器发来的消息
r_thread.IsBackground = true;
r_thread.Start();
Thread s_thread = new Thread(SendMessage); //开启新的线程,不停的给服务器发送消息
s_thread.IsBackground = true;
s_thread.Start();
}
catch (Exception)
{
Debug.Log("IP或者端口号错误......");
staInfo = "IP或者端口号错误......";
}
}
/// <summary>
/// 接收服务端返回的消息
/// </summary>
void Received()
{
while (IsConnect)
{
try
{
byte[] buffer = new byte[1024 * 6];
//实际接收到的有效字节数
int len = socketSend.Receive(buffer);
if (len == 0)
{
break;
}
recMes = Encoding.UTF8.GetString(buffer, 0, len);
Debug.Log("客户端接收到的数据 : " + recMes);
recTimes++;
staInfo = "接收到一次数据,接收次数为 :" + recTimes;
Debug.Log("接收次数为:" + recTimes);
}
catch { }
}
}
/// <summary>
/// 向服务器发送消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void SendMessage()
{
try
{
while (IsConnect)
{
if (clickSend) //如果点击了发送按钮
{
clickSend = false;
string msg = inputMes;
byte[] buffer = new byte[1024 * 6];
buffer = Encoding.UTF8.GetBytes(msg);
socketSend.Send(buffer);
Debug.Log("发送的数据为:" + msg);
}
}
}
catch { }
}
private void OnDisable()
{
Debug.Log("begin OnDisable()");
if (socketSend.Connected)
{
try
{
IsConnect = false;
socketSend.Shutdown(SocketShutdown.Both); //禁用Socket的发送和接收功能
socketSend.Close(); //关闭Socket连接并释放所有相关资源
}
catch (Exception e)
{
print(e.Message);
}
}
Debug.Log("end OnDisable()");
}
//用户界面
void OnGUI()
{
GUI.color = Color.black;
GUI.Label(new Rect(65, 10, 60, 20), "状态信息");
GUI.Label(new Rect(135, 10, 80, 60), staInfo);
GUI.Label(new Rect(65, 70, 50, 20), "服务器ip地址");
inputIp = GUI.TextField(new Rect(125, 70, 100, 20), inputIp, 20);
GUI.Label(new Rect(65, 110, 50, 20), "服务器端口");
inputPort = GUI.TextField(new Rect(125, 110, 100, 20), inputPort, 20);
GUI.Label(new Rect(65, 150, 80, 20), "接收到消息:");
GUI.Label(new Rect(155, 150, 80, 20), recMes);
GUI.Label(new Rect(65, 190, 80, 20), "发送的消息:");
inputMes = GUI.TextField(new Rect(155, 190, 100, 20), inputMes, 20);
if (GUI.Button(new Rect(65, 230, 60, 20), "开始连接"))
{
ClickConnect();
}
if (GUI.Button(new Rect(65, 270, 60, 20), "发送信息"))
{
clickSend = true;
}
}
}