123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using UnityEngine;
- using System.IO;
- using System.Net.NetworkInformation;
- using BeinLab.Util;
- public class server : MonoBehaviour
- {
-
- private string info = "NULL";
- private string recMes = "NULL";
- private int recTimes = 0;
- private string inputIp = "192.168.50.17";
- private string inputPort = "18180";
- private string Message = "NULL";
- private Socket socketWatch;
- private Socket socketSend;
- private bool isSendData = false;
- private bool clickConnectBtn = false;
- public Queue<String> queueRecMsg;
-
- void Start()
- {
-
- inputIp = GetIP();
- ClickConnect();
- TimerMgr.Instance.CreateTimer(() => { SendData(); }, 0.1f, -1);
- queueRecMsg = new Queue<string>();
-
- }
-
- void Update()
- {
- }
-
-
-
-
-
- public string GetIP()
- {
- string output = "";
- IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
- foreach (IPAddress ip in ips)
- {
- if (ip.AddressFamily == AddressFamily.InterNetwork)
- {
- Debug.Log( ip.ToString());
- output = ip.ToString();
- }
- else if (ip.AddressFamily == AddressFamily.InterNetworkV6)
- {
- Debug.Log(ip.ToString());
- output = ip.ToString();
- }
-
- }
- return output;
- }
-
- private void ClickConnect()
- {
- try
- {
- int _port = Convert.ToInt32(inputPort);
- string _ip = inputIp;
- Debug.Log(" ip 地址是 :" + _ip);
- Debug.Log(" 端口号是 :" + _port);
- clickConnectBtn = true;
- info = "ip地址是 : " + _ip + "端口号是 : " + _port;
-
- socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPAddress ip = IPAddress.Parse(inputIp);
- IPEndPoint point = new IPEndPoint(ip, _port);
- socketWatch.Bind(point);
- Debug.Log("监听成功!");
- info = "监听成功";
- socketWatch.Listen(10);
-
- Thread thread = new Thread(Listen);
- thread.IsBackground = true;
- thread.Start(socketWatch);
- }
- catch { }
- }
-
-
-
- void Listen(object o)
- {
- try
- {
- Socket socketWatch = o as Socket;
- while (true)
- {
- socketSend = socketWatch.Accept();
- Debug.Log(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!");
- info = socketSend.RemoteEndPoint.ToString() + " 连接成功!";
- Thread r_thread = new Thread(Received);
- r_thread.IsBackground = true;
- r_thread.Start(socketSend);
- Thread s_thread = new Thread(SendMessage);
- s_thread.IsBackground = true;
- s_thread.Start(socketSend);
- }
- }
- catch { }
- }
-
- void Received(object o)
- {
- try
- {
- Socket socketSend = o as Socket;
- while (true)
- {
- byte[] buffer = new byte[1024 * 6];
- int len = socketSend.Receive(buffer);
- if (len == 0)
- {
- break;
- }
- string str = Encoding.UTF8.GetString(buffer, 0, len);
- Debug.Log("接收到的消息:" + socketSend.RemoteEndPoint + ":" + str);
- recMes = str;
- recTimes++;
- info = "接收到一次数据,接收次数为:" + recTimes;
-
- queueRecMsg.Enqueue(recMes);
-
- }
- }
- catch { }
- }
- public void SendData()
- {
- if (socketSend != null)
- Debug.Log("sendData " + SocketExtensions.IsConnected(socketSend));
- if (socketSend!=null&& SocketExtensions.IsConnected(socketSend))
- {
- if(NetWorkLANManager.Instance.createDesMsg.Count>0)
- {
- Message = NetWorkLANManager.Instance.createDesMsg.Dequeue();
- isSendData = true;
- return;
- }
- else if (NetWorkLANManager.Instance.queueMsg.Count > 0)
- {
- Message = NetWorkLANManager.Instance.queueMsg.Dequeue();
- isSendData = true;
- }
-
- }
- }
-
- void SendMessage(object o)
- {
- try
- {
- Socket socketSend = o as Socket;
- while (true)
- {
-
- if (isSendData)
- {
- isSendData = false;
- byte[] sendByte = Encoding.UTF8.GetBytes(Message);
- Debug.Log("发送的数据为 :" + Message);
- Debug.Log("发送的数据字节长度 :" + sendByte.Length);
- socketSend.Send(sendByte);
- }
- }
- }
- catch { }
- }
-
- private void OnDisable()
- {
- Debug.Log("begin OnDisable()");
- if (clickConnectBtn)
- {
- try
- {
- socketWatch.Shutdown(SocketShutdown.Both);
- socketWatch.Close();
- socketSend.Shutdown(SocketShutdown.Both);
- socketSend.Close();
- }
- catch (Exception e)
- {
- Debug.Log(e.Message);
- }
- }
- Debug.Log("end OnDisable()");
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
|