Skip to content

Commit 85ce9d6

Browse files
karelzBillWagner
authored andcommitted
Update UdpClient sample (#482)
Rename listenPort to s_listenPort Fix code formatting Cleanup code a bit
1 parent b5044b4 commit 85ce9d6

File tree

1 file changed

+137
-158
lines changed
  • snippets/csharp/VS_Snippets_Remoting/System.Net.Sockets.UdpClient1/CS

1 file changed

+137
-158
lines changed

snippets/csharp/VS_Snippets_Remoting/System.Net.Sockets.UdpClient1/CS/asyncudp.cs

Lines changed: 137 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -6,200 +6,179 @@
66
using System.Security.Permissions;
77
using System.Threading;
88

9-
10-
class MyUdpClient
9+
class MyUdpClientF
1110
{
12-
static int listenPort = 13000;
13-
14-
public static void Main(String[] args)
15-
{
16-
// Parse arguments
17-
String server = "";
18-
String message = "This is a test!";;
19-
bool isServer = false;
20-
int sendMethod = 1; // n called SendMessagen
11+
static int s_listenPort = 13000;
2112

22-
if(args.Length == 0)
23-
{
24-
server = "localhost";
25-
isServer = false;
26-
}
27-
else if(args.Length == 1)
28-
{
29-
isServer = args[0]=="s" ? true : false;
30-
}
31-
else if(args.Length == 2)
32-
{
33-
isServer = args[0]=="s" ? true : false;
34-
server = args[1];
35-
}
36-
else if(args.Length == 3)
37-
{
38-
isServer = args[0]=="s" ? true : false;
39-
server = args[1];
40-
message = args[2];
41-
}
42-
else if(args.Length == 4)
13+
public static void Main(String[] args)
4314
{
44-
isServer = args[0]=="s" ? true : false;
45-
server = args[1];
46-
message = args[2];
47-
sendMethod = Convert.ToInt32(args[3]);
48-
}
49-
else
50-
{
51-
Console.WriteLine("Usage: asyncudp [s|c] [host name] [message] [send method]");
52-
return;
15+
// Parse arguments
16+
string server;
17+
string message = "This is a test!";
18+
bool isServer;
19+
int sendMethodIndex = 1; // Which SendMessage* method to call (1/2/3)
20+
21+
if (args.Length == 0)
22+
{
23+
server = "localhost";
24+
isServer = false;
25+
}
26+
else if (args.Length > 4)
27+
{
28+
Console.WriteLine("Usage: asyncudp [s|c] [host name] [message] [send method index]");
29+
return;
30+
}
31+
else
32+
{
33+
isServer = (args[0] == "s");
34+
server = (args.Length >= 2) ? args[1] : "";
35+
if (args.Length >= 3)
36+
{
37+
message = args[2];
38+
}
39+
if (args.Length >= 4)
40+
{
41+
sendMethodIndex = Convert.ToInt32(args[3]);
42+
}
43+
}
44+
45+
if (isServer)
46+
{
47+
ReceiveMessages();
48+
}
49+
else
50+
{
51+
switch (sendMethodIndex)
52+
{
53+
case 1:
54+
SendMessage1(server, message);
55+
break;
56+
case 2:
57+
SendMessage2(server, message);
58+
break;
59+
case 3:
60+
SendMessage3(server, message);
61+
break;
62+
}
63+
}
5364
}
5465

55-
if (isServer)
66+
//<snippet1>
67+
public struct UdpState
5668
{
57-
ReceiveMessages();
69+
public UdpClient u;
70+
public IPEndPoint e;
5871
}
59-
else
72+
73+
public static bool messageReceived = false;
74+
75+
public static void ReceiveCallback(IAsyncResult ar)
6076
{
61-
switch (sendMethod)
62-
{
63-
case 1:
64-
SendMessage1(server,message);
65-
break;
66-
case 2:
67-
SendMessage2(server,message);
68-
break;
69-
case 3: UdpClient u = new UdpClient();
70-
71-
SendMessage3(server,message);
72-
break;
73-
}
74-
}
75-
}
77+
UdpClient u = ((UdpState)(ar.AsyncState)).u;
78+
IPEndPoint e = ((UdpState)(ar.AsyncState)).e;
7679

80+
byte[] receiveBytes = u.EndReceive(ar, ref e);
81+
string receiveString = Encoding.ASCII.GetString(receiveBytes);
7782

78-
UdpClient u = new UdpClient();
83+
Console.WriteLine($"Received: {receiveString}");
84+
messageReceived = true;
85+
}
7986

80-
//<snippet1>
81-
public struct UdpState
82-
{
83-
public UdpClient u;
84-
public IPEndPoint e;
85-
}
86-
87-
public static bool messageReceived = false;
88-
89-
public static void ReceiveCallback(IAsyncResult ar)
90-
{
91-
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
92-
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
93-
94-
Byte[] receiveBytes = u.EndReceive(ar, ref e);
95-
string receiveString = Encoding.ASCII.GetString(receiveBytes);
96-
97-
Console.WriteLine("Received: {0}", receiveString);
98-
messageReceived = true;
99-
}
100-
101-
public static void ReceiveMessages()
102-
{
103-
// Receive a message and write it to the console.
104-
IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
105-
UdpClient u = new UdpClient(e);
106-
107-
UdpState s = new UdpState();
108-
s.e = e;
109-
s.u = u;
110-
111-
Console.WriteLine("listening for messages");
112-
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
113-
114-
// Do some work while we wait for a message. For this example,
115-
// we'll just sleep
116-
while (!messageReceived)
87+
public static void ReceiveMessages()
11788
{
118-
Thread.Sleep(100);
89+
// Receive a message and write it to the console.
90+
IPEndPoint e = new IPEndPoint(IPAddress.Any, s_listenPort);
91+
UdpClient u = new UdpClient(e);
92+
93+
UdpState s = new UdpState();
94+
s.e = e;
95+
s.u = u;
96+
97+
Console.WriteLine("listening for messages");
98+
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
99+
100+
// Do some work while we wait for a message. For this example, we'll just sleep
101+
while (!messageReceived)
102+
{
103+
Thread.Sleep(100);
104+
}
119105
}
120-
}
121106
//</snippet1>
122107

123108
//<snippet2>
124-
public static bool messageSent = false;
109+
public static bool messageSent = false;
125110

126-
public static void SendCallback(IAsyncResult ar)
127-
{
128-
UdpClient u = (UdpClient)ar.AsyncState;
111+
public static void SendCallback(IAsyncResult ar)
112+
{
113+
UdpClient u = (UdpClient)ar.AsyncState;
129114

130-
Console.WriteLine("number of bytes sent: {0}", u.EndSend(ar));
131-
messageSent = true;
132-
}
115+
Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
116+
messageSent = true;
117+
}
133118
//</snippet2>
134119

135120
//<snippet3>
136-
static void SendMessage1(string server, string message)
137-
{
138-
// create the udp socket
139-
UdpClient u = new UdpClient();
140-
141-
u.Connect(server, listenPort);
142-
Byte [] sendBytes = Encoding.ASCII.GetBytes(message);
143-
144-
// send the message
145-
// the destination is defined by the call to .Connect()
146-
u.BeginSend(sendBytes, sendBytes.Length,
147-
new AsyncCallback(SendCallback), u);
148-
149-
// Do some work while we wait for the send to complete. For
150-
// this example, we'll just sleep
151-
while (!messageSent)
121+
static void SendMessage1(string server, string message)
152122
{
153-
Thread.Sleep(100);
123+
// create the udp socket
124+
UdpClient u = new UdpClient();
125+
126+
u.Connect(server, s_listenPort);
127+
byte[] sendBytes = Encoding.ASCII.GetBytes(message);
128+
129+
// send the message
130+
// the destination is defined by the call to .Connect()
131+
u.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), u);
132+
133+
// Do some work while we wait for the send to complete. For this example, we'll just sleep
134+
while (!messageSent)
135+
{
136+
Thread.Sleep(100);
137+
}
154138
}
155-
}
156139
//</snippet3>
157140

