客户端 接收端 :
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;public class UDPServer : MonoBehaviour
{//以下默认都是私有的成员Socket socket;//目标socketEndPoint clientEnd;//客户端IPEndPoint ipEnd;//侦听端口public string recvStr; //接收的字符串string sendStr;//发送的字符串byte[] recvData = new byte[1024];//接收的数据,必须为字节byte[] sendData = new byte[1024];//发送的数据,必须为字节int recvLen;//接收的数据长度Thread connectThread;//连接线程// Start is called before the first frame updatevoid Start(){InitSocket();}private void OnApplicationQuit(){SocketQuit();}// Update is called once per framevoid Update(){//监听接受数据switch (recvStr){//接收到的数据case ""://事件break;}}void InitSocket(){// 定义侦听端口,侦听任何IP 自定义端口号和服务端保持一致ipEnd = new IPEndPoint(IPAddress.Any, 8883);//定义套接字类型,在主线程中定义socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//服务端需要绑定ipsocket.Bind(ipEnd);//定义客户端IPEndPoint sender = new IPEndPoint(IPAddress.Any, 8880);//自定义端口号 发送给别的某个电脑的 这台电脑的端口号clientEnd = (EndPoint)sender;//print("waiting for UDP dgram");//开启一个线程连接,必须的,否则主线程卡死connectThread = new Thread(new ThreadStart(SocketReceive));connectThread.Start();}public void SocketSend(string sendStr){//清空发送缓存sendData = new byte[1024];//数据类型转换sendData = Encoding.UTF8.GetBytes(sendStr);//发送给指定客户端socket.SendTo(sendData, sendData.Length, SocketFlags.None, clientEnd);}void SocketReceive(){//进入接收循环while (true){//对data清零recvData = new byte[1024];//获取客户端,获取客户端数据,用引用给客户端赋值recvLen = socket.ReceiveFrom(recvData, ref clientEnd);//print("message from:" + clientEnd.ToString());//打印客户端信息// 输出接收到的数据recvStr = Encoding.ASCII.GetString(recvData, 0, recvLen);print("我是服务器,接收到客户端的数据" + recvStr);//将接收到的数据经过处理再发送出去sendStr = "From Server333: " + recvStr;SocketSend(sendStr);//ProcessInformation(recvStr);// Debug.Log(sendStr);}}//连接关闭void SocketQuit(){//关闭线程if (connectThread != null){connectThread.Interrupt();connectThread.Abort();}// 最后关闭socketif (socket != null)socket.Close();print("disconnect-1");}}
服务器 发送端 :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
//服务器 发送端
public class UDPClient : MonoBehaviour
{public string recvStr;private string UDPClientIP;Socket socket;EndPoint serverEnd;IPEndPoint ipEnd;byte[] recvData = new byte[1024];byte[] sendData = new byte[1024];int recvLen = 0;Thread connectThread;private void Awake(){}void Start(){//IP地址UDPClientIP = "192.168.1.16";//自己电脑的ip地址UDPClientIP = UDPClientIP.Trim();InitSocket();}//ipEnd = new IPEndPoint(IP地址,端口号);//这里用json读取配置方便外部修改void InitSocket(){ipEnd = new IPEndPoint(IPAddress.Parse(UDPClientIP), 8880);// 后面的端口是接收端的端口号socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);serverEnd = (EndPoint)sender;//开启一个线程连接connectThread = new Thread(new ThreadStart(SocketReceive));connectThread.Start();}//可以绑定Button发送string类型信息public void SocketSend(string sendStr){//清空sendData = new byte[1024];//数据转换sendData = Encoding.UTF8.GetBytes(sendStr);//发送给指定服务端socket.SendTo(sendData, sendData.Length, SocketFlags.None, ipEnd);}//接收服务器信息void SocketReceive(){while (true){recvData = new byte[1024];try{recvLen = socket.ReceiveFrom(recvData, ref serverEnd);}catch (Exception e){}//print("信息来自: " + serverEnd.ToString());if (recvLen > 0){recvStr = Encoding.UTF8.GetString(recvData, 0, recvLen);print(recvStr);}}}//连接关闭void SocketQuit(){//关闭线程if (connectThread != null){connectThread.Interrupt();connectThread.Abort();}//最后关闭socketif (socket != null)socket.Close();}void OnApplicationQuit(){SocketQuit();}void Update(){if (Input.GetKeyDown(KeyCode.K)){SocketSend("ddddd");}}}