首页 > 其他分享 >unity3d:最简单的服务器,把收到消息发回客户端

unity3d:最简单的服务器,把收到消息发回客户端

时间:2022-11-01 11:10:25浏览次数:79  
标签:unity3d Socket System 发回 myThread myClientSocket new using 客户端


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

public class Server: MonoBehaviour {

private static byte[] result = new byte[1024];
static Socket serverSocket;
Thread myThread;

void Start()
{
//服务器IP地址
IPAddress ip = IPAddress.Parse(GameConst.IP);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, GameConst.Port)); //绑定IP地址:端口
serverSocket.Listen(10); //设定最多10个排队连接请求
//通过Clientsoket发送数据
myThread = new Thread(ListenClientConnect);
myThread.Start();
Console.ReadLine();
}

/// <summary>
/// 监听客户端连接
/// </summary>
private void ListenClientConnect()
{
while (true)
{
Socket clientSocket = serverSocket.Accept();
Thread receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
//Debug.Log(clientSocket.RemoteEndPoint.ToString());
}
}

/// <summary>
/// 接收消息
/// </summary>
/// <param name="clientSocket"></param>
private static void ReceiveMessage(object clientSocket)
{
Socket myClientSocket = (Socket)clientSocket;
while (true)
{
try
{
int receiveNumber = myClientSocket.Receive(result);
if (receiveNumber > 0)
{
myClientSocket.Send(result, receiveNumber,0);
}
}
catch(Exception ex)
{
//Console.WriteLine(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
break;
}
}
}

void OnApplicationQuit()
{

if (myThread != null)
{
myThread.Abort();
myThread = null;
}
}


}


标签:unity3d,Socket,System,发回,myThread,myClientSocket,new,using,客户端
From: https://blog.51cto.com/u_15544328/5812289

相关文章

  • unity3d:ui跟着gameobject移动
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassUiFollowObj:MonoBehaviour{Transformm_trans;publicTransform......
  • unity3d:protobuf .java转.cs
    服务器端定义好protobuf结构,放unity编辑器中生成.cs的结构usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;usingSystem......
  • unity3d:复制选中物体transform信息到剪切板
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;publicclassSaveTransInfo:EditorWindow{[MenuItem("SaveTrans......
  • unity3d:ugui 每个字间隔间距
    usingUnityEngine;usingSystem.Collections;usingUnityEngine.UI;usingSystem;usingSystem.Collections.Generic;publicclassLine{privateint_startVertexInde......
  • Unity3D :Mob SMSSDK 运行崩溃
    报错信息android.content.ActivityNotFoundException:Unabletofindexplicitactivityclass{com.shuiying.smsm09061/com.mob.tools.MobUIShell};haveyoudeclaredt......
  • unity3d:显示FPS
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassFPSShow:MonoBehaviour{privatevoidOnGUI(){stringte......
  • unity3d:编辑器脚本,替换选中物体的材质
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;publicclassChangeMat:MonoBehaviour{[MenuItem("Tools/Chang......
  • unity3d:xlua 加载自定义Loader
    在xLua加自定义loader是很简单的,只涉及到一个接口:publicdelegatebyte[]CustomLoader(refstringfilepath);publicvoidLuaEnv.AddLoader(CustomLoaderloader)通过A......
  • unity3d:xlua hotfix 官方例子
    1.新建工程,xlua文件夹与Plugins文件夹放入assets,tools放assets同级目录2.添加宏:HOTFIX_ENABLE3.D:\WorkSoft\unity2017.2.0\Editor\Data\Managed3个文件Unity.Cecil.dll,U......
  • python tcp多个客户端连接服务器
    一、传输层**该层为两台主机上的应用程序提供端到端的通信。传输层有两个传输协议:TCP(传输控制协议)和UDP(用户数据报协议)。其中,TCP是一个可靠的面向连接的协议,udp是不可......