交互就是比较烦的,涉及的东西还是很多的
使用Socket通信,在局域网中还是比较合适的。
TCP的三次握手差不多也可以保证数据的正确性了,算是比较可靠的了
也参考了网络上其他的一些写法,自己写个测试下,可以通过。
服务器端:
using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
public class Server1
: MonoBehaviour
{
Socket serverSocket;
Socket clientSocket;
Thread clientThread;
Thread t;
// Use this for initialization
void Start()
{
t = new Thread(new ThreadStart(OpenServer));
t.Start();
}
// Update is called once per frame
void Update()
{
}
void OpenServer()
{
IPAddress ipAdr = IPAddress.Parse("127.0.0.1");
//端口号,服务器端和客户端需要保持一致,否则端口访问错误
IPEndPoint ipEp = new IPEndPoint(ipAdr, 8081);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(ipEp);
serverSocket.Listen(10);
while (true)
{
try
{
clientSocket = serverSocket.Accept();
clientThread = new Thread(new ThreadStart(ReceiveData));
clientThread.Start();
}
catch (System.Exception ex)
{
print(ex);
}
}
}
void ReceiveData()
{
bool keepalive = true;
Socket s = clientSocket;
byte[] buffer = new byte[1024];
//根据收听到的客户端套接字向客户端发送信息
IPEndPoint clientep = (IPEndPoint)s.RemoteEndPoint;
//lstServer.Items.Add("Client:" + clientep.Address + "("+clientep.Port+")");
string welcome = "IP为" + clientep.Address + "的客户端连接成功";
byte[] data = new byte[1024];
data = Encoding.UTF8.GetBytes(welcome);
s.Send(data, data.Length, SocketFlags.None);
while (keepalive)
{
//在套接字上接收客户端发送的信息
int bufLen = 0;
try
{
bufLen = s.Available;
s.Receive(buffer, 0, bufLen, SocketFlags.None);
if (bufLen == 0)
continue;
}
catch (Exception ex)
{
Debug.Log("Receive Error:" + ex.Message);
return;
}
clientep = (IPEndPoint)s.RemoteEndPoint;
string clientcommand = System.Text.Encoding.UTF8.GetString(buffer).Substring(0, bufLen);
Debug.Log("服务端收到:" + clientcommand);
//lstServer.Items.Add(clientcommand + "("+clientep.Address + ":"+clientep.Port+")");
}
}
}
客户端;
1 using UnityEngine; 2 using System; 3 using System.Net; 4 using System.Net.Sockets; 5 using System.Text; 6 using System.Threading; 7 8 using System.Collections.Generic; 9 public class Client1 10 : MonoBehaviour 11 { 12 Socket clientSocket; 13 byte[] sdata = new byte[1024]; 14 Thread t; 15 // Use this for initialization 16 void Start() 17 { 18 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 25 } 26 27 void ConnectServer() 28 { 29 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8081); 30 clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 31 try 32 { 33 clientSocket.Connect(ipep); 34 } 35 catch (SocketException ex) 36 { 37 Debug.Log("connect error: " + ex.Message); 38 return; 39 } 40 41 while (true) 42 { 43 //接收服务器信息 44 int bufLen = 0; 45 try 46 { 47 bufLen = clientSocket.Available; 48 clientSocket.Receive(sdata, 0, bufLen, SocketFlags.None); 49 if (bufLen == 0) 50 { 51 continue; 52 } 53 } 54 catch (Exception ex) 55 { 56 Debug.Log("Receive Error:" + ex.Message); 57 return; 58 } 59 string clientcommand = System.Text.Encoding.UTF8.GetString(sdata).Substring(0, bufLen); 60 Debug.Log("客户端收到:" + clientcommand); 61 62 } 63 } 64 65 void sendMsg() 66 { 67 byte[] data = new byte[1024]; 68 data = Encoding.UTF8.GetBytes(sendStr); 69 clientSocket.Send(data, data.Length, SocketFlags.None); 70 } 71 72 string sendStr = "发送信息"; 73 void OnGUI() 74 { 75 if (GUILayout.Button("连接")) 76 { 77 t = new Thread(new ThreadStart(ConnectServer)); 78 t.Start(); 79 } 80 sendStr = GUILayout.TextArea(sendStr); 81 if (GUILayout.Button("send")) 82 { 83 sendMsg(); 84 } 85 } 86 87 88 }
搞定
只需建立两个项目工程,一个客户端,一个服务器,分别挂载脚本