프로젝트

일반

사용자정보

통계
| 개정판:

root / HAgent / Horizon(TCP, HTTP) / Horizon / Main.xaml.cs

이력 | 보기 | 이력해설 | 다운로드 (5.76 KB)

1
using System;
2
using System.IO;
3
using System.Net;
4
using System.Net.NetworkInformation;
5
using System.Net.Sockets;
6
using System.Text;
7
using System.Threading;
8
using System.Windows;
9
using System.Windows.Input;
10
using System.Windows.Threading;
11
using System.Xml;
12
using Newtonsoft.Json.Linq;
13

    
14
namespace Horizon
15
{
16
    public partial class Main : Window
17
    {
18
        
19
        string PCIP;
20
        string PCMac;
21

    
22
        int count = 0;
23
        private const int listenPort = 11000;
24
        bool check = true;
25
        Thread thread1, thread2;
26

    
27
        Updater updater = new Updater();
28
        Communicator commu = new Communicator();
29
        public Main()
30
        {
31
            //XMLCheck();
32
            // GetPcInfo();
33
            // this.Show();
34
            InitializeComponent();
35

    
36

    
37
            FileInfo xmlfile = new FileInfo("Setting.xml");
38
            WindowPositionSet();
39
            thread1 = new Thread(new ThreadStart(commu.Connect));
40
            thread1.Start();
41
            if (!xmlfile.Exists)
42
            {
43
                Console.WriteLine("없다.");
44
                GetMac();
45

    
46
                Communicator.state = "ready";
47
                this.Close();
48

    
49
            }
50
            else
51
            {
52
                Console.WriteLine("있다.");
53
                //thread = new Thread(new ThreadStart(commu.HttpConnect));
54
                //thread.Start();
55
                this.Close();
56
                
57
                updater.Show();
58
            }
59
            //commu.Connect();
60

    
61
        }
62

    
63
        public void MainFormClose()
64
        {
65
            this.Close();
66
        }
67
        //public void Connect() // UDP 패킷 수신
68
        //{
69
        //    try
70
        //    {
71
        //        UdpClient udp = new UdpClient();
72

    
73
        //        IPEndPoint localEP = new IPEndPoint(IPAddress.Any, listenPort);
74
        //        udp.Client.Bind(localEP);
75

    
76
        //        IPAddress multicastIP = IPAddress.Parse("224.1.1.1");
77
        //        udp.JoinMulticastGroup(multicastIP);
78

    
79
        //        IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, listenPort);
80

    
81
        //        while (check)
82
        //        {
83
        //            check = false;
84
        //            byte[] buff = udp.Receive(ref remoteEP);
85

    
86
        //            string data = Encoding.ASCII.GetString(buff, 0, buff.Length);
87
                    
88
        //            JObject obj = JObject.Parse(data);
89

    
90
        //            ServerIP= obj["IP"].ToString();
91
        //            ServerPort = obj["PORT"].ToString();
92

    
93
        //            Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
94
        //            {
95
        //                thread.Abort();
96
        //                HttpConnect();
97
        //                XMLCheck();
98
        //                ContentCheck Check = new ContentCheck();
99
        //                Check.Show();
100
        //                this.Close();
101

    
102
        //            }));
103
        //        }
104
        //    }
105
        //    catch (Exception e)
106
        //    {
107
        //        Console.WriteLine(e);
108
        //    }
109
        //}
110

    
111
        //public void HttpConnect() // http 전송 
112
        //{
113
        //    string url = "http://192.168.0.12:3000/aabb";
114
        //    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
115
        //    httpRequest.ContentType = "application/json";
116
        //    httpRequest.Method = "POST";
117
        //    httpRequest.Credentials = CredentialCache.DefaultCredentials;
118

    
119
        //    using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
120
        //    {
121
        //        string json = "{\"IP\":\""+PCIP+"\"," + "\"MAC\":\""+PCMac+"\"}";
122

    
123
        //        streamWriter.Write(json);
124
        //        streamWriter.Flush();
125
        //        streamWriter.Close();
126
        //    }
127

    
128
        //    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
129
        //    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
130
        //    {
131
        //        var result = streamReader.ReadToEnd();
132
        //        Console.WriteLine(result);
133
        //    }
134
        //}
135

    
136
        public void GetPcInfo() // pc의 ip주소 mac주소 저장
137
        {
138
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
139
            s.EnableBroadcast = true;
140

    
141
            IPAddress[] a = Dns.GetHostByName(Dns.GetHostName()).AddressList;
142

    
143
            PCIP = a[0].ToString();
144
            PCMac = MacAddress.Content.ToString(); 
145
        }
146
        
147
        public void WindowPositionSet() // 창 크기 조절
148
        {
149
            Width = SystemParameters.VirtualScreenWidth * ((SystemParameters.VirtualScreenWidth + SystemParameters.PrimaryScreenWidth) / (SystemParameters.PrimaryScreenWidth * 2));
150
            Height = SystemParameters.VirtualScreenHeight;
151
            WindowState = WindowState.Normal;
152
            WindowStyle = WindowStyle.None;
153
        }
154

    
155
        public void GetMac() // mac 주소 출력
156
        {
157
            NetworkInterface[] b = NetworkInterface.GetAllNetworkInterfaces();
158
            string mac = b[0].GetPhysicalAddress().ToString();
159

    
160
            int MacLength = mac.Length;
161
            for (int i = 1; i < MacLength; i++)
162
            {
163
                if (i % 2 == 0)
164
                {
165
                    mac = mac.Insert(i + count, "-");
166
                    count++;
167
                }
168
            }
169
            MacAddress.Content = mac;
170
        }
171
        
172
        private void ExitButton(object sender, MouseButtonEventArgs e)
173
        {
174
            Close();
175
        }
176

    
177
        private void Minimization(object sender, MouseButtonEventArgs e)
178
        {
179
            this.WindowState = WindowState.Minimized;
180
        }
181
    }
182
}