forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServerSendBytes.cs
More file actions
62 lines (50 loc) · 1.88 KB
/
ServerSendBytes.cs
File metadata and controls
62 lines (50 loc) · 1.88 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 ServerSendBytes
{
public void Start()
{
CreateClient();
CreateServer();
}
void CreateServer()
{
// Create the server
var server = new SimplSocketServer();
// Start listening for client connections on loopback end poiny
server.Listen(new IPEndPoint(IPAddress.Loopback, 5000));
// Wait until a new client has connected
var connectedClient = server.WaitForNewClient();
// Create a byte array to send
var arrayToSend1 = new byte[1000];
// Send it
Console.WriteLine($"Server is going to send message of {arrayToSend1.Length} bytes");
server.Send(arrayToSend1, connectedClient);
// Get a byte array from the memory pool
var arrayToSend2 = PooledMessage.Rent(1000);
Console.WriteLine($"Server is going to send a pooled message of {arrayToSend1.Length} bytes");
server.Send(arrayToSend2, connectedClient);
// Return message to pool
arrayToSend2.ReturnAfterSend();
}
void CreateClient()
{
// Create the client
var client = new SimplSocketClient();
// Make the client connect automatically
client.AutoConnect();
// Create a callback for received data
client.MessageReceived += (s, e) =>
{
// Get the message
var receivedMessage = e.ReceivedMessage;
Console.WriteLine($"Client received message of {receivedMessage.Length} bytes");
// Return the message to the pool
receivedMessage.Return();
};
}
}
}