-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
191 lines (161 loc) · 7.29 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NetworkConsole
{
public partial class Form1 : Form
{
UdpClient receiver = new UdpClient(48762);
public Form1()
{
InitializeComponent();
UDPsender.EnableBroadcast = true;
UDPsender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UDPsender.ExclusiveAddressUse = false;
receiver.BeginReceive(DataReceived, receiver); //}
MyIPLbl.Text = MyIPAddress;
BroadcastBtn.PerformClick();
}
private void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)ar.AsyncState;
if (c == null) return;
try
{
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 48762);
if (receivedIpEndPoint == null) return;
Byte[] receivedBytes = c.EndReceive(ar, ref receivedIpEndPoint);
// Convert data to ASCII and print in console
string receivedText = Encoding.UTF8.GetString(receivedBytes);
//Console.WriteLine(receivedText);
if (receivedText.IndexOf("^") > 0)
{
string[] arr = receivedText.Split('^');
if (arr.Length > 0)
{
//if (arr[0] == "MyRetailer Broadcast Server")
//{
// System.Console.Beep();
// var builer = new StringBuilder();
// builer.Append(string.Format(DateTime.Now.ToString("hh:mm:ss ") + "\t{0} : {1}", arr[0], arr[1]));
// builer.Append(Environment.NewLine);
// Rt += builer.ToString(); this.Invoke(new EventHandler(datarecieved));
//}
//else
{
System.Console.Beep();
var builer = new StringBuilder();
builer.Append(string.Format(DateTime.Now.ToString("hh:mm:ss ") + "\t{0} : {1}", arr[0], arr[1]));
builer.Append(Environment.NewLine);
Rt += builer.ToString();
this.Invoke(new EventHandler(datarecieved));
}
}
}
c.BeginReceive(DataReceived, ar.AsyncState);
}
catch (Exception ex) { }
}
string Rt;
private void datarecieved(object sender, EventArgs e)
{
ConsoleWindow.AppendText(Rt);
Rt = "";
ConsoleWindow.ScrollToCaret();
//ConsoleWindow.SelectionLength = 0;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
receiver.Close();
receiver.Dispose();
}
public static UdpClient UDPsender = new UdpClient();
private void BroadcastTimer_Tick(object sender, EventArgs e)
{
IPEndPoint broadcastAddress = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 48769);
UDPsender.Send(Encoding.UTF8.GetBytes("MyRetailer Broadcast Server^" + MyIPAddress), Encoding.UTF8.GetBytes("MyRetailer Broadcast Server^" + MyIPAddress).Length, broadcastAddress);
}
private string _myIP = "";
private string MyIPAddress
{
get
{
if (_myIP == "") _myIP = DisplayIPAddresses();
return _myIP;
}
}
public static string DisplayIPAddresses()
{
string returnAddress = String.Empty;
// Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface network in networkInterfaces)
{
// Read the IP configuration for each network
IPInterfaceProperties properties = network.GetIPProperties();
if (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
network.OperationalStatus == OperationalStatus.Up &&
!network.Description.ToLower().Contains("virtual") &&
!network.Description.ToLower().Contains("pseudo"))
{
// Each network interface may have multiple IP addresses
foreach (IPAddressInformation address in properties.UnicastAddresses)
{
// We're only interested in IPv4 addresses for now
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
// Ignore loopback addresses (e.g., 127.0.0.1)
if (IPAddress.IsLoopback(address.Address))
continue;
returnAddress = address.Address.ToString();
//Console.WriteLine(address.Address.ToString() + " (" + network.Name + " - " + network.Description + ")");
}
}
else
{
if (network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 &&
network.OperationalStatus == OperationalStatus.Up &&
!network.Description.ToLower().Contains("virtual") &&
!network.Description.ToLower().Contains("pseudo"))
{
// Each network interface may have multiple IP addresses
foreach (IPAddressInformation address in properties.UnicastAddresses)
{
// We're only interested in IPv4 addresses for now
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
// Ignore loopback addresses (e.g., 127.0.0.1)
if (IPAddress.IsLoopback(address.Address))
continue;
returnAddress = address.Address.ToString();
//Console.WriteLine(address.Address.ToString() + " (" + network.Name + " - " + network.Description + ")");
}
}
}
}
return returnAddress;
}
private void BroadcastBtn_Click(object sender, EventArgs e)
{
BroadcastBtn.Checked = !BroadcastBtn.Checked;
if(BroadcastBtn.Checked)
{
BroadcastTimer.Interval = 2000;
BroadcastTimer.Enabled = true;
}
else
{
BroadcastTimer.Enabled = false;
}
}
}
}