总共三个脚本
@第一个(工具类脚本)单例
using?System.Collections;
using?System.Collections.Generic;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System;
///?
///?工具类
///?
public?class?NetWorkManager??{
????#region?单例
????private?static?NetWorkManager?instance;
????public?static?NetWorkManager?Instance?{
????????get?{
????????????if?(instance?==?null)?{
????????????????instance?=?new?NetWorkManager?();
????????????}
????????????return?instance;
????????}
????}
????#endregion
????public?delegate?void?MyCallBack(string?msg);
????#region?服务器端
????/*
?????*?服务器创建流程
?????*?1、创建服务器
?????*?2、等待客户端连接
????*/
????/*
????*?使用到Socket的哪些方法
????*?1、创建套接字
????*?2、绑定IP地址和端口
????*?3、监听客户端连接的方法
????*?4、接收客户端请求
????*?5、收发信息
????*/
????///?
????///?服务端的套接字对象
????///?
????private?Socket?serverSocket;
????///?
????///?服务端的输入输出缓冲区
????///?
????private?byte[]?serverBuffer?=?new?byte[1024];
????///?
????///?服务端的委托
????///?
????private?MyCallBack?serverCallBack;
????///?
????///?初始化服务器
????///?
????///?外界传进来的委托方法
????public?void?ServerInit(MyCallBack?userCallBack)?{
????????serverCallBack?=?userCallBack;
????????//?创建
????????serverSocket?=?new?Socket?(
????????????AddressFamily.InterNetwork,?
????????????SocketType.Stream,?
????????????ProtocolType.Tcp
????????);
????????//?绑定
????????IPEndPoint?point?=?new?IPEndPoint(IPAddress.Parse("127.0.0.1"),12345);
????????serverSocket.Bind?(point);
????????//?开始监听
????????serverSocket.Listen(10);
????????serverCallBack?("服务器启动成功");
????????//?接受客户端的请求
????????serverSocket.BeginAccept(ServerAccept,serverSocket);
????}
????///?
????///?当服务器接受到客户端请求的回调事件
????///?
????///?Ar.
????void?ServerAccept(IAsyncResult?ar)?{
????????serverSocket?=?ar.AsyncState?as?Socket;
????????//?接受结束
????????Socket?workingSocket?=?serverSocket.EndAccept(ar);
????????//?开始基于新的Socket进行收发信息
????????workingSocket.BeginReceive?(
????????????//?收发缓冲区
????????????serverBuffer,
????????????//?起始位置
????????????0,
????????????//?接受数据的的长度
????????????serverBuffer.Length,
????????????//?特殊服务
????????????SocketFlags.None,
????????????//?当接收到信息的回调委托
????????????ServerReceive,
????????????//?当前的套接字状态
????????????workingSocket
????????);
????????serverSocket.BeginAccept?(ServerAccept,?serverSocket);
????}
????///?
????///?服务器接收到客户端的消息的委托回调
????///?
????///?Ar.
????void?ServerReceive(IAsyncResult?ar)?{
????????Socket?workingSocket?=?ar.AsyncState?as?Socket;
????????//?接收到的字节数据
????????int?count?=?workingSocket.EndReceive(ar);
????????string?result?=?UTF8Encoding.UTF8.GetString?(serverBuffer,0,count);
????????serverCallBack?(result);
????????workingSocket.BeginReceive?(
????????????//?收发缓冲区
????????????serverBuffer,
????????????//?起始位置
????????????0,
????????????//?接受数据的的长度
????????????serverBuffer.Length,
????????????//?特殊服务
????????????SocketFlags.None,
????????????//?当接收到信息的回调委托
????????????ServerReceive,
????????????//?当前的套接字状态
????????????workingSocket
????????);
????}
????#endregion
????#region?客户端
????///?
????///?客户端的套接字
????///?
????Socket?clientSocket;
????///?
????///?客户端的缓冲区
????///?
????byte[]?clientBuffer?=?new?byte[1024];
????///?
????///?客户端的委托
????///?
????MyCallBack?clientCallBack;
????///?
????///??开始连接服务器
????///?
????///?IP地址
????///?端口号
????///?委托事件
????public?void?ClientConnect(string?ip,?int?port,?MyCallBack?userCallBack)?{
????????clientSocket?=?new?Socket?(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
????????clientSocket.Connect?(IPAddress.Parse(ip),port);
????????clientCallBack?=?userCallBack;
????????clientCallBack?("客户端连接成功");
????}?
????///?
????///??客户端发送消息的回调事件
????///?
????///?Message.
????public?void?ClientSendMessage(string?message)?{
????????clientBuffer?=?UTF8Encoding.UTF8.GetBytes?(message);
????????clientSocket.BeginSend(
????????????clientBuffer,
????????????0,
????????????message.Length,
????????????SocketFlags.None,
????????????SendSuccess,
????????????clientSocket
????????);
????}
????//消息发送成功的回调方法
????void?SendSuccess(IAsyncResult?ar)?{
????????Socket?workingSocket?=?ar.AsyncState?as?Socket;
????????workingSocket.EndSend?(ar);
????}
????#endregion
}
@第二个脚本(服务器)
using?System.Collections;
using?System.Collections.Generic;
using?UnityEngine;
using?UnityEngine.UI;
public?class?Server?:?MonoBehaviour?{
????Text?text;
????void?Awake(){
????????text?=?GameObject.Find?("Text").GetComponent?();
????????Application.runInBackground?=?true;
????}
????void?Start?()?{
????????NetWorkManager.Instance.ServerInit?(
????????????(string?msg)?=>?{
????????????????if?(msg?==?"1")?{
????????????????????msg?=?"开启";
????????????????}else{
????????????????????msg?=?"关闭";????????????????
????????????????}
????????????????text.text?=?msg;
????????????}
????????);
????}
}
@第三脚本(客户端)
using?System.Collections;
using?System.Collections.Generic;
using?UnityEngine;
using?UnityEngine.UI;
public?class?Client?:?MonoBehaviour?{
????Button?send; // 发送指令的按钮
??? InputField?text; ? // 发送的指令
????void?Awake(){
????????Application.runInBackground?=?true; // 后台运行
????????send?=?GameObject.Find?("Button").GetComponent?();
????????text?=?GameObject.Find?("InputField").GetComponent?();
????????send.onClick.AddListener?(SendMessage);
????}
????void?Start?()?{
????????NetWorkManager.Instance.ClientConnect?(
????????????"127.0.0.1",
????????????12345,
????????????(string?msg)?=>?{
????????????}
????????);
????}
????void?SendMessage(){
????????NetWorkManager.Instance.ClientSendMessage?(text.text);
????}
}
3
如下效果图
如果想写及时通讯(聊天对话框),直接修改一些代码即可。如有不当,多多指教!?