forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientSendBytes.cs
More file actions
62 lines (49 loc) · 1.76 KB
/
ClientSendBytes.cs
File metadata and controls
62 lines (49 loc) · 1.76 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
using System;
using System.Net;
using SimplSockets;
namespace SimplSocketsClient
{
public class ClientSendBytes
{
public void Start()
{
CreateServer();
CreateClient();
}
void CreateServer()
{
// Create the server
var server = new SimplSocketServer();
// Create a callback for received array
server.MessageReceived += (s, e) =>
{
// Get the message
var receivedMessage = e.ReceivedMessage;
Console.WriteLine($"Server received message of {receivedMessage.Length} bytes");
// Return the message to the pool
receivedMessage.Return();
};
// Start listening for client connections on loopback endpoint
server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
}
void CreateClient()
{
// Create the client
var client = new SimplSocketClient();
// Make the client connect automatically
client.AutoConnect();
// Wait until the connection is actually made
client.WaitForConnection();
// Create a byte array to send
var arrayToSend1 = new byte[1000];
// Send it
client.Send(arrayToSend1);
// Get a byte array from the memory pool
var arrayToSend2 = PooledMessage.Rent(1000);
client.Send(arrayToSend2);
// We will not dispose the message directly, since it may still need to be send
// Instead we use the ReturnAfterSend, which will return the message to the pool after sending
arrayToSend2.ReturnAfterSend();
}
}
}