110,534
社区成员
发帖
与我相关
我的任务
分享
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Net.NetworkInformation;
using UnityEngine.UI;
using System.Security.Cryptography;
public class UDPServer : MonoBehaviour
{
static UdpClient udpclient;
static IPEndPoint ipend;
static byte[] getmessage;
public Text text;
bool receivebool,see;
static Thread receivethread;
static string getip;
void Start ()
{
text = GameObject.Find("Text").GetComponent<Text>();
text.text = "创建服务器";
Receive();
}
void Update ()
{
if (see)
{
string a = Encoding.UTF8.GetString(getmessage);
text.text = a;
text.color = new Color(UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f));
Testsend();
see = false;
}
if (Input.GetKeyDown(KeyCode.Escape))
{
receivebool = false;
receivethread.Abort();
text.text = "结束";
}
}
/// <summary>
/// 接收
/// </summary>
public void Receive()
{
udpclient = new UdpClient(new IPEndPoint(IPAddress.Any, 45));
receivethread = new Thread(Receivethreadfunction);
receivebool = true;
receivethread.Start();
text.text = "开始接收";
}
void Receivethreadfunction()
{
ipend = new IPEndPoint(IPAddress.Any, 0);
while (receivebool)
{
try
{
getmessage = udpclient.Receive(ref ipend);
getip = ipend.ToString();
see = true;
}
catch
{
Thread.Sleep(1);
}
}
}
/// <summary>
/// 回发
/// </summary>
public void Testsend()
{
UdpClient udpc = new UdpClient();
byte[] date = Encoding.UTF8.GetBytes("hello client");
udpc.Send(date, date.Length, ipend);
text.text = ipend.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System;
using System.Net.NetworkInformation;
using UnityEngine.UI;
using System.IO;
public class UDPClient : MonoBehaviour
{
static UdpClient udpclient;
static byte[] getmessage;
static bool getbool,changeindex;
static Thread getthread;
public Text text;
void Start ()
{
text = GameObject.Find("Text").GetComponent<Text>();
Testreceive();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.D))
{
//text.text = getstring.Count.ToString();
text.text = "创建客户端";
Sendtest();
}
if (Input.GetKeyDown(KeyCode.Escape))
{
getbool = false;
getthread.Abort();
text.text = "结束";
}
if (changeindex)
{
text.text = Encoding.UTF8.GetString(getmessage);
changeindex = false;
}
}
/// <summary>
/// 发送
/// </summary>
void Sendtest()
{
UdpClient udpc = new UdpClient();
string t = UnityEngine.Random.Range(0, 10).ToString();
byte[] date = Encoding.UTF8.GetBytes(t);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("39.108.191.141"), 45);
udpc.Send(date, date.Length, ipe);
text.text = t;
text.color = new Color(UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f), UnityEngine.Random.Range(0.0f, 1.0f));
}
/// <summary>
/// 接收
/// </summary>
void Testreceive()
{
text.text = "zero";
udpclient = new UdpClient(new IPEndPoint(IPAddress.Any, 8080));
text.text = "one";
getthread = new Thread(Testthread);
text.text = "two";
getbool = true;
text.text = "three";
getthread.Start();
text.text = "four";
}
void Testthread()
{
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 8080);
while (getbool)
{
try
{
Debug.Log("five");
getmessage = udpclient.Receive(ref ipe);
changeindex = true;
Debug.Log("ok");
}
catch
{
Thread.Sleep(1);
}
}
}
}