-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSharpClient.cs
More file actions
143 lines (132 loc) · 4.04 KB
/
SharpClient.cs
File metadata and controls
143 lines (132 loc) · 4.04 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace NahroTo.SharpFileTransferrer
{
public class SharpClient
{
private const int MAX_PACKET_SIZE = 32768;
private class Address : IDisposable
{
public string ip;
public int port;
public IEnumerable<string> files;
public CancellationTokenSource cts;
public Address(string ip, int port, IEnumerable<string> files)
{
this.ip = ip;
this.port = port;
this.files = files;
cts = new CancellationTokenSource();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing && (cts != null))
{
cts.Cancel();
cts.Dispose();
cts = null;
}
}
}
public static void SendFile(string ipadress, int port, string filePath)
{
ThreadPool.QueueUserWorkItem(token =>
{
ConnectAsClient(new Address(ipadress, port, new string[] { filePath }));
});
Thread.Sleep(500);
}
public static void SendFiles(string ipadress, int port, IEnumerable<string> filePaths)
{
ThreadPool.QueueUserWorkItem(token =>
{
ConnectAsClient(new Address(ipadress, port, filePaths));
});
Thread.Sleep(500);
}
private static void ConnectAsClient(Address param) // #3
{
using (TcpClient client = new TcpClient())
{
client.Connect(IPAddress.Parse(param.ip), param.port);
using (NetworkStream stream = client.GetStream())
{
bool newFile = true;
stream.Write(BitConverter.GetBytes(param.files.Count()), 0, 4);
for (int i = 0; i < param.files.Count(); i++)
{
string filePath = param.files.ElementAt(i);
int fileLength = File.ReadAllBytes(filePath).Length;
double amountFilePackets = 1; // one data info packet
if (fileLength > MAX_PACKET_SIZE)
{
amountFilePackets = Math.Ceiling((double)fileLength / MAX_PACKET_SIZE);
}
if (newFile)
{
// WRITE PACKET 0 (FILE INFO)
byte[] dataFilename = Encoding.ASCII.GetBytes(System.IO.Path.GetFileName(filePath));
int packetLength0 = dataFilename.Length;
// write packetlength0 (4 bytes)
stream.Write(BitConverter.GetBytes(packetLength0), 0, 4);
byte[] header0 = { 0 };
// write header0 (1 byte)
stream.Write(header0, 0, 1);
// write dataFilename (packetlength0 bytes)
stream.Write(dataFilename, 0, packetLength0);
newFile = false;
}
// WRITE PACKET 1 (FILE DATA)
int packetsLeftToWrite = (int)amountFilePackets;
int packetsWritten = 0;
int remainingBytes;
while (packetsLeftToWrite != 0)
{
byte[] dataFilePacket;
remainingBytes = File.ReadAllBytes(filePath).Skip(packetsWritten * MAX_PACKET_SIZE).ToArray().Length;
if (!(remainingBytes <= MAX_PACKET_SIZE)) // if remainig bytes are NOT enough for 1 packet
{
dataFilePacket = File.ReadAllBytes(filePath).Skip(packetsWritten * MAX_PACKET_SIZE).Take(MAX_PACKET_SIZE).ToArray();
}
else // if last packet
{
dataFilePacket = File.ReadAllBytes(filePath).Skip(packetsWritten * MAX_PACKET_SIZE).Take(File.ReadAllBytes(filePath).Length - packetsWritten * MAX_PACKET_SIZE).ToArray();
newFile = true;
}
int packetLength1 = dataFilePacket.Length;
// write packetlength1 (4 bytes)
stream.Write(BitConverter.GetBytes(packetLength1), 0, 4);
packetsLeftToWrite--;
byte[] header = new byte[1];
if (packetsLeftToWrite == 0)
{
header[0] = 2;
}
else
{
header[0] = 1;
}
// write header1 (1 byte)
stream.Write(header, 0, 1);
// write dataFilePacket
stream.Write(dataFilePacket, 0, packetLength1);
packetsWritten++;
}
}
}
}
param.Dispose();
}
}
}