C3204
2025-12-29 fec341de45f4b3fd1825807f0b3261143fa13caa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_SmartVisionCommon
{
    public class NetworkDiagnostics
    {
        public static async Task<NetworkStatus> CheckNetworkStatusAsync(string host = "www.google.com")
        {
            var status = new NetworkStatus();
 
            // 测试Ping
            status.CanPing = await TestPingAsync(host);
 
            // 测试HTTP
            status.CanAccessHttp = await TestHttpAsync($"https://{host}");
 
            // 测试常用端口
            status.CanAccessPort80 = await TestTcpPortAsync(host, 80);
            status.CanAccessPort443 = await TestTcpPortAsync(host, 443);
 
            status.IsNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
 
            return status;
        }
 
        public static async Task<NetworkStatus> CheckNetworkCanPingStatusAsync(string host = "www.google.com")
        {
            var status = new NetworkStatus();
 
            // 测试Ping
            status.CanPing = await TestPingAsync(host);
 
            //// 测试HTTP
            //status.CanAccessHttp = await TestHttpAsync($"https://{host}");
 
            //// 测试常用端口
            //status.CanAccessPort80 = await TestTcpPortAsync(host, 80);
            //status.CanAccessPort443 = await TestTcpPortAsync(host, 443);
 
            status.IsNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
 
            return status;
        }
 
        private static async Task<bool> TestPingAsync(string host)
        {
            try
            {
                using (var ping = new Ping())
                {
                    var reply = await ping.SendPingAsync(host, 3000);
                    return reply.Status == IPStatus.Success;
                }
            }
            catch
            {
                return false;
            }
        }
 
        private static async Task<bool> TestHttpAsync(string url)
        {
            try
            {
                using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(10) })
                {
                    var response = await client.GetAsync(url);
                    return response.IsSuccessStatusCode;
                }
            }
            catch
            {
                return false;
            }
        }
 
        private static async Task<bool> TestTcpPortAsync(string host, int port)
        {
            try
            {
                using (var client = new TcpClient())
                {
                    var task = client.ConnectAsync(host, port);
                    return await Task.WhenAny(task, Task.Delay(3000)) == task && client.Connected;
                }
            }
            catch
            {
                return false;
            }
        }
    }
 
    public class NetworkStatus
    {
        public bool IsNetworkAvailable { get; set; }
        public bool CanPing { get; set; }
        public bool CanAccessHttp { get; set; }
        public bool CanAccessPort80 { get; set; }
        public bool CanAccessPort443 { get; set; }
 
        public void PrintStatus()
        {
            Console.WriteLine($"网络接口可用: {IsNetworkAvailable}");
            Console.WriteLine($"Ping测试: {CanPing}");
            Console.WriteLine($"HTTP访问: {CanAccessHttp}");
            Console.WriteLine($"端口80: {CanAccessPort80}");
            Console.WriteLine($"端口443: {CanAccessPort443}");
        }
    }
}