1. 程式人生 > >Unity 網絡編程(Socket)應用

Unity 網絡編程(Socket)應用

call tostring plist obj point client cep .text ntc

服務端:

/***
 * 
 * 
 *  服務器端 
 * 
 */

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using UnityEngine.UI;
using System;

public class Server : MonoBehaviour
{
    
public InputField InpIPAdress; //IP地址 public InputField InpPort; //端口號 public InputField InpDisplayInfo; //顯示信息 public InputField InpSendMsg; //發送信息 public Dropdown Dro_IPList; //
客戶端的IP列表 private Socket socketServer; //服務端套接字 private bool IsListionContect; //是否監聽 private StringBuilder _strDisplayInfo = new StringBuilder();//追加信息 private string _CurClientIPValue = string.Empty; //當前選擇的IP地址
//保存客戶端通訊的套接字 private Dictionary<string, Socket> _DicSocket = new Dictionary<string, Socket>(); //保存DropDown的數據,目的為了刪除節點信息 private Dictionary<string, Dropdown.OptionData> _DicDropdowm = new Dictionary<string, Dropdown.OptionData>(); // Start is called before the first frame update void Start() { //控件初始化 InpIPAdress.text = "127.0.0.1"; InpPort.text = "1000"; InpSendMsg.text = string.Empty; //下拉列表處理 Dro_IPList.options.Clear(); //清空列表信息 //添加一個空節點 Dropdown.OptionData op = new Dropdown.OptionData(); op.text = ""; Dro_IPList.options.Add(op); } /// <summary> /// 退出系統 /// </summary> public void ExitSystem() { if (socketServer != null) { socketServer.Shutdown(SocketShutdown.Both); //關閉連接 socketServer.Close(); //清理資源 } Application.Quit(); } /// <summary> /// 獲取當前好友 /// </summary> public void GetCurrentIPInformation() { //選擇玩家當前列表 _CurClientIPValue = Dro_IPList.options[Dro_IPList.value].text; } public void EnableServerReady() { //需要綁定地址和端口號 IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(InpIPAdress.text), Convert.ToInt32(InpPort.text)); //定義監聽Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //綁定 socketServer.Bind(endPoint); //開始socket監聽 socketServer.Listen(10); //顯示內容 //定義方法 //開啟前臺監聽(監聽客戶端連接) Thread thClientCon = new Thread(ListionCilentCon); thClientCon.Name = "thListionClientCon"; thClientCon.Start(); } private void ListionCilentCon() { Socket socMesgServer = null; //會話Socket try { while (IsListionContect) { //等待接受客戶端連接,此處會“阻斷”當前線程 socMesgServer = socketServer.Accept(); //獲取遠程節點信息 string strClientIPAndPort = socketServer.RemoteEndPoint.ToString(); //把遠程節點信息添加到DropDown控件中 Dropdown.OptionData op = new Dropdown.OptionData(); op.text = strClientIPAndPort; Dro_IPList.options.Add(op); //把會話Socket添加到集合中(為後面發送信息使用) _DicSocket.Add(strClientIPAndPort, socMesgServer); //控件顯示,有客戶端連接 //開啟後臺線程,接受客戶端信息 Thread thClientMsg = new Thread(ReceivMsg); thClientMsg.IsBackground = true; thClientMsg.Name = "thListionClientMsg"; thClientMsg.Start(socMesgServer); } } catch (Exception) { throw; } } /// <summary> /// 後臺線程,接受客戶端信息 /// </summary> /// <param name="sockMsg"></param> private void ReceivMsg(object sockMsg) { } // Update is called once per frame void Update() { } }

Unity 網絡編程(Socket)應用