实现步骤
1.在Unity项目中新建UDP通信脚本,代码如下所示: `using UnityEngine; using System.Collections; //引入库 using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Collections.Generic; using UnityEngine.UI;
public class Udp : MonoBehaviour { //以下默认都是私有的成员 //Socket socketSend; //发送socket UdpClient socketSend; IPEndPoint ipSend; //客户端端口
Socket
socketReceive
;
//接收socket
IPEndPoint
ipReceive
;
//服务端端口
List
<
EndPoint
>
clientEnds
;
//客户端
string
recvStr
;
//接收的字符串
string
sendStr
;
//发送的字符串
byte
[
]
recvData
=
new
byte
[
1024
]
;
//接收的数据,必须为字节
byte
[
]
sendData
=
new
byte
[
1024
]
;
//发送的数据,必须为字节
int
recvLen
;
//接收的数据长度
Thread
connectThread
;
//连接线程
//输入框
public
InputField
thisField
;
//初始化
void
InitSocket
(
)
{
//定义连接的服务器ip和端口,可以是本机ip,局域网,互联网
ipSend
=
new
IPEndPoint
(
IPAddress
.

最低0.47元/天 解锁文章
872

被折叠的 条评论
为什么被折叠?



