Currently, I'm trying to send many raw requests over a single connection in the faster way. I achieve that using HTTPClient lib but due the need of send raw bytes, I'm using TCPClient.
Some of requisites of this application:
What I have of code:
Method to send all requests
private static async Task SendAllRequests(Dictionary<int, string> requestsDictionary)
{
var client = new TcpClient();
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
await client.ConnectAsync("localhost", 44392);
SslStream sslStream = new SslStream(
client.GetStream(),
false,
null
);
await sslStream.AuthenticateAsClientAsync("localhost");
var tasks = new List<Task<KeyValuePair<int, SslStream>>>();
foreach (var k in requestsDictionary.Keys)
{
tasks.Add(SendStreamAsync(requestString, sslStream));
}
var requestsInfo = await Task.WhenAll(tasks);
}
Method to send and read bytes of a single request
byte[] buffer = new byte[2048];
int bytes;
byte[] request = Encoding.UTF8.GetBytes(requestString);
await sslStream.WriteAsync(request, 0, request.Length);
await sslStream.FlushAsync();
do
{
bytes = await sslStream.ReadAsync(buffer, 0, buffer.Length);
} while (bytes == 2048);
Currently behaviour:
If you want to send multiple requests, you literally just: send multiple requests. It isn't clear what the question is here, or what unexpected behaviour you're seeing. However! A few considerations: