-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketUnixListener.cs
59 lines (48 loc) · 1.79 KB
/
SocketUnixListener.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
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketUnixListener : SocketIPListener
{
public SocketUnixListener(Socket clientSocket): base(clientSocket) {}
public new void newClient()
{
// Data buffer
byte[] bytes = new Byte[1024];
string? data = null;
string history = "";
while (true)
{
EndPoint? sender = clientSocket.RemoteEndPoint;
int numByte = clientSocket.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, numByte);
history += data;
if (data.IndexOf("\n") > -1)
{
// Console.WriteLine("From " + (((IPEndPoint) sender).Address.ToString() ?? "-") + " : " + data);
clientSocket.Send(Encoding.ASCII.GetBytes("\nPong : " + data + "\n"));
if (data.IndexOf("<EOF>") > -1)
data = "<EOF>";
else
data = "";
}
if (data.IndexOf("<EOF>") > -1)
{
// Console.WriteLine("From " + (((IPEndPoint) sender).Address.ToString() ?? "-")
// + " : " + data);
clientSocket.Send(Encoding.ASCII.GetBytes("\nPong : " + data + "\n"));
break;
}
}
Console.WriteLine("Text received -> {0} ", history);
byte[] message = Encoding.ASCII.GetBytes("\nTest Server");
// Send a message to Client
// using Send() method
clientSocket.Send(message);
// Close client Socket using the
// Close() method. After closing,
// we can use the closed Socket
// for a new Client Connection
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}