Files @ ae8f7007cb0e
Branch filter:

Location: seniordesign-ui/Demo.WindowsForms/Source/Program.cs

ethanzonca@CL-ENS241-08.cedarville.edu
Added licensing information
  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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;

namespace Demo.WindowsForms
{
   class Program
   {
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      /// 
       // Instantiate the communications port 
       private SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
       static MainForm windowGUI;

      [STAThread]
      static void Main()
      {
          Application.SetCompatibleTextRenderingDefault(false);
          var program = new Program();
          windowGUI = new MainForm();
          program.SerialInitialize();
         Application.EnableVisualStyles();

         Application.Run(windowGUI);
      }

      //inits the serial port and event handler
      private void SerialInitialize()
      {
          // http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx
          // Attach a method to be called when there is data waiting in the port's buffer
          port.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);

          // Open the port for communications
          port.Open();

      }

      //process received data
      private void ReceiveData(object sender, SerialDataReceivedEventArgs e)
      {
          // Show all the incoming data in the port's buffer
          string testChk = port.ReadLine();
          windowGUI.ParseIncomingData(testChk);
      }
   }

   public class Dummy
   {

   }

   class IpInfo
   {
      public string Ip;
      //public int Port;
      //public TcpState State;
      //public string ProcessName;

      public string CountryName;
      public string RegionName;
      public string City;
      public double Latitude;
      public double Longitude;
      public DateTime CacheTime;

      //public DateTime StatusTime;
      //public bool TracePoint;
   }

   struct IpStatus
   {
      private string countryName;
      public string CountryName
      {
         get
         {
            return countryName;
         }
         set
         {
            countryName = value;
         }
      }

      private int connectionsCount;
      public int ConnectionsCount
      {
         get
         {
            return connectionsCount;
         }
         set
         {
            connectionsCount = value;
         }
      }
   }

   class DescendingComparer : IComparer<IpStatus>
   {
      public bool SortOnlyCountryName = false;

      public int Compare(IpStatus x, IpStatus y)
      {
         int r = 0;

         if(!SortOnlyCountryName)
         {
            r = y.ConnectionsCount.CompareTo(x.ConnectionsCount);
         }

         if(r == 0)
         {
            return x.CountryName.CompareTo(y.CountryName);
         }
         return r;
      }
   }

   class TraceRoute
   {
      readonly static string Data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
      readonly static byte[] DataBuffer;
      readonly static int timeout = 8888;

      static TraceRoute()
      {
         DataBuffer = Encoding.ASCII.GetBytes(Data);
      }

      public static List<PingReply> GetTraceRoute(string hostNameOrAddress)
      {
         var ret = GetTraceRoute(hostNameOrAddress, 1);

         return ret;
      }

      private static List<PingReply> GetTraceRoute(string hostNameOrAddress, int ttl)
      {
         List<PingReply> result = new List<PingReply>();

         using(Ping pinger = new Ping())
         {
            PingOptions pingerOptions = new PingOptions(ttl, true);

            PingReply reply = pinger.Send(hostNameOrAddress, timeout, DataBuffer, pingerOptions);

            //Debug.WriteLine("GetTraceRoute[" + hostNameOrAddress + "]: " + reply.RoundtripTime + "ms " + reply.Address + " -> " + reply.Status);

            if(reply.Status == IPStatus.Success)
            {
               result.Add(reply);
            }
            else if(reply.Status == IPStatus.TtlExpired)
            {
               // add the currently returned address
               result.Add(reply);

               // recurse to get the next address...
               result.AddRange(GetTraceRoute(hostNameOrAddress, ttl + 1));
            }
            else
            {
               Debug.WriteLine("GetTraceRoute: " + hostNameOrAddress + " - " + reply.Status);
            }
         }

         return result;
      }
   }