158141
//<snippet4>
159-
static void SendMessage2(string server, string message)
160-
{
161-
// create the udp socket
162-
UdpClient u = new UdpClient();
163-
Byte [] sendBytes = Encoding.ASCII.GetBytes(message);
142+
static void SendMessage2(string server, string message)
143+
{
144+
// create the udp socket
145+
UdpClient u = new UdpClient();
146+
byte[] sendBytes = Encoding.ASCII.GetBytes(message);
164147

165-
// resolve the server name
166-
IPHostEntry heserver = Dns.GetHostEntry(server);
148+
// resolve the server name
149+
IPHostEntry heserver = Dns.GetHostEntry(server);
167150

168-
IPEndPoint e = new IPEndPoint(heserver.AddressList[0], listenPort);
151+
IPEndPoint e = new IPEndPoint(heserver.AddressList[0], s_listenPort);
169152

170-
// send the message
171-
// the destination is defined by the IPEndPoint
172-
u.BeginSend(sendBytes, sendBytes.Length, e,
173-
new AsyncCallback(SendCallback), u);
153+
// send the message
154+
// the destination is defined by the IPEndPoint
155+
u.BeginSend(sendBytes, sendBytes.Length, e, new AsyncCallback(SendCallback), u);
174156

175-
// Do some work while we wait for the send to complete. For
176-
// this example, we'll just sleep
177-
while (!messageSent)
178-
{
179-
Thread.Sleep(100);
157+
// Do some work while we wait for the send to complete. For this example, we'll just sleep
158+
while (!messageSent)
159+
{
160+
Thread.Sleep(100);
161+
}
180162
}
181-
}
182163
//</snippet4>
183164

184165
//<snippet5>
185-
static void SendMessage3(string server, string message)
186-
{
187-
// create the udp socket
188-
UdpClient u = new UdpClient();
166+
static void SendMessage3(string server, string message)
167+
{
168+
// create the udp socket
169+
UdpClient u = new UdpClient();
189170

190-
Byte [] sendBytes = Encoding.ASCII.GetBytes(message);
171+
byte[] sendBytes = Encoding.ASCII.GetBytes(message);
191172

192-
// send the message
193-
// the destination is defined by the server name and port
194-
u.BeginSend(sendBytes, sendBytes.Length, server, listenPort,
195-
new AsyncCallback(SendCallback), u);
173+
// send the message
174+
// the destination is defined by the server name and port
175+
u.BeginSend(sendBytes, sendBytes.Length, server, s_listenPort, new AsyncCallback(SendCallback), u);
196176

197-
// Do some work while we wait for the send to complete. For
198-
// this example, we'll just sleep
199-
while (!messageSent)
200-
{
201-
Thread.Sleep(100);
177+
// Do some work while we wait for the send to complete. For this example, we'll just sleep
178+
while (!messageSent)
179+
{
180+
Thread.Sleep(100);
181+
}
202182
}
203-
}
204183
//</snippet5>
205-
}
184+
}

0 commit comments

Comments
 (0)