⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientform.cs

📁 rudp可靠保障得udp传输
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

using Helper.Net.RUDP;
using Helper.Net.UDT;

namespace Test.Application
{
	public partial class ClientForm : Form
	{
		#region Variables

		Thread thread;
		System.Windows.Forms.Timer timer;

		//---- Sockets
		RUDPSocket rudpSocket;
		Socket tcpSocket;
		UDTSocket udtSocket;

		//---- Rate
		private string _rate = "";

		long TotalBytes = 0;

		bool _isReliable = true;

		Stopwatch _stopWatch = new Stopwatch();

		#endregion

		#region Constructor

		public ClientForm()
		{
			InitializeComponent();
			txtIPAddress.Text = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
		}

		#endregion

		#region Events...

		private void btnStart_Click(object sender, EventArgs e)
		{
			TotalBytes = 0;
			_stopWatch.Stop();
			_stopWatch.Reset();

			btnStart.Enabled = false;
			btnStop.Enabled = true;
			if (rbRUDP.Checked)
			{
				rudpSocket = new RUDPSocket();
				thread = new Thread(new ThreadStart(StartBenchmark_RUDP));
			}
			else if (rbUDT.Checked)
			{
				udtSocket = new UDTSocket(AddressFamily.InterNetwork);
				thread = new Thread(new ThreadStart(StartBenchmark_UDT));
			}
			else
			{
				tcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
				thread = new Thread(new ThreadStart(StartBenchmark_TCP));
			}
			thread.Start();

			if (timer != null)
				timer.Stop();

			timer = new System.Windows.Forms.Timer();
			timer.Interval = 1000;
			timer.Tick += new EventHandler(timer_Tick);
			timer.Start();
		}

		private void btnStop_Click(object sender, EventArgs e)
		{
			OnTransportDisconnected_RUDP();
			OnTransportDisconnected_UDT();
			OnTransportDisconnected_TCP();
			try
			{
				thread.Abort();
			}
			catch { }
			btnStart.Enabled = true;
			btnStop.Enabled = false;

			timer.Stop();
			TotalBytes = 0;
			_rate = "";
			_stopWatch.Stop();
		}

		void timer_Tick(object sender, EventArgs e)
		{
			txtRate.Text = _rate;
			txtTotalBytes.Text = "" + (TotalBytes / 1024);
			if (!_stopWatch.IsRunning)
				txtSeconds.Text = "0";
			else
				txtSeconds.Text = "" + _stopWatch.ElapsedMilliseconds / 1000;

			if (rudpSocket != null)
			{
				txtPMTU.Text = rudpSocket.NetworkInformation.PathMTU.ToString();
				txtSCWND.Text = "" + (rudpSocket.NetworkInformation.SendCongestionWindow);
				txtRTT.Text = ((double)rudpSocket.NetworkInformation.RTT / 1000).ToString();
				txtRTO.Text = ((double)rudpSocket.NetworkInformation.RTO / 1000).ToString();
				txtBDP.Text = (1000.0 * rudpSocket.NetworkInformation.BandWidth / (1024*1024)).ToString();
			}
		}

		#endregion

		#region StartBenchmark_RUDP

		private void StartBenchmark_RUDP()
		{
			_isReliable = chkReliable.Checked;

			//---- Prepare
			IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(txtIPAddress.Text), Int32.Parse(txtIPPort.Text));
			try
			{
				rudpSocket.Connect(endPoint);
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
				return;
			}

			_stopWatch.Start();

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			RUDPSocketError error = RUDPSocketError.Success;
			rudpSocket.BeginSend(buffer, 0, buffer.Length, out error, new AsyncCallback(EndOfSend_RUDP), null, _isReliable);

			if (error != RUDPSocketError.Success)
			{
				OnTransportDisconnected_RUDP();
				return;
			}

			/*
			while (true)
			{
				if (rudpSocket.Send(buffer, 0, buffer.Length) < 0)
				{
					OnTransportDisconnected_RUDP();
					return;
				}

				Thread.Sleep(1);
			}
			*/
		}

		#endregion

		#region StartBenchmark_TCP

		private void StartBenchmark_TCP()
		{
			//---- Prepare
			IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(txtIPAddress.Text), Int32.Parse(txtIPPort.Text));
			try
			{
				tcpSocket.Connect(endPoint);
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
				return;
			}

			_stopWatch.Start();

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			SocketError error;

			while (true)
			{
				error = SocketError.Success;
				tcpSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, out error, new AsyncCallback(EndOfSend_TCP), null);

				if (error == SocketError.Success)
					return;

				if (error != SocketError.NoBufferSpaceAvailable)
				{
					OnTransportDisconnected_TCP();
					return;
				}