#if !MONO
   #region Managed IP Helper API

   public struct TcpTable : IEnumerable<TcpRow>
   {
      #region Private Fields

      private IEnumerable<TcpRow> tcpRows;

      #endregion

      #region Constructors

      public TcpTable(IEnumerable<TcpRow> tcpRows)
      {
         this.tcpRows = tcpRows;
      }

      #endregion

      #region Public Properties

      public IEnumerable<TcpRow> Rows
      {
         get
         {
            return this.tcpRows;
         }
      }

      #endregion

      #region IEnumerable<TcpRow> Members

      public IEnumerator<TcpRow> GetEnumerator()
      {
         return this.tcpRows.GetEnumerator();
      }

      #endregion

      #region IEnumerable Members

      IEnumerator IEnumerable.GetEnumerator()
      {
         return this.tcpRows.GetEnumerator();
      }

      #endregion
   }

   public struct TcpRow
   {
      #region Private Fields

      private IPEndPoint localEndPoint;
      private IPEndPoint remoteEndPoint;
      private TcpState state;
      private int processId;

      #endregion

      #region Constructors

      public TcpRow(IpHelper.TcpRow tcpRow)
      {
         this.state = tcpRow.state;
         this.processId = tcpRow.owningPid;

         int localPort = (tcpRow.localPort1 << 8) + (tcpRow.localPort2) + (tcpRow.localPort3 << 24) + (tcpRow.localPort4 << 16);
         long localAddress = tcpRow.localAddr;
         this.localEndPoint = new IPEndPoint(localAddress, localPort);

         int remotePort = (tcpRow.remotePort1 << 8) + (tcpRow.remotePort2) + (tcpRow.remotePort3 << 24) + (tcpRow.remotePort4 << 16);
         long remoteAddress = tcpRow.remoteAddr;
         this.remoteEndPoint = new IPEndPoint(remoteAddress, remotePort);
      }

      #endregion

      #region Public Properties

      public IPEndPoint LocalEndPoint
      {
         get
         {
            return this.localEndPoint;
         }
      }

      public IPEndPoint RemoteEndPoint
      {
         get
         {
            return this.remoteEndPoint;
         }
      }

      public TcpState State
      {
         get
         {
            return this.state;
         }
      }

      public int ProcessId
      {
         get
         {
            return this.processId;
         }
      }

      #endregion
   }

   public static class ManagedIpHelper
   {
      public static readonly List<TcpRow> TcpRows = new List<TcpRow>();

      #region Public Methods

      public static void UpdateExtendedTcpTable(bool sorted)
      {
         TcpRows.Clear();

         IntPtr tcpTable = IntPtr.Zero;
         int tcpTableLength = 0;

         if(IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, sorted, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidConnections, 0) != 0)
         {
            try
            {
               tcpTable = Marshal.AllocHGlobal(tcpTableLength);
               if(IpHelper.GetExtendedTcpTable(tcpTable, ref tcpTableLength, true, IpHelper.AfInet, IpHelper.TcpTableType.OwnerPidConnections, 0) == 0)
               {
                  IpHelper.TcpTable table = (IpHelper.TcpTable)Marshal.PtrToStructure(tcpTable, typeof(IpHelper.TcpTable));

                  IntPtr rowPtr = (IntPtr)((long)tcpTable + Marshal.SizeOf(table.Length));
                  for(int i = 0; i < table.Length; ++i)
                  {
                     TcpRows.Add(new TcpRow((IpHelper.TcpRow)Marshal.PtrToStructure(rowPtr, typeof(IpHelper.TcpRow))));
                     rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(typeof(IpHelper.TcpRow)));
                  }
               }
            }
            finally
            {
               if(tcpTable != IntPtr.Zero)
               {
                  Marshal.FreeHGlobal(tcpTable);
               }
            }
         }
      }

      #endregion
   }

   #endregion

   #region P/Invoke IP Helper API

   /// <summary>
   /// <see cref="http://msdn2.microsoft.com/en-us/library/aa366073.aspx"/>
   /// </summary>
   public static class IpHelper
   {
      #region Public Fields

      public const string DllName = "iphlpapi.dll";
      public const int AfInet = 2;

      #endregion

      #region Public Methods

      /// <summary>
      /// <see cref="http://msdn2.microsoft.com/en-us/library/aa365928.aspx"/>
      /// </summary>
      [DllImport(IpHelper.DllName, SetLastError = true)]
      public static extern uint GetExtendedTcpTable(IntPtr tcpTable, ref int tcpTableLength, bool sort, int ipVersion, TcpTableType tcpTableType, int reserved);

      #endregion

      #region Public Enums

      /// <summary>
      /// <see cref="http://msdn2.microsoft.com/en-us/library/aa366386.aspx"/>
      /// </summary>
      public enum TcpTableType
      {
         BasicListener,
         BasicConnections,
         BasicAll,
         OwnerPidListener,
         OwnerPidConnections,
         OwnerPidAll,
         OwnerModuleListener,
         OwnerModuleConnections,
         OwnerModuleAll,
      }

      #endregion

      #region Public Structs

      /// <summary>
      /// <see cref="http://msdn2.microsoft.com/en-us/library/aa366921.aspx"/>
      /// </summary>
      [StructLayout(LayoutKind.Sequential)]
      public struct TcpTable
      {
         public uint Length;
         public TcpRow row;
      }

      /// <summary>
      /// <see cref="http://msdn2.microsoft.com/en-us/library/aa366913.aspx"/>
      /// </summary>
      [StructLayout(LayoutKind.Sequential)]
      public struct TcpRow
      {
         public TcpState state;
         public uint localAddr;
         public byte localPort1;
         public byte localPort2;
         public byte localPort3;
         public byte localPort4;
         public uint remoteAddr;
         public byte remotePort1;
         public byte remotePort2;
         public byte remotePort3;
         public byte remotePort4;
         public int owningPid;
      }

      #endregion
   }

   #endregion
#endif
}