123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.IO;
- using System.Threading;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- public class clientTCP : MonoBehaviour
- {
- private string staInfo = "NULL"; //状态信息
- private string inputIp = "192.168.50.201"; //输入ip地址
- private string inputPort = "18180"; //输入端口号
- public string Message = "NULL"; //发送的消息
- private int recTimes = 0; //接收到信息的次数
- private string recMes = "NULL"; //接收到的消息
- private Socket socketSend; //客户端套接字,用来链接远端服务器
- private bool clickSend = false; //是否点击发送按钮
- private bool isConnect = true;
- private float times = 0;
- private string ipPath;
- public Queue<String> queueRecMsg;
- void Start()
- {
- /*
- ipPath = Application.persistentDataPath + "/Ipconfig/" + "ip.txt";
- Debug.Log(ipPath);
- StartCoroutine( SettingIP());*/
- // ClickConnect();
- queueRecMsg = new Queue<string>();
- }
- public float dmaxx = 1;
- public float dmaxy = 1;
- float maxx;
- float minx;
- float maxy;
- float miny;
- float xx;
- float yy;
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.A))
- {
- SendMessage();
- }
- if (Input.GetKeyDown(KeyCode.S))
- {
- SendMessage2();
- }
- if (Input.GetKeyDown(KeyCode.D))
- {
- SendMessage3();
- }
- Debug.Log("x的长度为:minx," + minx + "_maxx" + maxx);
- Debug.Log("y的长度为:miny," + miny + "_maxy" + maxy);
- Debug.Log("当前长度:x," + xx + "_y" + yy);
- //float ctx = xx / maxx;
- //float cty = yy / maxy;
- float fx = 0;
- float fy = 0;
- if(minx<0)
- {
- fx = minx;
- }
- if(miny<0)
- {
- fy = miny;
- }
- float zctx = Mathf.Abs(minx) + maxx;
- float zcty = Mathf.Abs(miny) + maxy;
- float curx = Mathf.Abs(fx) - Mathf.Abs(xx);//xx + minx;
- if (xx > 0)
- {
- curx = xx + Mathf.Abs(fx);
- }
- else
- {
- }
- if (maxx < 0)
- {
- curx = Mathf.Abs(xx) + maxx;
- }
- float cury = Mathf.Abs(yy);//xx + minx;
- if (yy > 0)
- {
- cury = yy + Mathf.Abs(fy);
- }
- if(maxy<0)
- {
- cury = Mathf.Abs(yy)+ maxy;
- }
- float ctx = curx / zctx;
- float cty = cury / zcty;
- if(minx!=0)
- img.transform.localPosition = new Vector3(0,(-(cty* dmaxy - dmaxy))/2f, (dmaxx * ctx - dmaxx / 2f));
- }
- IEnumerator SettingIP()
- {
- yield return new WaitForSeconds(0.2f);
- if (File.Exists(ipPath))
- {
- var webRequest = UnityWebRequest.Get(ipPath);
- yield return webRequest.SendWebRequest();
- if (webRequest.error != null)
- {
- Debug.Log("通信中");
- yield return webRequest;
- }
- var downloadHandler = webRequest.downloadHandler;
- if (!downloadHandler.isDone)
- {
- Debug.Log("下载中");
- yield return downloadHandler;
- }
- else
- {
- Debug.Log("下载成功");
- Debug.Log(downloadHandler.data);
- // if (!File.Exists(Application.dataPath + "/Resources/YourVideoName.mp4"))
- inputIp = System.Text.Encoding.Default.GetString(downloadHandler.data);
- // ClickConnect();
- }
- }
- else
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/Ipconfig");
- FileInfo fileInfo = new FileInfo(ipPath);
- //设置Log文件输出地址
- FileStream FileWriter = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
- UTF8Encoding encoding = new UTF8Encoding();
- FileWriter.Write(encoding.GetBytes("192.168.50.211"), 0, encoding.GetByteCount("192.168.50.211"));
- FileWriter.Close();
- Application.Quit();
- }
- }
- //建立链接
- public void ClickConnect(string ipurl)
- {
- return;
- Debug.Log("ClickConnect");
- isConnect = true;
- try
- {
- int _port = 8000;//Convert.ToInt32(inputPort); //获取端口号
- string _ip = ipurl;//"192.168.1.109"; //获取ip地址
- Debug.Log("ClickConnect1 " + _ip + _port);
- //创建客户端Socket,获得远程ip和端口号
- socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress ip = IPAddress.Parse(_ip);
- IPEndPoint point = new IPEndPoint(ip, _port);
- Debug.Log("ClickConnect2");
- socketSend.Connect(point);
- Debug.Log("连接成功 , " + " ip = " + ip + " port = " + _port);
- staInfo = ip + ":" + _port + " 连接成功";
- Thread r_thread = new Thread(Received); //开启新的线程,不停的接收服务器发来的消息
- r_thread.IsBackground = true;
- r_thread.Start();
- Thread s_thread = new Thread(SendMessage); //开启新的线程,不停的给服务器发送消息
- s_thread.IsBackground = true;
- s_thread.Start();
- }
- catch (Exception e)
- {
- Debug.LogError(e.ToString());
- Debug.Log("IP或者端口号错误......");
- staInfo = "IP或者端口号错误......";
- isConnect = false;
- }
- }
- public GameObject img;
- byte[] cx = new byte[2];
- /// <summary>
- /// 接收服务端返回的消息
- /// </summary>
- void Received()
- {
- while (true)
- {
- try
- {
- byte[] buffer = new byte[23];
- //实际接收到的有效字节数
- int len = socketSend.Receive(buffer);
- if (len == 0)
- {
- // isConnect = false;
- break;
- }
- // Debug.Log("curx==:" + buffer[3]);
- switch (buffer[3])
- {
- case 0x00:
- break;
- case 0x01:
- break;
- case 0x02:
- break;
- case 0x03:
- float af = 0;
- cx[0] = buffer[4];
- cx[1] = buffer[5];
- // Debug.Log("curx4==:" + buffer[4]);
- // Debug.Log("curx5==:" + buffer[5]);
- af = byteArrayToshort(cx, 0);
- // Debug.Log("minx==:" + af);
- minx = af;
- cx[0] = buffer[6];
- cx[1] = buffer[7];
- af = byteArrayToshort(cx, 0);
- maxx = af;
- // Debug.Log("maxx==:" + af);
- cx[0] = buffer[8];
- cx[1] = buffer[9];
- af = byteArrayToshort(cx, 0);
- Debug.Log("curx==:" + af);
- xx = af;
- byte[] cy = new byte[2];
- cy[0] = buffer[10];
- cy[1] = buffer[11];
- af = byteArrayToshort(cy, 0);
- miny = af;
- // Debug.Log("miny==:" + af);
- cy[0] = buffer[12];
- cy[1] = buffer[13];
- af = byteArrayToshort(cy, 0);
- maxy = af;
- // Debug.Log("maxy==:" + af);
- cy[0] = buffer[14];
- cy[1] = buffer[15];
- af = byteArrayToshort(cy, 0);
- Debug.Log("cury==:" + af);
- yy = af;
- break;
- }
- /*
- Debug.Log("客户端接收到的数据 : " );
- recMes = Encoding.UTF8.GetString(buffer, 0, len);
- Debug.Log("客户端接收到的数据 : " + recMes);
- recTimes++;
- staInfo = "接收到一次数据,接收次数为 :" + recTimes;
- // Debug.Log("接收次数为:" + recTimes);
- */
- // queueRecMsg.Enqueue(recMes);
- }
- catch { }
- }
- }
- public void SendData()
- {
- }
- /// <summary>
- /// 向服务器发送消息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void SendMessage()
- {
- Debug.Log("发送数据");
- byte[] buffer = new byte[5];
- buffer[0] = 0x3F;
- buffer[1] = 0x02;
- buffer[2] = 0x05;
- buffer[3] = 0x03;
- buffer[4] = 0x3F;
- socketSend.Send(buffer);
- }
- /// <summary>
- /// 向服务器发送消息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void SendMessage2()
- {
- Debug.Log("发送数据");
- byte[] buffer = new byte[4];
- buffer[0] = 0x3F;
- buffer[1] = 0x01;
- buffer[2] = 0x01;
- buffer[3] = 0x3F;
- socketSend.Send(buffer);
- }
- /// <summary>
- /// 向服务器发送消息
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void SendMessage3()
- {
- Debug.Log("发送数据");
- byte[] buffer = new byte[5];
- buffer[0] = 0x3F;
- buffer[1] = 0x02;
- buffer[2] = 0x05;
- buffer[2] = 0x00;
- buffer[3] = 0x3F;
- socketSend.Send(buffer);
- }
- private short byteArrayToshort(byte[] b, int offset)
- {
- // throw new NotImplementedException();
- short s = 0;
- short s0 = (short)(b[1] & 0xff);// 最低位
- short s1 = (short)(b[0] & 0xff);
- s1 <<= 8;
- s = (short)(s0 | s1);
- return s;
- }
- private void OnDisable()
- {
- Debug.Log("begin OnDisable()");
- if (socketSend.Connected)
- {
- try
- {
- socketSend.Shutdown(SocketShutdown.Both); //禁用Socket的发送和接收功能
- socketSend.Close(); //关闭Socket连接并释放所有相关资源
- }
- catch (Exception e)
- {
- print(e.Message);
- }
- }
- Debug.Log("end OnDisable()");
- }
- //用户界面
- //void OnGUI()
- //{
- // //GUI.color = Color.black;
- // //GUI.Label(new Rect(65, 10, 60, 20), "状态信息");
- // //GUI.Label(new Rect(135, 10, 80, 60), staInfo);
- // //GUI.Label(new Rect(65, 70, 50, 20), "服务器ip地址");
- // //inputIp = GUI.TextField(new Rect(125, 70, 100, 20), inputIp, 20);
- // //GUI.Label(new Rect(65, 110, 50, 20), "服务器端口");
- // //inputPort = GUI.TextField(new Rect(125, 110, 100, 20), inputPort, 20);
- // //GUI.Label(new Rect(65, 150, 80, 20), "接收到消息:");
- // //GUI.Label(new Rect(155, 150, 80, 20), recMes);
- // //GUI.Label(new Rect(65, 190, 80, 20), "发送的消息:");
- // //Message = GUI.TextField(new Rect(155, 190, 100, 20), Message, 20);
- // //if (GUI.Button(new Rect(65, 270, 300, 100), "发送信息"))
- // //{
- // // clickSend = true;
- // //}
- //}
- }
|