				Thread.Sleep(1);
			}
		}

		#endregion

		#region StartBenchmark_UDT

		private void StartBenchmark_UDT()
		{
			//---- Prepare
			IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(txtIPAddress.Text), Int32.Parse(txtIPPort.Text));
			try
			{
				udtSocket.Connect(endPoint);
			}
			catch (Exception e)
			{
				System.Console.WriteLine(e.Message);
				return;
			}

			_stopWatch.Start();

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			SocketError error;

			while (true)
			{
				error = SocketError.Success;
				udtSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, out error, new AsyncCallback(EndOfSend_UDT), null);

				if (error == SocketError.Success)
					return;

				if (error != SocketError.NoBufferSpaceAvailable)
				{
					OnTransportDisconnected_UDT();
					return;
				}

				Thread.Sleep(1);
			}
		}

		#endregion

		#region EndOfSend_RUDP

		private void EndOfSend_RUDP(IAsyncResult result)
		{
			int bytes = -1;
			try
			{
				bytes = rudpSocket.EndSend(result);
			}
			catch (SocketException socketException)
			{
				if (socketException.ErrorCode == 10054)
				{
					OnTransportDisconnected_RUDP();
					return;
				}

				System.Console.WriteLine("Error " + socketException.Message);
			}
			catch (ObjectDisposedException ode)
			{
				System.Console.WriteLine("Error " + ode.Message);
			}

			TotalBytes += bytes;

			double seconds = _stopWatch.ElapsedMilliseconds / 1000;
			if (seconds > 0)
			{
				double rate = ((double)TotalBytes) / 1024 / seconds;
				_rate = ((int)rate).ToString() + " Kbytes/seconds";
			}

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			RUDPSocketError error = RUDPSocketError.Success;
			rudpSocket.BeginSend(buffer, 0, buffer.Length, out error, new AsyncCallback(EndOfSend_RUDP), null, _isReliable);

			if (error != RUDPSocketError.Success)
			{
				OnTransportDisconnected_RUDP();
				return;
			}
		}

		#endregion

		#region EndOfSend_TCP

		private void EndOfSend_TCP(IAsyncResult result)
		{
			int bytes = -1;
			try
			{
				bytes = tcpSocket.EndSend(result);
			}
			catch (SocketException socketException)
			{
				if (socketException.ErrorCode == 10054)
				{
					OnTransportDisconnected_TCP();
					return;
				}

				System.Console.WriteLine("Error " + socketException.Message);
			}
			catch (ObjectDisposedException ode)
			{
				System.Console.WriteLine("Error " + ode.Message);
			}

			TotalBytes += bytes;

			double seconds = _stopWatch.ElapsedMilliseconds / 1000;
			if (seconds > 0)
			{
				double rate = ((double)TotalBytes) / 1024 / seconds;
				_rate = ((int)rate).ToString() + " Kbytes/seconds";
			}

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			SocketError error;

			while (true)
			{
				error = SocketError.Success;
				tcpSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, out error, new AsyncCallback(EndOfSend_TCP), null);

				if (!tcpSocket.Connected)
					throw new Exception("Connection closed");

				if (error == SocketError.Success)
					return;

				if (error != SocketError.NoBufferSpaceAvailable)
				{
					OnTransportDisconnected_TCP();
					return;
				}

				Thread.Sleep(1);
			}
		}

		#endregion

		#region EndOfSend_UDT

		private void EndOfSend_UDT(IAsyncResult result)
		{
			int bytes = -1;
			try
			{
				bytes = udtSocket.EndSend(result);
			}
			catch (SocketException socketException)
			{
				if (socketException.ErrorCode == 10054)
				{
					OnTransportDisconnected_UDT();
					return;
				}

				System.Console.WriteLine("Error " + socketException.Message);
			}
			catch (ObjectDisposedException ode)
			{
				System.Console.WriteLine("Error " + ode.Message);
			}

			TotalBytes += bytes;

			double seconds = _stopWatch.ElapsedMilliseconds / 1000;
			if (seconds > 0)
			{
				double rate = ((double)TotalBytes) / 1024 / seconds;
				_rate = ((int)rate).ToString() + " Kbytes/seconds";
			}

			//---- 2 Send
			byte[] buffer = new byte[32 * 1024];

			SocketError error;

			while (true)
			{
				error = SocketError.Success;
				udtSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, out error, new AsyncCallback(EndOfSend_UDT), null);

				if (!udtSocket.Connected)
					throw new Exception("Connection closed");

				if (error == SocketError.Success)
					return;

				if (error != SocketError.NoBufferSpaceAvailable)
				{
					OnTransportDisconnected_UDT();
					return;
				}

				Thread.Sleep(1);
			}
		}

		#endregion

		#region OnTransportDisconnected_RUDP

		private void OnTransportDisconnected_RUDP()
		{
			if (rudpSocket == null)
				return;
			try
			{
				rudpSocket.Close();
			}
			catch { }
		}

		#endregion

		#region OnTransportDisconnected_TCP

		private void OnTransportDisconnected_TCP()
		{
			if (tcpSocket == null)
				return;
			try
			{
				tcpSocket.Close();
			}
			catch { }
		}

		#endregion

		#region OnTransportDisconnected_UDT

		private void OnTransportDisconnected_UDT()
		{
			if (udtSocket == null)
				return;
			try
			{
				if (udtSocket != null)
					udtSocket.Close();
			}
			catch { }
		}

		#endregion

		#region Closing

		private void TCPClientForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			OnTransportDisconnected_RUDP();
			OnTransportDisconnected_TCP();

			try
			{
				thread.Abort();
			}
			catch { }
		}

		#endregion

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -