mirror of
				https://github.com/bolucat/Archive.git
				synced 2025-10-31 11:57:05 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			66 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Net;
 | |
| using System.Net.Sockets;
 | |
| 
 | |
| namespace Shadowsocks.Util.Sockets
 | |
| {
 | |
|     public static class SocketUtil
 | |
|     {
 | |
|         private class DnsEndPoint2 : DnsEndPoint
 | |
|         {
 | |
|             public DnsEndPoint2(string host, int port) : base(host, port)
 | |
|             {
 | |
|             }
 | |
| 
 | |
|             public DnsEndPoint2(string host, int port, AddressFamily addressFamily) : base(host, port, addressFamily)
 | |
|             {
 | |
|             }
 | |
| 
 | |
|             public override string ToString()
 | |
|             {
 | |
|                 return this.Host + ":" + this.Port;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         public static EndPoint GetEndPoint(string host, int port)
 | |
|         {
 | |
|             IPAddress ipAddress;
 | |
|             bool parsed = IPAddress.TryParse(host, out ipAddress);
 | |
|             if (parsed)
 | |
|             {
 | |
|                 return new IPEndPoint(ipAddress, port);
 | |
|             }
 | |
| 
 | |
|             // maybe is a domain name
 | |
|             return new DnsEndPoint2(host, port);
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public static void FullClose(this System.Net.Sockets.Socket s)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 s.Shutdown(SocketShutdown.Both);
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|             }
 | |
|             try
 | |
|             {
 | |
|                 s.Disconnect(false);
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|             }
 | |
|             try
 | |
|             {
 | |
|                 s.Close();
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|             }
 | |
|         }
 | |
|         
 | |
|     }
 | |
| }
 | 
![github-action[bot]](/assets/img/avatar_default.png)