ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
 
namespace Common.NetWork
{
    public class TcpIP
    {
        /// <summary>
        /// ip‚æ‚èA–¢Žg—pƒ|[ƒg‚̎擾
        /// (1024`65535)
        /// </summary>
        /// <param name="ip4"></param>
        /// <returns>0:Žæ“¾Ž¸”s</returns>
        public static int GetFreePort(string ip4)
        {
            for (int i = 1024; i < 65535; i++)
            {
                if (IsFreePort(ip4, i)) { return i; }
            }
            return 0;
        }
 
        /// <summary>
        /// Žw’肵‚½ƒ|[ƒg‚ÍŽg—p‰Â”\‚©‚Ç‚¤‚©
        /// </summary>
        /// <param name="ip4"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public static bool IsFreePort(string ip4, int port)
        {
            using (TcpClient tc = new TcpClient())
            {
                try
                {
                    tc.Connect(ip4, port);
                    return true;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }
            return false;
        }
 
        /// <summary> Ž©•ªIP‚̎擾
        /// </summary>
        /// <returns></returns>
        public static List<string> GetLocalIpv4()
        {
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            List<string> IpCollection = new List<string>();
            foreach (IPAddress ip in localIPs)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork) { IpCollection.Add(ip.ToString()); }
            }
            return IpCollection;
        }
 
        /// <summary> Ž©•ªIP‚̎擾
        /// </summary>
        /// <returns></returns>
        public static string GetFirstLocalIpv4()
        {
            //192.168.—DæIII
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            SortedList<string, string> IpCollection = new SortedList<string, string>();
            foreach (IPAddress ip in localIPs)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork) { IpCollection.Add(string.Format("{0}-{1}", ip.ToString().StartsWith("192") ? "0" : "1", ip.ToString()), ip.ToString()); }
            }
            return IpCollection.Count == 0 ? string.Empty : IpCollection.Values[0];
        }
 
        /// <summary> Ž©•ªIP‚̎擾
        /// </summary>
        /// <returns></returns>
        public static IPAddress GetFirstLocalIp4Address()
        {
            //192.168.—DæIII
            IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
            SortedList<string, IPAddress> IpCollection = new SortedList<string, IPAddress>();
            foreach (IPAddress ip in localIPs)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork) { IpCollection.Add(string.Format("{0}-{1}", ip.ToString().StartsWith("192") ? "0" : "1", ip.ToString()), ip); }
            }
            return IpCollection.Count == 0 ? null : IpCollection.Values[0];
        }
    }
}