-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin-client-ete.cs
107 lines (105 loc) · 3.97 KB
/
win-client-ete.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
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace BBBTest2
{
public class tcpClient
{
public static int TCP_PORT = 5001;
private bool rcvStarted = false;
private Int32 blockSize = 0;
private Int32 totalBytesReceived = 0;
public static string REMOTE_IP = "";
public static bool done = false;
public static string fileNamePrefix;
public static void Main(String[] argv)
{
if (argv.Length != 2)
{
Console.WriteLine("Restart with two parameters: <remote server IP Address> & <TCP port> ... example: 192.168.20.90 5001\n");
}
else
{
fileNamePrefix = GenerateFileName();
REMOTE_IP = argv[0];
TCP_PORT = int.Parse(argv[1]);
tcpClient client = new tcpClient();
client.clientProcess(REMOTE_IP);
Console.WriteLine("Receive operation Complete.");
Console.WriteLine("Data saved to file: " + fileNamePrefix.ToString());
Console.WriteLine("Press any Key to Exit...\n");
}
}
public void clientProcess(String serverName)
{
try
{
TcpClient tcpClient = new TcpClient(serverName, TCP_PORT);
NetworkStream tcpStream = tcpClient.GetStream();
while (tcpStream.CanRead && blockSize == 0) // Get first block from Server
{
if (tcpStream.DataAvailable) // Expect 4 bytes (length in bytes of capture data expected)
{
Byte[] received = new Byte[4];
int nBytesReceived = tcpStream.Read(received, 0, received.Length);
if (!rcvStarted && nBytesReceived != 0 && totalBytesReceived == 0)
{
rcvStarted = true;
}
blockSize = BitConverter.ToInt32(received, 0);
Console.WriteLine("Connected to remote server");
Console.WriteLine("Received blockSize info = " + blockSize.ToString() + " bytes");
}
}
if (tcpStream.CanWrite && !done) // Acknowledge and Send a GO
{
Byte[] inputToBeSent = System.Text.Encoding.ASCII.GetBytes("GO".ToCharArray());
tcpStream.Write(inputToBeSent, 0, inputToBeSent.Length);
tcpStream.Flush();
}
while (tcpStream.CanRead && !done) // Grab data until all bytes received and save to file.
{
if (tcpStream.DataAvailable)
{
Byte[] received = new Byte[65536];
int nBytesReceived = tcpStream.Read(received, 0, received.Length);
totalBytesReceived += nBytesReceived;
BinarySave(fileNamePrefix, received, nBytesReceived);
if (totalBytesReceived == blockSize) done = true;
}
}
if (tcpStream.CanWrite && done) // Acknowledge full receipt by sending "DONE" and shut down
{
Byte[] inputToBeSent = System.Text.Encoding.ASCII.GetBytes("DONE".ToCharArray());
tcpStream.Write(inputToBeSent, 0, inputToBeSent.Length);
tcpStream.Flush();
}
}
catch (Exception e)
{
Console.WriteLine("An Exception has occurred.");
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
public static string GenerateFileName()
{
DateTime d = DateTime.Now;
string path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
string directory = (System.IO.Path.GetDirectoryName(path)).Substring(6);
return (directory + @"\Capture_" + d.Year.ToString() + d.Month.ToString() +
d.Day.ToString() + " " + d.Hour.ToString() + d.Minute.ToString() +
d.Second.ToString());
}
public void BinarySave(string prefix, byte[] b, int size)
{
string binFileName = prefix + ".bin";
FileStream fs = new FileStream(binFileName, FileMode.Append);
BinaryWriter bW = new BinaryWriter(fs);
bW.Write(b, 0, size);
bW.Close();
}
}
}