Last active
December 3, 2020 14:07
-
-
Save Leonardo-Ferreira/5016a18277ead3cd81aecc8a735b2483 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace SendDoesBlock | |
{ | |
class Program | |
{ | |
static List<TimeSpan> col = new List<TimeSpan>(200); | |
static List<Task> tasks = new List<Task>(200); | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello World!"); | |
for (int i = 0; i < 50; i++) | |
{ | |
SocketSendReceive("australiatesting.azurewebsites.net", 80, false); | |
Console.WriteLine("NON BLOCKING. Avg = " + col.Average(i => i.Ticks)); | |
col.Clear(); | |
SocketSendReceive("australiatesting.azurewebsites.net", 80, true); | |
Console.WriteLine("BLOCKING. Avg = " + col.Average(i => i.Ticks)); | |
col.Clear(); | |
tasks.Clear(); | |
SocketSendReceiveAsync("australiatesting.azurewebsites.net", 80); | |
Console.WriteLine("ASYNC. Avg = " + col.Average(i => i.Ticks)); | |
col.Clear(); | |
Thread.Sleep(1000); | |
} | |
Console.ReadKey(); | |
} | |
private static Socket ConnectSocket(string server, int port) | |
{ | |
Socket s = null; | |
IPHostEntry hostEntry = null; | |
// Get host related information. | |
hostEntry = Dns.GetHostEntry(server); | |
// Loop through the AddressList to obtain the supported AddressFamily. This is to avoid | |
// an exception that occurs when the host IP Address is not compatible with the address family | |
// (typical in the IPv6 case). | |
foreach (IPAddress address in hostEntry.AddressList) | |
{ | |
IPEndPoint ipe = new IPEndPoint(address, port); | |
Socket tempSocket = | |
new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); | |
tempSocket.Connect(ipe); | |
if (tempSocket.Connected) | |
{ | |
s = tempSocket; | |
break; | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
return s; | |
} | |
// This method requests the home page content for the specified server. | |
private static void SocketSendReceive(string server, int port, bool blocking) | |
{ | |
Random rnd = new Random(); | |
string request = "GET /?{0}={1} HTTP/1.1\r\nHost: " + server + | |
"\r\nConnection: Close\r\nCache-Control: no-store\r\n\r\n"; | |
Byte[] bytesSent; | |
// Create a socket connection with the specified server and port. | |
using (Socket s = ConnectSocket(server, port)) | |
{ | |
s.Blocking = blocking; | |
s.NoDelay = true; | |
if (s == null) | |
return; | |
// Send request to the server. | |
var st = Stopwatch.StartNew(); | |
var i = 0; | |
while (i < 200) | |
{ | |
bytesSent = Encoding.UTF8.GetBytes(string.Format(request, rnd.Next(999999, int.MaxValue), rnd.Next(9999999, int.MaxValue))); | |
s.Send(bytesSent, bytesSent.Length, 0,SocketFlags.None, out var e); | |
if(e != SocketError.Success) | |
{ | |
Console.Write("ERROR"); | |
} | |
col.Add(st.Elapsed); | |
st.Restart(); | |
i++; | |
} | |
//Thread.Sleep(3000); | |
} | |
} | |
private static void SocketSendReceiveAsync(string server, int port) | |
{ | |
Random rnd = new Random(); | |
string request = "GET /?{0}={1} HTTP/1.1\r\nHost: " + server + | |
"\r\nConnection: Close\r\nCache-Control: no-store\r\n\r\n"; | |
ReadOnlyMemory<byte> toSend = new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes(string.Format(request, rnd.Next(999999, int.MaxValue), rnd.Next(9999999, int.MaxValue)))); | |
// Create a socket connection with the specified server and port. | |
using (Socket s = ConnectSocket(server, port)) | |
{ | |
s.NoDelay = true; | |
if (s == null) | |
return; | |
// Send request to the server. | |
var st = Stopwatch.StartNew(); | |
var i = 0; | |
while (i < 200) | |
{ | |
s.SendAsync(toSend, SocketFlags.None); | |
col.Add(st.Elapsed); | |
st.Restart(); | |
i++; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment