-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
94 lines (81 loc) · 3.11 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
using System;
using System.Threading;
namespace InterProcessCommunication
{
class Program
{
static void printUsage()
{
Console.WriteLine(
"The purpose of this program is to demonstrate data exchange between two processes using bidirectional communication.\n" +
"Implemented data exchange method: Socket-JSON over TCP\n\n" +
"Usage:\n" +
"\t-c server_host:port_number\t\tRun in client mode, connect to the specific address:port" +
"\t\t -c 10.0.0.1:3001\n" +
"\t-s listening_address port_number\t\tRun in server mode, listening on specific IP and port" +
"\t\t -s 127.0.0.1 3001" +
"\t\t -s 3001" +
"\t?\t\tDisplay this message.");
}
static void Main(string[] args)
{
if( args.Length < 2 )
{
Console.WriteLine("You missed the program parameters!");
printUsage();
}
else if( args[0] == "-c" )
{
ClientImpl client = new ClientImpl();
Console.WriteLine("<Running in client mode>");
try
{
int colonIndex = args[1].IndexOf(':');
if(colonIndex == -1)
{
throw new ArgumentException("Invalid host:port format");
}
string host = args[1].Substring(0, colonIndex);
string port = args[1].Substring(colonIndex + 1);
while(! client.Connect(host, Int32.Parse(port)) )
{
Console.WriteLine("Trying to connect to {0}:{1}...",host, port);
Thread.Sleep(1000);
}
}
catch (Exception e)
{
Console.WriteLine("Failed to connect to the server: {0}", e.ToString());
}
if (client.IsConnected)
{
Thread clientReceiveTH = new Thread(client.Receive);
clientReceiveTH.Start();
DataEncoderImpl.SendSampleData(client.Send);
DataEncoderImpl.SendRandomSampleData(client.Send);
}
}
else if( args[0] == "-s" )
{
ServerImpl server = new ServerImpl();
Console.WriteLine("<Running in server mode>");
if (args.Length < 3)
{
server.Listening("", int.Parse(args[1]));
}
else
{
server.Listening(args[1], Int32.Parse(args[2]));
}
Thread serverReceiveTH = new Thread(server.Receive);
serverReceiveTH.Start();
DataEncoderImpl.SendSampleData(server.Send);
DataEncoderImpl.SendRandomSampleData(server.Send);
}
else
{
printUsage();
}
}
}
}