using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class UDPClient : MonoBehaviour
{
public string recvStr;
private string UDPClientIP;
string str = "客户端01发送消息";
Socket socket;
EndPoint serverEnd;
IPEndPoint ipEnd;
byte[] recvData = new byte[1024];
byte[] sendData = new byte[1024];
int recvLen = 0;
Thread connectThread;
void Start()
{
UDPClientIP = "168.178.2.11";//服务端的IP.自己更改
UDPClientIP = UDPClientIP.Trim();
InitSocket();
}
void InitSocket()
{
ipEnd = new IPEndPoint(IPAddress.Parse(UDPClientIP), 7401);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
serverEnd = (EndPoint)sender;
print("等待连接");
SocketSend(str);
print("连接");
//开启一个线程连接
connectThread = new Thread(new ThreadStart(SocketReceive));
connectThread.Start();
}
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();
}
//最后关闭socket
if (socket != null)
socket.Close();
}
void OnApplicationQuit()
{
SocketQuit();
}
void Update()
{
}
}
客户端
using UnityEngine;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class UDPServer : MonoBehaviour
{
public string ipAddress = "168.178.2.11";
public int ConnectPort = 7401;
public string recvStr;
Socket socket;
EndPoint clientEnd;
IPEndPoint ipEnd;
string sendStr;
byte[] recvData = new byte[1024];
byte[] sendData = new byte[1024];
int recvLen;
Thread connectThread;
//初始化
void InitSocket()
{
ipEnd = new IPEndPoint(IPAddress.Parse(ipAddress), ConnectPort);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(ipEnd);
//定义客户端
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
clientEnd = (EndPoint)sender;
print("等待连接数据");
//开启一个线程连接
connectThread = new Thread(new ThreadStart(SocketReceive));
connectThread.Start();
}
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)
{
recvData = new byte[1024];
recvLen = socket.ReceiveFrom(recvData, ref clientEnd);
recvStr = Encoding.UTF8.GetString(recvData, 0, recvLen);
Debug.Log("收到得信息 " + recvStr);
}
}
//连接关闭
void SocketQuit()
{
//关闭线程
if (connectThread != null)
{
connectThread.Interrupt();
connectThread.Abort();
}
//最后关闭socket
if (socket != null)
socket.Close();
Debug.LogWarning("断开连接");
}
// Use this for initialization
void Start()
{
InitSocket(); //在这里初始化server
}
void OnApplicationQuit()
{
SocketQuit();
}
}