-
Notifications
You must be signed in to change notification settings - Fork 0
/
OIMNTFSServer.cs
101 lines (86 loc) · 3.21 KB
/
OIMNTFSServer.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace OIMNTFS_Service
{
public class OIMNTFSServer
{
TcpListener listener;
IPAddress ipAddress = IPAddress.Any;
int port;
bool runLoops = true;
EventLog eventLog;
private OIMNTFSPartialScanner OIMNTFSScanner;
public OIMNTFSServer(OIMNTFSPartialScanner scanner, int listentoport = 16383)
{
eventLog = new EventLog("OIMNTFS Server");
OIMNTFSScanner = scanner;
port = listentoport;
Thread listener = new Thread(new ThreadStart(acceptConnections));
listener.Start();
eventLog.Write("Server started");
}
void acceptConnections ()
{
listener = new TcpListener(ipAddress, port);
eventLog.Write("Listening on port " + port + "...");
try
{
listener.Start();
while (runLoops)
{
while (!listener.Pending()) { Thread.Sleep(100); }
// fork server thread
(new Thread(new ThreadStart(processCommands))).Start();
Thread.Sleep(100);
}
}
catch (Exception ex)
{
throw new Exception("Fehler bei Verbindungserkennung", ex);
}
}
void sendText (Socket socket, string text)
{
Byte[] bytesSent = Encoding.ASCII.GetBytes(text);
socket.Send(bytesSent, SocketFlags.None);
}
string receiveText(Socket socket)
{
Byte[] bytesReceived = new Byte[4096];
int bytes = 0;
string text = "";
do
{
bytes = socket.Receive(bytesReceived, bytesReceived.Length, SocketFlags.None);
// kovertiere die Byte Daten in einen string
eventLog.Write("Received: {0}", Encoding.ASCII.GetString(bytesReceived, 0, bytes));
text = text + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
} while (bytes > 0 && !text.Contains("\n"));
return text;
}
void processCommands()
{
Socket socket = listener.AcceptSocket();
eventLog.Write("Neue Client-Verbindung (" +
"IP: " + socket.RemoteEndPoint + ", " +
"Port " + ((IPEndPoint)socket.LocalEndPoint).Port.ToString() + ")");
sendText(socket, "Nice to meet you!\r\nPlease type path to be scanned and finish with <ENTER>.\r\n\n");
string scanPath = receiveText(socket);
sendText(socket, string.Format("\n\rScanning folder \n\r {0} as per your request. Stand by.\r\n\n", scanPath));
// insert into database for scanning
try
{
socket.Close();
}
catch
{
eventLog.Write("Verbindung zum Client beendet");
}
}
}
}