You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
5.6 KiB
185 lines
5.6 KiB
2 years ago
|
using QFramework;
|
||
|
using UnityEngine;
|
||
|
using System;
|
||
|
using System.Text;
|
||
|
using System.Net.Sockets;
|
||
|
using System.IO;
|
||
|
using System.Threading.Tasks;
|
||
|
using System.Threading;
|
||
|
using System.Net.NetworkInformation;
|
||
|
|
||
|
namespace TCPClientTools
|
||
|
{
|
||
|
public class TCPUtility : IUtility
|
||
|
{
|
||
|
public string tcpAddress;
|
||
|
public int tcpPort;
|
||
|
public TcpClient tcpClient;
|
||
|
public NetworkStream sendStream;
|
||
|
public BindableProperty<bool> isOpenTCP = new BindableProperty<bool>(false);
|
||
|
public bool isReceivedValue = false;
|
||
|
public BindableProperty<string> receivedData = new BindableProperty<string>("");
|
||
|
public BindableProperty<string> exceptionData = new BindableProperty<string>("");
|
||
|
|
||
|
public Thread reciveT;
|
||
|
public BindableProperty<bool> isTimeOut = new BindableProperty<bool>(false);
|
||
|
|
||
|
public TCPUtility() {
|
||
|
Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务");
|
||
|
|
||
|
}
|
||
|
public TCPUtility(string tcpAddress, int tcpPort)
|
||
|
{
|
||
|
this.tcpAddress = tcpAddress;
|
||
|
this.tcpPort = tcpPort;
|
||
|
this.isTimeOut.Value = false;
|
||
|
StartTCPClient();
|
||
|
}
|
||
|
public bool StartTCPClient()
|
||
|
{
|
||
|
isTimeOut.Value = false;
|
||
|
if (!isOpenTCP)
|
||
|
{
|
||
|
try {
|
||
|
tcpClient = new TcpClient(tcpAddress, tcpPort);
|
||
|
sendStream = tcpClient.GetStream();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
exceptionData.Value = e.ToString();
|
||
|
return false;
|
||
|
}
|
||
|
isOpenTCP.Value = true;
|
||
|
reciveT = new Thread(RecciveMsg);
|
||
|
reciveT.IsBackground = true;
|
||
|
reciveT.Start();
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
public bool StartTCPClient(string ip, int port) {
|
||
|
isTimeOut.Value = false;
|
||
|
if (!isOpenTCP) {
|
||
|
tcpAddress = ip;
|
||
|
tcpPort = port;
|
||
|
try
|
||
|
{
|
||
|
tcpClient = new TcpClient(tcpAddress, tcpPort);
|
||
|
sendStream = tcpClient.GetStream();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
exceptionData.Value = e.ToString();
|
||
|
return false;
|
||
|
}
|
||
|
isOpenTCP.Value = true;
|
||
|
reciveT = new Thread(RecciveMsg);
|
||
|
reciveT.IsBackground = true;
|
||
|
reciveT.Start();
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void CloseTCPClient() {
|
||
|
if (tcpClient.Connected) {
|
||
|
sendStream.Close();
|
||
|
tcpClient.Close();
|
||
|
isOpenTCP.Value = false;
|
||
|
reciveT.Abort();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void sendData(string data)
|
||
|
{
|
||
|
if (isOpenTCP == false)
|
||
|
return;
|
||
|
byte[] sendBytes = Encoding.Default.GetBytes(data);
|
||
|
sendStream.Write(sendBytes, 0, sendBytes.Length);
|
||
|
}
|
||
|
|
||
|
public void RecciveMsg()
|
||
|
{
|
||
|
byte[] receiveBuff = new byte[1024];
|
||
|
int reviceLength = 0;
|
||
|
string msg = "";
|
||
|
while (isOpenTCP.Value)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
reviceLength = tcpClient.Client.Receive(receiveBuff);
|
||
|
msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength);
|
||
|
if (msg != "")
|
||
|
{
|
||
|
receivedData.Value = msg;
|
||
|
isReceivedValue = true;
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Debug.LogWarning(e);
|
||
|
//断线发送异常
|
||
|
isTimeOut.Value = true;
|
||
|
exceptionData.Value = e.ToString();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
Debug.Log("------------end While-------------");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 线程接收到消息后
|
||
|
/// </summary>
|
||
|
/// <returns>接收到的消息</returns>
|
||
|
public string getReceivedValue() {
|
||
|
if (isReceivedValue)
|
||
|
{
|
||
|
isReceivedValue = false;
|
||
|
return receivedData.Value;
|
||
|
}
|
||
|
else {
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public bool getTimeOutState() {
|
||
|
return isTimeOut;
|
||
|
}
|
||
|
|
||
|
public bool IsReceivedMsg() {
|
||
|
return sendStream.Length != 0;
|
||
|
}
|
||
|
public bool IsConnected() {
|
||
|
return tcpClient.Connected;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取本机IP
|
||
|
/// </summary>
|
||
|
/// <returns>string :ip地址</returns>
|
||
|
public string GetIP()
|
||
|
{
|
||
|
string output = "";
|
||
|
|
||
|
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
|
||
|
{
|
||
|
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; //无线局域网适配器
|
||
|
|
||
|
if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up)
|
||
|
{
|
||
|
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
|
||
|
{
|
||
|
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
||
|
{
|
||
|
output = ip.Address.ToString();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return output;
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
}
|