Ok, good point. So here is the source code of the most important parts:
This happens, when a client joins the server. Here, the data is read until the connection is closed.
Code:
private void ClientThread(TcpClient client)
{
try
{
NetworkStream stream = client.GetStream();
BinaryReader bin = new BinaryReader(stream);
byte[] buffer = new byte[bin.ReadInt32()];
while (true)
{
stream.Read(buffer, 0, buffer.Length);
}
}
catch { }
}
This is the connection code for a TCP client:
Code:
public TcpClient Connect(string ip)
{
try
{
TcpClient client = new TcpClient(ip, Port);
client.NoDelay = true;
return client;
}
catch
{
return null;
}
}
and this is the code, which sends tcp packets:
Code:
private void btnRun_Click(object sender, EventArgs e)
{
btnStop.Text = "Stop";
Stopped = false;
lblSpeed.Text = "0 Bytes/s";
lblSpeedBit.Text = "0 Bits/s";
netGauge.KBitsPerSecond = 0;
netGauge.Invalidate();
lblConnectionLost.Visible = false;
try
{
if (lstComputers.SelectedItems.Count == 1)
{
tabMain.SelectedIndex = 2;
btnNext.Visible = false;
btnRun.Visible = false;
btnStop.Visible = true;
lblConnectedTo.Text = lstComputers.SelectedItems[0].Text;
lblConnectedIP.Text = (string)lstComputers.SelectedItems[0].Tag;
TcpClient client = Analyzer.Connect(lblConnectedIP.Text);
NetworkStream stream = client.GetStream();
BinaryWriter bin = new BinaryWriter(stream);
int len = (int)edtPacketSize.Value;
bin.Write(len);
byte[] data = new byte[len];
if (chkRandomData.Checked)
{
RandomNumberGenerator.Create().GetBytes(data);
}
long transferred = 0, transferredthissecond = 0;
DateTime start = DateTime.Now, lastrefresh = DateTime.Now, lastmeasured = DateTime.Now;
while (!Stopped)
{
stream.Write(data, 0, data.Length);
transferred += data.Length;
transferredthissecond += data.Length;
if ((DateTime.Now - lastmeasured).TotalMilliseconds > 500)
{
lastmeasured = DateTime.Now;
transferredthissecond *= 2;
lblSpeed.Text = GetSizeName(transferredthissecond) + "/s";
lblSpeedBit.Text = GetBitSizeName(transferredthissecond) + "/s";
netGauge.KBitsPerSecond = (int)(transferredthissecond * 8 / 1000);
netGauge.Invalidate();
transferredthissecond = 0;
}
lblTransferred.Text = GetSizeName(transferred);
if ((DateTime.Now - lastrefresh).TotalMilliseconds > 25)
{
TimeSpan timespan = DateTime.Now - start;
lblTimeElapsed.Text = (timespan).ToString().PadRight(11).Substring(0, 11);
int avg = (int)(transferred / timespan.TotalSeconds);
lblAverageSpeed.Text = GetSizeName(avg) + "/s - " + GetBitSizeName(avg) + "/s";
Application.DoEvents();
lastrefresh = DateTime.Now;
}
}
client.Close();
}
}
catch
{
lblConnectionLost.Visible = true;
}
btnStop.Text = "<< First";
}