Socket
1 using System; 2 using System.Collections.Generic; 3 using System.Net; 4 using System.Net.Sockets; 5 using System.Text; 6 7 8 namespace Socket服务端 { 9 class Program { 10 static void Main(string[] args) { 11 12 //1.创建一个Socket对象 13 Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 14 //2.绑定一个IP和端口 15 //IPAddress ipAddress = new IPAddress(new byte[] { 127,0,0,1 }); 16 IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); 17 EndPoint endPoint = new IPEndPoint(ipAddress,5000); 18 tcpServer.Bind(endPoint); 19 //3.开始监听客户端的连接请求 20 tcpServer.Listen(100); 21 Console.WriteLine("服务器启动完成"); 22 Socket clientSocket = tcpServer.Accept(); 23 Console.WriteLine("接收到客户端的连接请求!"); 24 //4.发送/接收消息 25 string sendMessage = "Hello Client"; 26 //将字符串转换为字节数组 27 byte[] sendData = Encoding.UTF8.GetBytes(sendMessage); 28 clientSocket.Send(sendData); 29 Console.WriteLine("服务器向客户端发送了一条消息:" + sendMessage); 30 31 //接收客户端消息 32 byte[] receiveData = new byte[1024]; 33 int length = clientSocket.Receive(receiveData); 34 string receiveMessage = Encoding.UTF8.GetString(receiveData); 35 Console.WriteLine("服务器接收到客户端发送过来的消息:" + receiveMessage); 36 37 Console.Read(); 38 } 39 } 40 }
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections; 10 using System.Net.Sockets; 11 using System.Net; 12 using System.Text; 13 14 namespace VoidGame { 15 16 public class SocketClient : MonoBehaviour { 17 18 private Socket tcpClient; 19 private string serverIP = "127.0.0.1"; 20 private int serverPort = 5000; 21 22 void Start() { 23 //1.创建一个Socket; 24 tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 25 //2.建立连接请求 26 IPAddress ipAddress = IPAddress.Parse(serverIP); 27 EndPoint endPoint = new IPEndPoint(ipAddress,serverPort); 28 tcpClient.Connect(endPoint); 29 Debug.Log("请求服务器连接"); 30 //3.接受/发送消息 31 byte[] receiveData = new byte[1024]; 32 int length = tcpClient.Receive(receiveData); 33 string receiveMessage = Encoding.UTF8.GetString(receiveData,0,length); 34 Debug.Log("客户端接收到服务器发来的消息:" + receiveMessage); 35 36 //发送消息 37 string sendMessage = "Client Say To Server Hello"; 38 tcpClient.Send(Encoding.UTF8.GetBytes(sendMessage)); 39 Debug.Log("客户端向服务器发送消息:" + sendMessage); 40 } 41 } 42 }
Json
xml
protobuf
www
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections; 10 using System.IO; 11 12 namespace VoidGame { 13 14 public enum GetPicType { 15 Download = 0, 16 LocalLoad = 1 17 } 18 19 public class Picture : MonoBehaviour { 20 21 private string url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3525821899,4147777390&fm=21&gp=0.jpg"; 22 23 /// <summary> 24 /// 从网络下载的图片 25 /// </summary> 26 private Texture2D img = null; 27 28 /// <summary> 29 /// 从本地读取的图片 30 /// </summary> 31 private Texture2D img2 = null; 32 33 34 private bool downloadOK = false; 35 36 37 void OnGUI() { 38 if(img != null) { 39 GUI.DrawTexture(new Rect(0,0,200,300),img); 40 } 41 if(img2 != null) { 42 GUI.DrawTexture(new Rect(320,0,200,300),img2); 43 } 44 if(GUI.Button(new Rect(210,0,100,20),"从网络加载图片")) { 45 StartCoroutine(DownloadTexture(url,GetPicType.Download)); 46 } 47 if(GUI.Button(new Rect(210,50,100,20),"从本地加载图片")) { 48 if(downloadOK) { 49 StartCoroutine(DownloadTexture("file://"+Application.streamingAssetsPath+"/dota2.png",GetPicType.LocalLoad)); 50 } else { 51 Debug.LogError("没有下载的图片"); 52 } 53 } 54 } 55 56 IEnumerator DownloadTexture(string url,GetPicType getPicType) { 57 WWW www = new WWW(url); 58 yield return www; 59 60 Texture2D tempImage = null; 61 if(www.isDone && www.error == null) { 62 switch(getPicType) { 63 case GetPicType.Download: 64 img = www.texture; 65 tempImage = img; 66 Debug.Log(tempImage.width + " " + tempImage.height); 67 break; 68 case GetPicType.LocalLoad: 69 img2 = www.texture; 70 tempImage = img2; 71 Debug.Log(tempImage.width + " " + tempImage.height); 72 break; 73 default: 74 tempImage = null; 75 break; 76 } 77 } 78 if(tempImage != null) { 79 byte[] data = tempImage.EncodeToPNG(); 80 File.WriteAllBytes(Application.streamingAssetsPath + "/dota2.png",data); 81 downloadOK = true; 82 } 83 } 84 } 85 }
NetWorkView
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections; 10 11 namespace VoidGame { 12 13 public class Server : MonoBehaviour { 14 15 private int port = 10100; 16 17 private string message = ""; 18 19 private Vector2 sc; 20 21 private void OnGUI() { 22 switch(Network.peerType) { 23 case NetworkPeerType.Disconnected: 24 StartServer(); 25 break; 26 case NetworkPeerType.Server: 27 OnServer(); 28 break; 29 case NetworkPeerType.Client: 30 31 break; 32 case NetworkPeerType.Connecting: 33 break; 34 default: 35 break; 36 } 37 } 38 39 /// <summary> 40 /// 启动服务器 41 /// </summary> 42 private void StartServer() { 43 if(GUILayout.Button("创建服务器")) { 44 NetworkConnectionError error = Network.InitializeServer(12,port,false); 45 switch(error) { 46 case NetworkConnectionError.NoError: 47 break; 48 default: 49 Debug.LogError("启动服务器失败"); 50 break; 51 } 52 } 53 } 54 55 /// <summary> 56 /// 服务器正在运行 57 /// </summary> 58 private void OnServer() { 59 GUILayout.Label("服务器已经运行,等待客户端连接"); 60 int length = Network.connections.Length; 61 for(int i = 0;i < length;i++) { 62 GUILayout.Label("客户端:" + i); 63 GUILayout.Label("客户端IP::" + Network.connections[i].ipAddress); 64 GUILayout.Label("客户端端口:" + Network.connections[i].port); 65 GUILayout.Label("==========="); 66 } 67 68 if(GUILayout.Button("断开服务器")) { 69 Network.Disconnect(); 70 } 71 72 sc = GUILayout.BeginScrollView(sc,GUILayout.Width(280),GUILayout.Height(400)); 73 GUILayout.Box(message); 74 GUILayout.EndScrollView(); 75 } 76 77 [RPC] 78 void ReceiveMessage(string msg,NetworkMessageInfo info) { 79 message = "发送端:" + info.sender + " 消息:" + msg; 80 } 81 } 82 }
1 /* 2 脚本名称: 3 脚本作者: 4 建立时间: 5 脚本功能: 6 版本号: 7 */ 8 using UnityEngine; 9 using System.Collections; 10 11 namespace VoidGame { 12 13 public class Client : MonoBehaviour { 14 15 private string IP = "127.0.0.1"; 16 17 int port = 10100; 18 19 string message = ""; 20 21 Vector2 sc; 22 23 void OnGUI() { 24 switch(Network.peerType) { 25 case NetworkPeerType.Disconnected: 26 StartConnect(); 27 break; 28 case NetworkPeerType.Server: 29 break; 30 case NetworkPeerType.Client: 31 OnClient(); 32 break; 33 case NetworkPeerType.Connecting: 34 break; 35 default: 36 break; 37 } 38 } 39 40 void StartConnect() { 41 if(GUILayout.Button("连接服务器")) { 42 NetworkConnectionError error = Network.Connect(IP,port); 43 switch(error) { 44 case NetworkConnectionError.NoError: 45 break; 46 default: 47 Debug.Log("客户端错误" + error); 48 break; 49 } 50 } 51 } 52 53 void OnClient() { 54 sc = GUILayout.BeginScrollView(sc,GUILayout.Width(280),GUILayout.Height(400)); 55 GUILayout.Box(message); 56 message = GUILayout.TextArea(message); 57 if(GUILayout.Button("发送")) { 58 GetComponent<NetworkView>().RPC("ReceiveMessage",RPCMode.All,message); 59 } 60 GUILayout.EndScrollView(); 61 } 62 63 [RPC] 64 void ReceiveMessage(string msg,NetworkMessageInfo info) { 65 message = "发送端" + info.sender + "消息" + msg; 66 } 67 } 68 }
photon
scut