-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
242 lines (227 loc) · 8.34 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SocketSingleSend
{
class Program
{
private const string ANSWER_FLAG = ">>OK<<";
private const string IP_FLAG = ">>IP<<";
private const string CRYPTO_KEY = "SimpleSocketChat";
private const int MAX_STR_LEN = 120;
private const int PORT = 10019;
private const int AS_PORT = 11019;
private const int BC_PORT = 19019;
static readonly IPAddress localIP = Dns.GetHostAddresses(Dns.GetHostName()).Last();
static bool sendFail = true;
static Messenger udpSender;
static Messenger udpReceiver;
static Messenger broadcaster;
static Dictionary<IPAddress, IPAddress> senderDic;
#region UtilMethod
static IPAddress StrToIP(string ipStr)
{
try
{
return IPAddress.Parse(ipStr);
}
catch (Exception ex)
{
#if DEBUG
Console.WriteLine(ex.Message);
#endif
}
return null;
}
static IPEndPoint CreateIPE(IPAddress ip, int port)
{
return new IPEndPoint(ip, port);
}
static EndPoint CreateEmptyEP()
{
return CreateIPE(IPAddress.Any, 0);
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("1. Send ;\r\n2. Receive ;");
#if DEBUG
Console.WriteLine("3. Loop Send ;");
#endif
Console.WriteLine("Make a choice to do(1 or 2):");
string choice = Console.ReadLine().Trim();
if (choice == "1")
{
//do Send
InitMessenger(true, true);
IPAddress targetIP = null;
while (targetIP == null)
{
Console.WriteLine("Enter the other side's IP or press Enter directly:");
string ipStr = Console.ReadLine().Trim();
if (string.IsNullOrEmpty(ipStr))
{
Console.WriteLine("Waiting to auto connect...");
EndPoint remoteIPE = CreateEmptyEP();
ipStr = broadcaster.ReceiveBroadcast(ref remoteIPE);
if (ipStr != null && ipStr.StartsWith(IP_FLAG))
ipStr = ipStr.Substring(IP_FLAG.Length);
}
targetIP = StrToIP(ipStr);
}
Console.WriteLine("Connect to Receiver: {0}", targetIP);
Console.WriteLine("Write down your text:");
Console.WriteLine("-----------------------------------");
string str = null;
do
{
str = Console.ReadLine();
if (str == null)
break;
else if (str.Length > MAX_STR_LEN)
{
Console.WriteLine("<<<<<<<<<<<Too Long!");
continue;
}
else if (str.Length == 0)
{
Console.WriteLine("<<<<<<<<<<<Can not be empty!");
continue;
}
if (!SendText(str, targetIP))
Console.WriteLine("<<<<<<<<<<<The other side may not have received!");
} while (str != "exit");
}
else if (choice == "2")
{
//do Receive
InitMessenger(true, false);
Console.WriteLine("Local IP is: " + localIP);
Console.WriteLine("Waiting to receive the Msg...");
broadcaster.Broadcasting(IP_FLAG + localIP, BC_PORT);
Console.WriteLine("-----------------------------------");
string msg = null;
do
{
msg = ReceiveText();
if (msg != null)
Console.WriteLine(msg);
} while (msg != "exit");
}
#if DEBUG
else if (choice == "3")
{
//Loop Send for Test
InitMessenger(true, true);
IPAddress targetIP = null;
string str = null;
int count = 0;
while (targetIP == null)
{
Console.WriteLine("Enter the other side's IP:");
targetIP = StrToIP(Console.ReadLine().Trim());
}
Console.WriteLine("-----------------------------------");
while (true)
{
str = $"Send Test {++count} -> {DateTime.Now.ToShortTimeString()}";
Console.WriteLine(str);
if (!SendText(str, targetIP))
Console.WriteLine("<<<<<<<<<<<The other side may not have received!");
Thread.Sleep(5000);
}
}
#endif
broadcaster?.Close();
udpReceiver?.Close();
udpSender?.Close();
}
static string ReceiveText()
{
//UDP Mode
EndPoint remoteIPE = CreateEmptyEP();
string receiveStr = udpReceiver.UDPReceive(ref remoteIPE);
if (receiveStr == null)
return receiveStr;
//judge whether it is IP or not and check in
if (IPCheckIn(receiveStr, remoteIPE))
return null;
//get the useful IP
IPAddress answerIP = IPCheckOut(remoteIPE);
if (answerIP == null)
return receiveStr;
//Send answer
udpSender.SteadySend(ANSWER_FLAG + CryptoUtil.SHA256Hash(receiveStr),
CreateIPE(answerIP, AS_PORT));
return receiveStr;
}
static bool SendText(string str, IPAddress targetIP)
{
//UDP Mode
EndPoint remoteIPE = CreateEmptyEP();
//send localIP to Receiver if sendFail
if (sendFail)
udpSender.SteadySend(IP_FLAG + localIP, CreateIPE(targetIP, PORT));
//clean up the buffer
udpReceiver.FlushReceiveBuf();
if (udpSender.UDPSend(str, CreateIPE(targetIP, PORT)))
{
//Receive answer
if (udpReceiver.ReceiveLastOne(ref remoteIPE) ==
ANSWER_FLAG + CryptoUtil.SHA256Hash(str))
{
sendFail = false;
return true;
}
}
sendFail = true;
return false;
}
static void InitMessenger(bool udpMode, bool sendMode)
{
CryptoUtil.Init(CRYPTO_KEY, CRYPTO_KEY);
//Init Socket
udpSender = new Messenger(udpMode);
if (sendMode)
{
udpReceiver = new Messenger(udpMode, CreateIPE(localIP, AS_PORT), 2500);
broadcaster = new Messenger(udpMode, CreateIPE(localIP, BC_PORT));
}
else
{
udpReceiver = new Messenger(udpMode, CreateIPE(localIP, PORT));
broadcaster = new Messenger(udpMode, CreateIPE(localIP, 0));
senderDic = new Dictionary<IPAddress, IPAddress>();
}
}
static IPAddress IPCheckOut(EndPoint remoteIPE)
{
//get the useful IP if it has been checked in
IPAddress remoteIP = ((IPEndPoint)remoteIPE).Address;
//check if the IP has been checked in
if (senderDic.ContainsKey(remoteIP))
return senderDic[remoteIP];
else
return null;
}
static bool IPCheckIn(string receiveStr, EndPoint remoteIPE)
{
//check in the Sender's IP
if (!receiveStr.StartsWith(IP_FLAG))
return false;
IPAddress receiveIP = null;
//Value
receiveIP = StrToIP(receiveStr.Substring(IP_FLAG.Length));
if (receiveIP == null)
return false;
//Key
IPAddress remoteIP = ((IPEndPoint)remoteIPE).Address;
senderDic[remoteIP] = receiveIP;
return true;
}
}
}