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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
namespace Lithium
{
/// <summary>
/// Логика взаимодействия для Window1.xaml
/// </summary>
public partial class ChatWindow : Window
{
public ChatWindow()
{
InitializeComponent();
}
private delegate void TextChanger();
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Send(client, "This is a test<EOF>");
}
public void ShowMessage(string message)
{
Thread mChanger = new Thread(new ThreadStart(delegate() { this.ChangeTextProperly(message); }));
mChanger.Start();
}
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 256;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
}
private void ChangeTextProperly(string msg)
{
if (ClientBox.Dispatcher.CheckAccess())
{
ClientBox.AppendText(msg);
return;
}
else
{
ClientBox.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new TextChanger(delegate() { this.ChangeTextProperly(msg); }));
}
}
private const int port = 11000;
bool ConnectionDone = false;
// The response from the remote device.
private static String response = String.Empty;
public void StartClient()
{
// Connect to a remote device.
try
{
IPAddress ipAddress = IPAddress.Parse("172.17.135.60");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
//Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
if (ConnectionDone == true)
{
// Receive the response from the remote device.
//Receive(client);
ShowMessage("Response received : " + response);
// Release the socket.
//client.Shutdown(SocketShutdown.Both);
//client.Close();
}
}
catch (Exception e)
{
ShowMessage(e.ToString());
}
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
ShowMessage("Socket connected to " + client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
ConnectionDone = true;
}
catch (Exception e)
{
ReconnectInTime(5000);
ShowMessage(e.ToString());
}
}
private void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
ShowMessage(e.ToString());
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
Receive(client);
}
}
catch (Exception e)
{
ShowMessage(e.ToString());
}
}
private void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
ShowMessage("Sent" + bytesSent + "to server.");
// Signal that all bytes have been sent.
}
catch (Exception e)
{
ShowMessage(e.ToString());
}
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
ServerWindow srw = new ServerWindow();
srw.Show();
}
private void Button_Loaded_1(object sender, RoutedEventArgs e)
{
this.StartClient();
}
private void Lit_Loaded(object sender, RoutedEventArgs e)
{
ClientBox.Document.Blocks.Clear();
NameBox.Document.Blocks.Clear();
ClientBox.IsReadOnly = true;
NameBox.IsReadOnly = true;
this.StartClient();
}
public void ReconnectInTime(Int32 Time)
{
Timer tmer = new Timer(TimerCallback, null, Time, System.Threading.Timeout.Infinite);
}
void TimerCallback(object param)
{
this.StartClient();
}
}
} |