271 lines
7.4 KiB
C#
271 lines
7.4 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine;
|
||
|
||
public class NetMain : SingleTon<NetMain>
|
||
{
|
||
private Socket socket;
|
||
private IPEndPoint ipe;
|
||
|
||
private string ip;
|
||
private int port;
|
||
|
||
public Socket conn()
|
||
{
|
||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||
ip = "122.112.171.137";
|
||
port = 85;
|
||
ipe = new IPEndPoint(IPAddress.Parse(ip), port);
|
||
socket.Connect(ipe);
|
||
if (socket.Connected)
|
||
{
|
||
Debug.Log("Á¬½Ó³É¹¦");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("Á¬½Óʧ°Ü");
|
||
}
|
||
return socket;
|
||
}
|
||
|
||
public Socket Create(string serverAddress, int nPort)
|
||
{
|
||
string connectionHost = serverAddress;
|
||
string convertedHost = string.Empty;
|
||
AddressFamily convertFamily = AddressFamily.InterNetwork;
|
||
|
||
if (IsIPAddress(serverAddress))
|
||
{
|
||
GetIPType(serverAddress, out convertedHost, out convertFamily);
|
||
if (string.IsNullOrEmpty(convertedHost))
|
||
{
|
||
connectionHost = convertedHost;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
convertedHost = GetIPAddress(serverAddress, EAddressFam.IPv6);
|
||
if (string.IsNullOrEmpty(convertedHost))
|
||
{
|
||
convertedHost = GetIPAddress(serverAddress, EAddressFam.IPv4);
|
||
}
|
||
else
|
||
{
|
||
convertFamily = AddressFamily.InterNetworkV6;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(convertedHost))
|
||
{
|
||
return null;
|
||
}
|
||
else
|
||
{
|
||
connectionHost = convertedHost;
|
||
}
|
||
}
|
||
|
||
IPAddress[] ipAddressArray = Dns.GetHostAddresses(connectionHost);
|
||
if (ipAddressArray.Length <= 0)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
IPAddress ipAddress = ipAddressArray[0];
|
||
AddressFamily addressFamily = AddressFamily.InterNetwork;
|
||
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||
{
|
||
addressFamily = AddressFamily.InterNetworkV6;
|
||
}
|
||
|
||
Socket socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
|
||
{
|
||
SendTimeout = 3000,
|
||
ReceiveTimeout = 3000,
|
||
SendBufferSize = 64 * 1024,
|
||
ReceiveBufferSize = 64 * 1024,
|
||
NoDelay = true
|
||
};
|
||
|
||
return socket;
|
||
}
|
||
|
||
#region IPv6
|
||
|
||
#if (UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR
|
||
private static extern string getIPv6(string host);
|
||
#endif
|
||
|
||
string GetIPv6(string host)
|
||
{
|
||
#if (UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR
|
||
return getIPv6(host);
|
||
#else
|
||
return host + "&&ipv4";
|
||
#endif
|
||
}
|
||
|
||
void GetIPType(string serverIp, out string newServerIp, out AddressFamily IPType)
|
||
{
|
||
IPType = AddressFamily.InterNetwork;
|
||
newServerIp = serverIp;
|
||
try
|
||
{
|
||
string ipv6 = GetIPv6(serverIp);
|
||
if (!string.IsNullOrEmpty(ipv6))
|
||
{
|
||
string[] tmp = System.Text.RegularExpressions.Regex.Split(ipv6, "&&");
|
||
if (null != tmp && tmp.Length >= 2)
|
||
{
|
||
string type = tmp[1];
|
||
if (type == "ipv6")
|
||
{
|
||
newServerIp = tmp[0];
|
||
IPType = AddressFamily.InterNetworkV6;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
|
||
string GetIPAddress(string hostName, EAddressFam af)
|
||
{
|
||
//»ù´¡²Ù×÷ϵͳºÍÍøÂçÊÊÅäÆ÷ÊÇ·ñÖ§³Ö internet ÐÒéµÄ°æ±¾ 6(ipv6)
|
||
if (af == EAddressFam.IPv6 && !Socket.OSSupportsIPv6)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(hostName))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
string connetIP = string.Empty;
|
||
IPHostEntry host;
|
||
|
||
try
|
||
{
|
||
host = Dns.GetHostEntry(hostName);
|
||
foreach (var ip in host.AddressList)
|
||
{
|
||
if (af == EAddressFam.IPv4)
|
||
{
|
||
if (ip.AddressFamily == AddressFamily.InterNetwork)
|
||
{
|
||
connetIP = ip.ToString();
|
||
}
|
||
}
|
||
else if (af == EAddressFam.IPv6)
|
||
{
|
||
if (ip.AddressFamily == AddressFamily.InterNetworkV6)
|
||
{
|
||
connetIP = ip.ToString();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError("GetIPAddress error : " + e.Message);
|
||
}
|
||
|
||
return connetIP;
|
||
}
|
||
//ÅжÏstrÊÇÓòÃû»¹ÊÇip
|
||
bool IsIPAddress(string str)
|
||
{
|
||
Match match = Regex.Match(str, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
|
||
return match.Success;
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
//NetMain net = new NetMain("122.112.171.137", 443);
|
||
//Socket _tcpClient = NetMain.Instance.conn();
|
||
|
||
//byte[] data = new byte[1024];
|
||
//int length = _tcpClient.Receive(data);
|
||
//var message = Encoding.UTF8.GetString(data, 0, length);
|
||
//Debug.Log("¿Í»§¶ËÊÕµ½À´×Ô·þÎñÆ÷·¢À´µÄÐÅÏ¢" + message);
|
||
|
||
|
||
//IPAddress ip2 = IPAddress.Parse("122.112.171.137");//ËùÓÃÍø¿¨
|
||
//int port2 = 443;//¼àÌý¶Ë¿Ú
|
||
//IPEndPoint ipe2 = new IPEndPoint(ip2, port2);
|
||
//EndPoint ep2 = (EndPoint)ipe2;
|
||
//_tcpClient.Bind(ep2);
|
||
|
||
//string msg = "/api/cases/base/list";
|
||
//byte[] buffer = Encoding.Unicode.GetBytes(msg);//½«×Ö·û´®×ª»¯Îª¶þ½øÖÆ
|
||
//_tcpClient.Send(buffer);
|
||
|
||
//TcpClient client = new TcpClient();//clientÊÇ·þÎñ¶ËµÄ±äÁ¿
|
||
//try
|
||
//{
|
||
// //Óë·þÎñ¶Ë½¨Á¢Ò»¸öTCPÁ¬½Ó£¬Á¬½ÓºÃÒԺ󣬿ͻ§¶Ë¾Í»áµÈ´ý·þÎñ¶Ë·¢À´µÄÊý¾Ý
|
||
// client.Connect(IPAddress.Parse("122.112.171.137"), 443);
|
||
//}
|
||
//catch (Exception ex)
|
||
//{
|
||
// Debug.Log("¿Í»§¶ËÁ¬½ÓÒì³££º" + ex.Message);
|
||
//}
|
||
////¿Í»§¶Ë½ÓÊÕ·þÎñ¶Ë·¢Ë͵ÄÊý¾Ý²¿·Ö
|
||
//var socket = NetMain.Instance;
|
||
//const int bufferSize = 8792;
|
||
//NetworkStream streamToClient = client.GetStream();//»ñµÃÀ´×Ô·þÎñ¶ËµÄÁ÷
|
||
//byte[] buffer = new byte[bufferSize];//¶¨ÒåÒ»¸ö»º´æbufferÊý×é
|
||
//int byteRead = streamToClient.Read(buffer, 0, bufferSize);//½«Êý¾Ý´æÈ뻺´æÖÐ
|
||
|
||
//string msg = Encoding.Unicode.GetString(buffer, 0, byteRead);//´Ó¶þ½øÖÆ×ª»»Îª×Ö·û´®¶ÔÓ¦µÄ¿Í»§¶Ë»áÓдÓ×Ö·û´®×ª»»Îª¶þ½øÖƵķ½·¨
|
||
//Debug.Log("½ÓÊÕÏûÏ¢£º" + msg);
|
||
|
||
|
||
//Socket _tcpClient = NetMain.Instance.conn();
|
||
|
||
//string msg = "/api/cases/base/list";
|
||
//byte[] buffer = Encoding.Unicode.GetBytes(msg);//½«×Ö·û´®×ª»¯Îª¶þ½øÖÆ
|
||
//_tcpClient.Send(buffer);
|
||
//string url = "http://122.112.171.137:8080/api/cases/base/list";
|
||
//StartCoroutine(PostRequest(url));
|
||
|
||
//StartCoroutine(Get(url));
|
||
|
||
//streamToClient.Write(buffer, 0, buffer.Length);//½«×ª»»ºÃµÄ¶þ½øÖÆÊý¾ÝдÈë¿Í»§¶ËµÄÁ÷Öв¢·¢ËÍ
|
||
|
||
|
||
//IEnumerator PostRequest(string url)
|
||
//{
|
||
// //ËùÌá½»µÄ±íµ¥
|
||
// WWWForm form = new WWWForm();
|
||
// //¼ÓÉÏÇëÇó²ÎÊý£¬Èç²ÎÊýÃû¡°Content-Type",ÄÚÈÝ¡±application/json¡°
|
||
// //form.AddField("Content-Type", "application/json");
|
||
|
||
// //·¢ËÍPostÇëÇó
|
||
// using (UnityWebRequest webRequest = UnityWebRequest.Post(url, form))
|
||
// {
|
||
// yield return webRequest.SendWebRequest();
|
||
// if (!string.IsNullOrEmpty(webRequest.error))
|
||
// {
|
||
// Debug.LogError(webRequest.error);
|
||
// }
|
||
// else
|
||
// {
|
||
// //»ñµÃ·µ»ØÊý¾Ý
|
||
// Debug.Log(webRequest.downloadHandler.text);
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
enum EAddressFam : byte
|
||
{
|
||
IPv4,
|
||
IPv6
|
||
} |