Socket
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text; namespace Socket服务端 {
class Program {
static void Main(string[] args) { //1.创建一个Socket对象
Socket tcpServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//2.绑定一个IP和端口
//IPAddress ipAddress = new IPAddress(new byte[] { 127,0,0,1 });
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
EndPoint endPoint = new IPEndPoint(ipAddress,);
tcpServer.Bind(endPoint);
//3.开始监听客户端的连接请求
tcpServer.Listen();
Console.WriteLine("服务器启动完成");
Socket clientSocket = tcpServer.Accept();
Console.WriteLine("接收到客户端的连接请求!");
//4.发送/接收消息
string sendMessage = "Hello Client";
//将字符串转换为字节数组
byte[] sendData = Encoding.UTF8.GetBytes(sendMessage);
clientSocket.Send(sendData);
Console.WriteLine("服务器向客户端发送了一条消息:" + sendMessage); //接收客户端消息
byte[] receiveData = new byte[];
int length = clientSocket.Receive(receiveData);
string receiveMessage = Encoding.UTF8.GetString(receiveData);
Console.WriteLine("服务器接收到客户端发送过来的消息:" + receiveMessage); Console.Read();
}
}
}
SocketServer
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text; namespace VoidGame { public class SocketClient : MonoBehaviour { private Socket tcpClient;
private string serverIP = "127.0.0.1";
private int serverPort = ; void Start() {
//1.创建一个Socket;
tcpClient = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//2.建立连接请求
IPAddress ipAddress = IPAddress.Parse(serverIP);
EndPoint endPoint = new IPEndPoint(ipAddress,serverPort);
tcpClient.Connect(endPoint);
Debug.Log("请求服务器连接");
//3.接受/发送消息
byte[] receiveData = new byte[];
int length = tcpClient.Receive(receiveData);
string receiveMessage = Encoding.UTF8.GetString(receiveData,,length);
Debug.Log("客户端接收到服务器发来的消息:" + receiveMessage); //发送消息
string sendMessage = "Client Say To Server Hello";
tcpClient.Send(Encoding.UTF8.GetBytes(sendMessage));
Debug.Log("客户端向服务器发送消息:" + sendMessage);
}
}
}
SocketClient
Json
xml
protobuf
www
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections;
using System.IO; namespace VoidGame { public enum GetPicType {
Download = ,
LocalLoad =
} public class Picture : MonoBehaviour { private string url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3525821899,4147777390&fm=21&gp=0.jpg"; /// <summary>
/// 从网络下载的图片
/// </summary>
private Texture2D img = null; /// <summary>
/// 从本地读取的图片
/// </summary>
private Texture2D img2 = null; private bool downloadOK = false; void OnGUI() {
if(img != null) {
GUI.DrawTexture(new Rect(,,,),img);
}
if(img2 != null) {
GUI.DrawTexture(new Rect(,,,),img2);
}
if(GUI.Button(new Rect(,,,),"从网络加载图片")) {
StartCoroutine(DownloadTexture(url,GetPicType.Download));
}
if(GUI.Button(new Rect(,,,),"从本地加载图片")) {
if(downloadOK) {
StartCoroutine(DownloadTexture("file://"+Application.streamingAssetsPath+"/dota2.png",GetPicType.LocalLoad));
} else {
Debug.LogError("没有下载的图片");
}
}
} IEnumerator DownloadTexture(string url,GetPicType getPicType) {
WWW www = new WWW(url);
yield return www; Texture2D tempImage = null;
if(www.isDone && www.error == null) {
switch(getPicType) {
case GetPicType.Download:
img = www.texture;
tempImage = img;
Debug.Log(tempImage.width + " " + tempImage.height);
break;
case GetPicType.LocalLoad:
img2 = www.texture;
tempImage = img2;
Debug.Log(tempImage.width + " " + tempImage.height);
break;
default:
tempImage = null;
break;
}
}
if(tempImage != null) {
byte[] data = tempImage.EncodeToPNG();
File.WriteAllBytes(Application.streamingAssetsPath + "/dota2.png",data);
downloadOK = true;
}
}
}
}
Picture
NetWorkView
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections; namespace VoidGame { public class Server : MonoBehaviour { private int port = ; private string message = ""; private Vector2 sc; private void OnGUI() {
switch(Network.peerType) {
case NetworkPeerType.Disconnected:
StartServer();
break;
case NetworkPeerType.Server:
OnServer();
break;
case NetworkPeerType.Client: break;
case NetworkPeerType.Connecting:
break;
default:
break;
}
} /// <summary>
/// 启动服务器
/// </summary>
private void StartServer() {
if(GUILayout.Button("创建服务器")) {
NetworkConnectionError error = Network.InitializeServer(,port,false);
switch(error) {
case NetworkConnectionError.NoError:
break;
default:
Debug.LogError("启动服务器失败");
break;
}
}
} /// <summary>
/// 服务器正在运行
/// </summary>
private void OnServer() {
GUILayout.Label("服务器已经运行,等待客户端连接");
int length = Network.connections.Length;
for(int i = ;i < length;i++) {
GUILayout.Label("客户端:" + i);
GUILayout.Label("客户端IP::" + Network.connections[i].ipAddress);
GUILayout.Label("客户端端口:" + Network.connections[i].port);
GUILayout.Label("===========");
} if(GUILayout.Button("断开服务器")) {
Network.Disconnect();
} sc = GUILayout.BeginScrollView(sc,GUILayout.Width(),GUILayout.Height());
GUILayout.Box(message);
GUILayout.EndScrollView();
} [RPC]
void ReceiveMessage(string msg,NetworkMessageInfo info) {
message = "发送端:" + info.sender + " 消息:" + msg;
}
}
}
Server
/*
脚本名称:
脚本作者:
建立时间:
脚本功能:
版本号:
*/
using UnityEngine;
using System.Collections; namespace VoidGame { public class Client : MonoBehaviour { private string IP = "127.0.0.1"; int port = ; string message = ""; Vector2 sc; void OnGUI() {
switch(Network.peerType) {
case NetworkPeerType.Disconnected:
StartConnect();
break;
case NetworkPeerType.Server:
break;
case NetworkPeerType.Client:
OnClient();
break;
case NetworkPeerType.Connecting:
break;
default:
break;
}
} void StartConnect() {
if(GUILayout.Button("连接服务器")) {
NetworkConnectionError error = Network.Connect(IP,port);
switch(error) {
case NetworkConnectionError.NoError:
break;
default:
Debug.Log("客户端错误" + error);
break;
}
}
} void OnClient() {
sc = GUILayout.BeginScrollView(sc,GUILayout.Width(),GUILayout.Height());
GUILayout.Box(message);
message = GUILayout.TextArea(message);
if(GUILayout.Button("发送")) {
GetComponent<NetworkView>().RPC("ReceiveMessage",RPCMode.All,message);
}
GUILayout.EndScrollView();
} [RPC]
void ReceiveMessage(string msg,NetworkMessageInfo info) {
message = "发送端" + info.sender + "消息" + msg;
}
}
}
Client
photon
scut