unity Socket TCP連線案例(一)
阿新 • • 發佈:2018-11-24
非常清晰的demo
服務端
using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using UnityEngine; public class SocketServer : MonoBehaviour { // Use this for initialization public void StartServer () { bt_connnect_Click(); } // Update is called once per frame void Update () { } private void bt_connnect_Click() { try { int _port = 6000; string _ip = "127.0.0.1"; //點選開始監聽時 在服務端建立一個負責監聽IP和埠號的Socket 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("監聽成功!"); socketWatch.Listen(10);//設定監聽,最大同時連線10臺 //建立監聽執行緒 Thread thread = new Thread(Listen); thread.IsBackground = true; thread.Start(socketWatch); } catch { } } /// <summary> /// 等待客戶端的連線 並且建立與之通訊的Socket /// </summary> Socket socketSend; void Listen(object o) { try { Socket socketWatch = o as Socket; while (true) { socketSend = socketWatch.Accept();//等待接收客戶端連線 Debug.Log(socketSend.RemoteEndPoint.ToString() + ":" + "連線成功!"); //開啟一個新執行緒,執行接收訊息方法 Thread r_thread = new Thread(Received); r_thread.IsBackground = true; r_thread.Start(socketSend); } } catch { } } /// <summary> /// 伺服器端不停的接收客戶端發來的訊息 /// </summary> /// <param name="o"></param> void Received(object o) { try { Socket socketSend = o as Socket; while (true) { //客戶端連線伺服器成功後,伺服器接收客戶端傳送的訊息 byte[] buffer = new byte[1024 * 1024 * 3]; //實際接收到的有效位元組數 int len = socketSend.Receive(buffer); if (len == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, len); Debug.Log("伺服器列印:" + socketSend.RemoteEndPoint + ":" + str); Send("我收到了"); } } catch { } } /// <summary> /// 伺服器向客戶端傳送訊息 /// </summary> /// <param name="str"></param> void Send(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); } }
客戶端
using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using UnityEngine; using UnityEngine.UI; public class SocketClient : MonoBehaviour { public InputField input;// Use this for initialization public void StartClient () { bt_connect_Click(); } // Update is called once per frame void Update () { } public void SendMsg() { bt_send_Click(input.text); } Socket socketSend; private voidbt_connect_Click() { try { int _port = 6000; string _ip = "127.0.0.1"; //建立客戶端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("連線成功!"); //開啟新的執行緒,不停的接收伺服器發來的訊息 Thread c_thread = new Thread(Received); c_thread.IsBackground = true; c_thread.Start(); } catch (Exception) { Debug.Log("IP或者埠號錯誤..."); } } /// <summary> /// 接收服務端返回的訊息 /// </summary> void Received() { while (true) { try { byte[] buffer = new byte[1024 * 1024 * 3]; //實際接收到的有效位元組數 int len = socketSend.Receive(buffer); if (len == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, len); Debug.Log("客戶端列印:" + socketSend.RemoteEndPoint + ":" + str); } catch { } } } /// <summary> /// 向伺服器傳送訊息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bt_send_Click(string str) { try { string msg = str; byte[] buffer = new byte[1024 * 1024 * 3]; buffer = Encoding.UTF8.GetBytes(msg); socketSend.Send(buffer); } catch { } } }