annotate Messaging/Client/Sockets/AsynchronousClient.cs @ 26:045dac571339

Working on data binding to a user control
author adminsh@apollo
date Wed, 21 Mar 2012 15:39:53 +0000
parents
children
rev   line source
26
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
1 using System;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
2 using System.Net;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
3 using System.Net.Sockets;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
4 using System.Text;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
5 using System.Threading;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
6
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
7 namespace Client.Sockets
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
8 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
9 public class AsynchronousClient
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
10 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
11 // The port number for the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
12 private const int Port = 11000;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
13
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
14 // ManualResetEvent instances signal completion.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
15 private static readonly ManualResetEvent connectDone =
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
16 new ManualResetEvent(false);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
17
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
18 private static readonly ManualResetEvent sendDone =
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
19 new ManualResetEvent(false);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
20
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
21 private static readonly ManualResetEvent receiveDone =
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
22 new ManualResetEvent(false);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
23
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
24 // The response from the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
25 private static String response = String.Empty;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
26
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
27 private static void StartClient()
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
28 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
29 // Connect to a remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
30 try
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
31 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
32 // Establish the remote endpoint for the socket.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
33 // The name of the
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
34 // remote device is "host.contoso.com".
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
35 var ipHostInfo = Dns.GetHostEntry("host.contoso.com");
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
36 var ipAddress = ipHostInfo.AddressList[0];
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
37 var remoteEP = new IPEndPoint(ipAddress, Port);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
38
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
39 // Create a TCP/IP socket.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
40 var client = new Socket(
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
41 AddressFamily.InterNetwork,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
42 SocketType.Stream,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
43 ProtocolType.Tcp);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
44
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
45 // Connect to the remote endpoint.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
46 client.BeginConnect(
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
47 remoteEP,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
48 ConnectCallback,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
49 client);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
50
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
51 connectDone.WaitOne();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
52
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
53 // Send test data to the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
54 Send(client, "This is a test<EOF>");
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
55 sendDone.WaitOne();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
56
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
57 // Receive the response from the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
58 Receive(client);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
59 receiveDone.WaitOne();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
60
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
61 // Write the response to the console.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
62 Console.WriteLine("Response received : {0}", response);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
63
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
64 // Release the socket.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
65 client.Shutdown(SocketShutdown.Both);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
66 client.Close();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
67 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
68 catch (Exception e)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
69 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
70 Console.WriteLine(e.ToString());
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
71 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
72 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
73
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
74 private static void ConnectCallback(IAsyncResult ar)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
75 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
76 try
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
77 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
78 // Retrieve the socket from the state object.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
79 var client = (Socket) ar.AsyncState;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
80
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
81 // Complete the connection.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
82 client.EndConnect(ar);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
83
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
84 Console.WriteLine(
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
85 "Socket connected to {0}",
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
86 client.RemoteEndPoint);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
87
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
88 // Signal that the connection has been made.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
89 connectDone.Set();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
90 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
91 catch (Exception e)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
92 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
93 Console.WriteLine(e.ToString());
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
94 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
95 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
96
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
97 private static void Receive(Socket client)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
98 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
99 try
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
100 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
101 // Create the state object.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
102 var state = new StateObject {WorkSocket = client};
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
103
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
104 // Begin receiving the data from the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
105 client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
106 ReceiveCallback, state);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
107 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
108 catch (Exception e)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
109 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
110 Console.WriteLine(e.ToString());
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
111 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
112 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
113
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
114 private static void ReceiveCallback(IAsyncResult ar)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
115 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
116 try
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
117 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
118 // Retrieve the state object and the client socket
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
119 // from the asynchronous state object.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
120 var state = (StateObject) ar.AsyncState;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
121 var client = state.WorkSocket;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
122
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
123 // Read data from the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
124 var bytesRead = client.EndReceive(ar);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
125
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
126 if (bytesRead > 0)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
127 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
128 // There might be more data, so store the data received so far.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
129 state.Sb.Append(Encoding.ASCII.GetString(state.Buffer, 0, bytesRead));
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
130
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
131 // Get the rest of the data.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
132 client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
133 ReceiveCallback, state);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
134 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
135 else
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
136 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
137 // All the data has arrived; put it in response.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
138 if (state.Sb.Length > 1)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
139 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
140 response = state.Sb.ToString();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
141 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
142 // Signal that all bytes have been received.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
143 receiveDone.Set();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
144 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
145 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
146 catch (Exception e)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
147 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
148 Console.WriteLine(e.ToString());
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
149 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
150 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
151
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
152 private static void Send(Socket client, String data)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
153 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
154 // Convert the string data to byte data using ASCII encoding.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
155 var byteData = Encoding.ASCII.GetBytes(data);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
156
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
157 // Begin sending the data to the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
158 client.BeginSend(
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
159 byteData,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
160 0,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
161 byteData.Length,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
162 0,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
163 SendCallback,
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
164 client);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
165 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
166
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
167 private static void SendCallback(IAsyncResult ar)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
168 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
169 try
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
170 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
171 // Retrieve the socket from the state object.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
172 var client = (Socket) ar.AsyncState;
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
173
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
174 // Complete sending the data to the remote device.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
175 var bytesSent = client.EndSend(ar);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
176 Console.WriteLine("Sent {0} bytes to server.", bytesSent);
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
177
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
178 // Signal that all bytes have been sent.
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
179 sendDone.Set();
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
180 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
181 catch (Exception e)
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
182 {
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
183 Console.WriteLine(e.ToString());
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
184 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
185 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
186 }
045dac571339 Working on data binding to a user control
adminsh@apollo
parents:
diff changeset
187 }