-
Notifications
You must be signed in to change notification settings - Fork 8
/
Form1.cs
370 lines (338 loc) · 11.9 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using Blank_TCP_Server.Servers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Blank_TCP_Server.Servers.Function;
using Microsoft.VisualBasic;
using Blank_TCP_Server.Views;
using System.Runtime.InteropServices;
using Blank_TCP_Server.Servers.AsyncAwaitServer;
using Blank_TCP_Server.Function;
using Blank_TCP_Server.Language;
namespace Blank_TCP_Server
{
public partial class tcpform : Form
{
AsyncAwaitServer aas;
public tcpform()
{
InitializeComponent();
txtipport.Text = Properties.Settings.Default.ipport;
txtTimer.Text = Properties.Settings.Default.beeptime;
Console.Title = "Tcp Server";
// 隐藏终端显示
ConsoleWindow.HideConsoleWindow();
ML.LoadFormLanguage(this);
BuildLanguageMenuItems();
}
private void BuildLanguageMenuItems()
{
if (ML.LanguageLocalNames.Length == 0)
{
languageToolStripMenuItem.Visible = false;
return;
}
for (int i = 0; i <= ML.LanguageLocalNames.Length - 1; i++)
{
string ln = ML.LanguageLocalNames[i];
var menuItem = new ToolStripMenuItem(ln);
//menuItem.CheckOnClick = true;
menuItem.Tag = i;
if (i == 0)
menuItem.Checked = true;
menuItem.Click += new EventHandler((sender, e) =>
{
foreach (ToolStripMenuItem item in languageToolStripMenuItem.DropDownItems)
item.Checked = (item == sender);
ML.LoadLanguageByIndex((int)(sender as ToolStripItem).Tag);
ML.LoadFormLanguage(this);
});
languageToolStripMenuItem.DropDownItems.Add(menuItem);
}
}
#region btn
private void btn_setting_Click(object sender, EventArgs e)
{
if (txtipport.Text.Length < 3 || txtTimer.Text=="")
{
MessageBox.Show("请设置正确的 IP,PORT,Timer!");
return;
}
Properties.Settings.Default.ipport = txtipport.Text;
Properties.Settings.Default.beeptime = txtTimer.Text;
Properties.Settings.Default.Save();
}
private void btn_timersend_Click(object sender, EventArgs e)
{
if (txtSendData.Text == string.Empty)
{
MessageBox.Show("please input something in above textbox!");
return;
}
if (btn_run.Text == "Stop")
{
if (timer1.Enabled == false)
{
Console.WriteLine("timer started!");
timer1.Interval = Convert.ToInt32(txtTimer.Text);
timer1.Enabled = true;
tsslInfo.Text = "Auto Sending...";
txtSendData.Enabled = false;
return;
}
}
Console.WriteLine("timer stoped!");
tsslInfo.Text = "Ready!";
timer1.Enabled = false;
txtSendData.Enabled = true;
}
private void btn_run_Click(object sender, EventArgs e)
{
if (txtipport.Text.Length < 3 )
{
MessageBox.Show("请设置正确的IP和TIME!");
return;
}
if (btn_run.Text == "Start")
{
btn_run.Text = "Stop";
var th = new Thread(run)
{
Priority = ThreadPriority.Highest
};
th.Start();
txtipport.Enabled = false;
txtTimer.Enabled = false;
txtMaxConnections.Enabled = false;
}
else
{
if (aas != null)
{
aas.Stop();
}
btn_run.Text = "Start";
txtipport.Enabled = true;
txtTimer.Enabled = true;
txtMaxConnections.Enabled = true;
}
}
private void btn_Send_Click(object sender, EventArgs e)
{
string data;
if (txtSendData.Text != string.Empty)
{
data = txtSendDataToAll.Text;
}
else
{
data = "hello from server!";
}
byte[] sendData;
if (rbtnH16.Checked == true)
{
sendData = StaticMethod.StringToByteArray(data);
}
else
{
sendData = Encoding.ASCII.GetBytes(data);
}
aas.SendToAll(sendData);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
FormAbout fa = new FormAbout();
fa.ShowDialog();
}
private void transferToolStripMenuItem_Click(object sender, EventArgs e)
{
DataTransfer dt = new DataTransfer();
dt.Show();
}
#endregion
#region start server
private void run()
{
try
{
int port = Convert.ToInt32(txtipport.Text);
int max = Convert.ToInt32(txtMaxConnections.Text);
aas = new AsyncAwaitServer(port, max);
string msg = "Server listening on port " + port.ToString() + "...\n";
Console.ForegroundColor = (ConsoleColor)((60 - 1) % 16);
Console.WriteLine(msg);
msg = "Timer Invert:" + timer1.Interval;
Console.WriteLine(msg);
aas.Eventlistview += UpdateListView;
aas.Run();
//aas.isStop = true;
//Console.WriteLine("Server has Stoped!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void PrintUsage()
{
Console.WriteLine("Usage: SocketAsyncServer <port> [numConnections] [bufferSize].");
Console.WriteLine("\t<port> Numeric value for the listening TCP port.");
Console.WriteLine("\t[numConnections] Numeric value for the maximum number of incoming connections.");
Console.WriteLine("\t[bufferSize] Numeric value for the buffer size of incoming connections.");
}
#endregion
#region send
private void timer1_Tick(object sender, EventArgs e)
{
try
{
string data = txtSendData.Text;
aas.SendToAll(data);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private void tsmiSend_Click(object sender, EventArgs e)
{
var selectedItems =lvClients.SelectedItems;
if (selectedItems.Count > 0)
{
string datagram = Microsoft.VisualBasic.Interaction.InputBox("please input something", "SendToTcpClient", string.Empty, -1, -1);
byte[] sendData;
if (rbtnH16.Checked == true)
{
sendData = StaticMethod.StringToByteArray(datagram);
}
else
{
sendData = Encoding.ASCII.GetBytes(datagram);
}
for (int i = 0; i < selectedItems.Count; i++)
{
string ip = selectedItems[i].SubItems[1].Text + ":" + selectedItems[i].SubItems[2].Text;
aas.SendToSelectedClient(ip, sendData);
}
}
}
#endregion
#region Update View From Socket
private void UpdateListView(string ip, AsyncAwaitServer.ConnectionStatus status)
{
lvClients.Invoke((MethodInvoker)delegate {
if (status==AsyncAwaitServer.ConnectionStatus.add)
{
ListViewItem lvi;
lvi = new ListViewItem(" Connected", 0);
var values = ip.Split(':');
lvi.SubItems.Add(ip.Substring(0, ip.Length - values[values.Count() - 1].Length - 1));
lvi.SubItems.Add(values[values.Count() - 1]);
lvClients.Items.Add(lvi);
string msg = "Connections:" + lvClients.Items.Count.ToString();
SetStatus(msg);
}
else
{
for (int i = lvClients.Items.Count - 1; i >= 0; i--)
{
string s=lvClients.Items[i].SubItems[1].Text+":" + lvClients.Items[i].SubItems[2].Text;
if (s==ip)
{
lvClients.Items[i].Remove();
}
}
string msg = "Connections:" + lvClients.Items.Count.ToString();
SetStatus(msg);
}
});
}
delegate void SetStatusCallback(String text);
private void SetStatus(String text)
{
if (statusStrip1.InvokeRequired)
{
SetStatusCallback d = new SetStatusCallback(SetStatus);
this.Invoke(d, new object[] { text});
}
else
{
tsslConnections.Text = text;
}
}
#endregion
#region Hidden application to tray
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
private void tcpform_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
//notifyIcon1.ShowBalloonTip(3000);
this.ShowInTaskbar = false;
var handle = GetConsoleWindow();
// Hide
ShowWindow(handle, SW_HIDE);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
showForm();
}
private void showForm()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
notifyIcon1.Visible = false;
var handle = GetConsoleWindow();
// Show
ShowWindow(handle, SW_SHOW);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm();
}
#endregion
private void tcpform_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult userAnswer = MessageBox.Show("Do you wish to close ALL Forms?", "Close",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (userAnswer != DialogResult.Yes)
{
e.Cancel = true;
return;
}
if (aas != null)
{
aas.Stop();
aas.Eventlistview -= UpdateListView;
}
aas = null;
this.Dispose();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
ConsoleWindow.ShowConsoleWindow();
}
private void hiddenToolStripMenuItem_Click(object sender, EventArgs e)
{
ConsoleWindow.HideConsoleWindow();
}
}
}