-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessInterface.cs
More file actions
121 lines (100 loc) · 3.74 KB
/
AccessInterface.cs
File metadata and controls
121 lines (100 loc) · 3.74 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace LILO.Shell
{
internal interface CommunicationInterface
{
/* Connects to the server
// Create a CommunicationInterface object
CommunicationInterface ci = new CommunicationInterface();
// Connect to two application using their hostname/IP addresses and ports
ci.Connect("host1", port1, "host2", port2);
// Send data from our application to the other applications
byte[] data1 = Encoding.ASCII.GetBytes("Hello Application 1!");
byte[] data2 = Encoding.ASCII.GetBytes("Hello Application 2!");
ci.SendData(data1, data2);
// Receive data from the other applications
ci.ReceiveDataFromAnotherApplication();
// Finally, disconnect from the applications
ci.Disconnect();
End*/
Socket Socket1 { get; set; }
Socket Socket2 { get; set; }
public void Connect(string host1, int port1, string host2, int port2)
{
// Initialize the socket objects
Socket1 = new Socket(SocketType.Stream, ProtocolType.Tcp);
Socket2 = new Socket(SocketType.Stream, ProtocolType.Tcp);
// Connect to each host using the given ports
Socket1.Connect(host1, port1);
Socket2.Connect(host2, port2);
Socket1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
Socket2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
}
public void SendData(byte[] data1, byte[] data2)
{
// Send the data over each socket
Socket1.Send(data1);
Socket2.Send(data2);
}
public void ReceiveData()
{
// Receive data from each socket
byte[] data1 = new byte[1024];
Socket1.Receive(data1);
byte[] data2 = new byte[1024];
Socket2.Receive(data2);
}
public void Disconnect()
{
// Close each socket connection
try
{
if (Socket1 != null)
Socket1.Close();
} finally
{
Socket2.Close();
}
}
public bool IsConnected(Socket s)
{
return s.Connected;
}
public void SendToDefaultBuffer(byte[] data1, byte[] data2)
{
// Send the data over each socket
Socket1.SendBufferSize = 10024;
Socket1.Send(data1);
Socket2.SendBufferSize = 10024;
Socket2.Send(data2);
}
public void ReceiveFromDefaultBuffer()
{
// Receive data from each socket
byte[] data1 = new byte[1024];
Socket1.ReceiveBufferSize = 1024;
Socket1.Receive(data1);
byte[] data2 = new byte[1024];
Socket2.ReceiveBufferSize = 1024;
Socket2.Receive(data2);
}
public void ReceiveDataFromAnotherApplication()
{
// Receive data from each socket
byte[] data1 = new byte[1024];
Socket1.Receive(data1);
byte[] data2 = new byte[1024];
Socket2.Receive(data2);
}
public void SendDataFromAnotherApplication(byte[] data1, byte[] data2)
{
Socket1.Send(data1);
Socket2.Send(data2);
}
}
}