using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public struct mystructs
{
public int Age; //获取结构里的
public string name;
public float Height;
public mystructs(int myage, string myname, float myheight)
{
Age = myage;
name = myname;
Height = myheight;
}
}
public class UdpClient : MonoBehaviour
{
int ii;
int Age;
string Name;//获取
float Height;
private Socket newsock;//定义一个socket变量
IPEndPoint ip;//定义一个IP地址和端口号
int recv;//定义一个接受值的变量
byte[] data = new byte[1024];//定义一个二进制的数组用来获取客户端发过来的数据包
byte[] bytedate = new byte[1024];
string mydata;
void Start()
{
mystructs my; //将结构初始化 付给另一个
Age = my.Age = 17;
Name = my.name = " haha ";
Height = my.Height = 180.50f;
//Debug.Log(Age + Name + Height);
byte[] agebyte = new byte[1024]; //定义二进制数组
byte[] namebyte = new byte[1024];
byte[] heightbyte = new byte[1024];
agebyte = Encoding.ASCII.GetBytes(Age.ToString()); //转换成二进制的
namebyte = Encoding.ASCII.GetBytes(Name);
heightbyte = Encoding.ASCII.GetBytes(Height.ToString());
bytedate = new byte[agebyte.Length + namebyte.Length + heightbyte.Length]; //byte
agebyte.CopyTo(bytedate, 0); //把值 拷贝 给bytedate 索引
namebyte.CopyTo(bytedate, agebyte.Length); //agebyte索引开始
heightbyte.CopyTo(bytedate, namebyte.Length);
//string sss = Encoding.ASCII.GetString(data);
// Debug.Log(sss);
//得到本机IP,设置TCP端口号
ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);//设置自身的IP和端口号,在这里IPAddress.Any是自动获取本机IP
newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//实例化socket对象设置寻址方案为internetwork(IP版本的4存放),设置Soket的类型,为Dgram(支持数据报形式的数据),设置协议的类型,为UDP
//绑定网络地址
newsock.Bind(ip);//绑定IP
//Debug.Log("This is a Server,host name is " + Dns.GetHostName());//输出本地的名字
//Debug.Log("Waiting for a client");
Thread test = new Thread(BeginListening);//定义一个子线程
test.Start();//子线程开始
}
void BeginListening()
{
// IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);//实例化一个网络端点,设置为IPAddress.Any为自动获取跟我通讯的IP,0代表所有的地址都可以
EndPoint Remote = (EndPoint)(ip);//实例化一个地址结束点来标识网络路径
//Debug.Log(Encoding.ASCII.GetString(data, 0, recv));//输出二进制转换为string类型用来测试
while (true)
{
data = new byte[1024];//实例化data
recv = newsock.ReceiveFrom(data, ref Remote);//将数据包接收到的数据放入缓存点,并存储终节点
mydata = Encoding.ASCII.GetString(data, 0, recv);
//mydata=Encoding.Default.GetString(data);
//int length = mydata.Length;
//My_AVSpeed._instance.str = cutSubstring(mydata, length);
// print(cutSubstring(mydata, length));
Debug.Log(mydata);
ii = int.Parse(mydata);
/*if (ii == 01)
{
My_AVSpeed._instance.str1 = 1;
}
else if (ii == 00)
{
My_AVSpeed._instance.str1 = 0;
}
else if (ii == 11)
{
My_AVSpeed._instance.str2 = 1;
}
else if (ii == 10)
{
My_AVSpeed._instance.str2 = 0;
}*/
//My_AVSpeed._instance.str = int.Parse(mydata);
}
}
//string input = "";
//void SendMessage(string message)
//{
// byte[] data = new byte[1024];
// byte[] messbyte = new byte[1024];
// Debug.Log("This is a client,host name is " + Dns.GetHostName());
// ip = new IPEndPoint(IPAddress.Parse("192.168.1.120"), 12345);
// Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// EndPoint ep = (EndPoint)ip;
// string welcome = "你好";
// data = Encoding.ASCII.GetBytes(welcome);
// server.SendTo(data, data.Length, SocketFlags.None, ep);
// messbyte = Encoding.ASCII.GetBytes(message);
// data = new byte[bytedate.Length + messbyte.Length];
// bytedate.CopyTo(data, 0);
// messbyte.CopyTo(data, bytedate.Length);
// server.SendTo(data, ep);
// data = new byte[1024];
// Debug.Log("Stopping Client.");
// server.Close();
//}
//void OnGUI()
//{
// input = GUILayout.TextField(input);
// if (GUILayout.Button("发送"))
// {
// SendMessage(input);
// }
//}
void OnApplicationQuit()
{
newsock.Close();
}
//private static string cutSubstring(string str, int length)
//{
// if (str == null || str.Length == 0 || length < 0)
// {
// return "";
// }
// byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str);
// int n = 0; // 表示当前的字节数
// int i = 0; // 要截取的字节数
// for (; i < bytes.GetLength(0) && n < length; i++)
// {
// // 偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节
// if (i % 2 == 0)
// {
// n++; // 在UCS2第一个字节时n加1
// }
// else
// {
// // 当UCS2编码的第二个字节大于0时,该UCS2字符为汉字,一个汉字算两个字节
// if (bytes[i] > 0)
// {
// n++;
// }
// }
// }
// // 如果i为奇数时,处理成偶数
// if (i % 2 == 1)
// {
// // 该UCS2字符是汉字时,去掉这个截一半的汉字
// if (bytes[i] > 0)
// i = i - 1;
// // 该UCS2字符是字母或数字,则保留该字符
// else
// i = i + 1;
// }
// return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
//}
}
Unity------------Socket之UDP
最新推荐文章于 2024-06-27 15:28:09 发布