-
Notifications
You must be signed in to change notification settings - Fork 71
/
Program.cs
85 lines (68 loc) · 2.78 KB
/
Program.cs
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
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpHog
{
class Program
{
private const int Port = 9000;
private const int PacketSize = 1024;
static void Main(string[] args)
{
Console.WriteLine("TCP traffic generator");
while (true)
{
Console.WriteLine();
Console.Write("Packets per burst:");
int packetsPerBurst = int.Parse(Console.ReadLine());
Console.Write("Sleep in milliseconds between bursts:");
int sleepMilliseconds = int.Parse(Console.ReadLine());
if (sleepMilliseconds > 0)
{
double estimatedThroughtput = PacketSize * packetsPerBurst / sleepMilliseconds;
Console.Write("Target throughtput {0:n} bytes/sec", estimatedThroughtput);
}
else
Console.WriteLine("No sleep at all");
Console.WriteLine();
GenerateLoad(packetsPerBurst, sleepMilliseconds);
}
}
public static void GenerateLoad(int packetsPerBurst, int sleepMilliseconds)
{
IPAddress address = (from a in Dns.GetHostAddresses(Environment.MachineName)
where a.AddressFamily == AddressFamily.InterNetwork
select a).FirstOrDefault();
Console.WriteLine("IP Address is: {0}", address.ToString());
Socket listenSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(new IPEndPoint(address, Port));
listenSocket.Listen(10);
Socket sendSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
sendSocket.Connect(new IPEndPoint(address, Port));
NetworkStream writeStream = new NetworkStream(sendSocket, true);
Socket receiveSocket = listenSocket.Accept();
NetworkStream readStream = new NetworkStream(receiveSocket, true);
BinaryReader reader = new BinaryReader(readStream);
byte[] buffer = new byte[PacketSize];
while (!Console.KeyAvailable)
{
for (int i=0; i<packetsPerBurst; i++)
{
writeStream.Write(buffer, 0, buffer.Length);
readStream.Read(buffer, 0, buffer.Length);
}
if (sleepMilliseconds > 0)
{
Thread.Sleep(sleepMilliseconds);
}
}
Console.ReadKey();
writeStream.Dispose();
readStream.Dispose();
listenSocket.Dispose();
}
}
}