/[H8]/trunk/docs/Microchip TCP_IP stack/TCPIP Stack/Microchip Ethernet Discoverer/frmMain.cs
ViewVC logotype

Contents of /trunk/docs/Microchip TCP_IP stack/TCPIP Stack/Microchip Ethernet Discoverer/frmMain.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Thu Apr 19 09:01:15 2007 UTC (17 years, 1 month ago) by hedin
File size: 5887 byte(s)
added the TCP/IP stack, source code.
1 /*********************************************************************
2 *
3 * Microchip Ethernet Discoverer
4 * -Provides a PC interface for the Announce.c Microchip TCP/IP
5 * module
6 *
7 *********************************************************************
8 * FileName: frmMain.cs
9 * Dependencies: Microsoft .NET Framework 2.0
10 * Processor: x86
11 * Complier: Microsoft Visual C# 2005 Express Edition
12 * Company: Microchip Technology, Inc.
13 *
14 * Software License Agreement
15 *
16 * This software is owned by Microchip Technology Inc. ("Microchip")
17 * and is supplied to you for use exclusively as described in the
18 * associated software agreement. This software is protected by
19 * software and other intellectual property laws. Any use in
20 * violation of the software license may subject the user to criminal
21 * sanctions as well as civil liability. Copyright 2006 Microchip
22 * Technology Inc. All rights reserved.
23 *
24 * This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
25 * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
26 * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
27 * INFRINGEMENT. Microchip shall in no event be liable for special,
28 * incidental, or consequential damages.
29 *
30 *
31 * Author Date Comment
32 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 * Howard Schlunder 7/31/06 Original
34 ********************************************************************/
35 using System;
36 using System.Collections.Generic;
37 using System.ComponentModel;
38 using System.Data;
39 using System.Drawing;
40 using System.Text;
41 using System.Windows.Forms;
42 using System.Net.Sockets;
43
44 namespace Embedded_Device_Discoverer
45 {
46 public partial class Form1 : Form
47 {
48 public delegate void AsyncCallback(IAsyncResult ar);
49 public delegate void AddTolstDiscoveredDevices(object o);
50
51 private UdpState GlobalUDP;
52
53 public Form1()
54 {
55 InitializeComponent();
56 }
57
58 struct UdpState
59 {
60 public System.Net.IPEndPoint EP;
61 public System.Net.Sockets.UdpClient UDPClient;
62 }
63
64 private void cmdDiscoverDevices_Click(object sender, EventArgs e)
65 {
66 try
67 {
68 // Clear the listbox of all current discovery responses
69 listView1.Items.Clear();
70
71 // Try to send the discovery request message
72 byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?");
73 GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303));
74 }
75 catch
76 {
77 MessageBox.Show("Unable to transmit discovery message. Check network connectivity and ensure that no other instances of this program are running.", "Error", MessageBoxButtons.OK);
78 return;
79 }
80 }
81
82 public void AddDiscoveryEntry(object o)
83 {
84 //lstDiscoveredDevices.Items.Add(o);
85 listView1.Items.Add(new ListViewItem(((string)(o)).Split('\n')));
86 }
87
88 public void ReceiveCallback(IAsyncResult ar)
89 {
90 UdpState MyUDP = (UdpState)ar.AsyncState;
91
92 // Obtain the UDP message body and convert it to a string, with remote IP address attached as well
93 string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP));
94 ReceiveString = MyUDP.EP.Address.ToString() + "\n" + ReceiveString.Replace("\r\n", "\n");
95
96 // Configure the UdpClient class to accept more messages, if they arrive
97 MyUDP.UDPClient.BeginReceive(ReceiveCallback, MyUDP);
98
99 // Write the received UDP message text to the listbox in a thread-safe manner
100 //lstDiscoveredDevices.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);
101 listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);
102 }
103
104 private void Form1_Load(object sender, EventArgs e)
105 {
106 try
107 {
108 GlobalUDP.UDPClient = new UdpClient();
109 GlobalUDP.EP = new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303);
110 System.Net.IPEndPoint BindEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 30303);
111 byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?");
112
113 // Set the local UDP port to listen on
114 GlobalUDP.UDPClient.Client.Bind(BindEP);
115
116 // Enable the transmission of broadcast packets without having them be received by ourself
117 GlobalUDP.UDPClient.EnableBroadcast = true;
118 GlobalUDP.UDPClient.MulticastLoopback = false;
119
120 // Configure ourself to receive discovery responses
121 GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP);
122
123 // Transmit the discovery request message
124 GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 30303));
125 }
126 catch
127 {
128 MessageBox.Show("Unable to transmit discovery message. Check network connectivity and ensure that no other instances of this program are running.", "Error", MessageBoxButtons.OK);
129 return;
130 }
131 }
132
133 private void listView1_ItemActivate(object sender, EventArgs e)
134 {
135 System.Diagnostics.Process.Start("http://" + listView1.SelectedItems[0].Text);
136 }
137 }
138 }

  ViewVC Help
Powered by ViewVC 1.1.20