Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created January 17, 2014 07:04
Show Gist options
  • Select an option

  • Save DevJohnC/8469533 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/8469533 to your computer and use it in GitHub Desktop.
Udp connection testing
//
// Author: John Carruthers (johnc@frag-labs.com)
//
// Copyright (C) 2013 John Carruthers
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Net;
using System.Threading;
using FragLabs.Network.ReliableUdp;
namespace FragLabs.Network.SocketTest
{
class Program
{
/// <summary>
/// Client 1.
/// </summary>
private static UdpConnectionClient _client1;
/// <summary>
/// Client 2.
/// </summary>
private static UdpConnectionClient _client2;
private static Stream _stream1;
private static Stream _stream2;
static void Main(string[] args)
{
var clientEndpoint1 = new IPEndPoint(IPAddress.Loopback, 23124);
var clientEndpoint2 = new IPEndPoint(IPAddress.Loopback, 23125);
var data = new byte[102400];
var recv = new byte[102400];
var mre = new ManualResetEventSlim(false);
using (var server = new UdpConnectionServer(clientEndpoint1))
{
server.ConnectionRequest += (s, a) =>
{
_client2 = a.Accept();
};
server.Start();
using (_client1 = new UdpConnectionClient(clientEndpoint1))
{
_client1.Connect();
_client1.ConnectionEstablished += (sender, eventArgs) => mre.Set();
mre.Wait();
_stream1 = _client1.GetStream();
_stream2 = _client2.GetStream();
var rng = new Random();
var buffer = new byte[10240];
rng.NextBytes(data);
for (var i = 0; i < data.Length; i += buffer.Length)
{
var writeCount = buffer.Length;
if (i + writeCount > data.Length)
writeCount = data.Length - i;
_stream1.Write(data, i, writeCount);
var read = _stream2.Read(buffer, 0, writeCount);
Console.WriteLine("Read {0} bytes", read);
Buffer.BlockCopy(buffer, 0, recv, i, read);
}
_stream1.Close();
_stream2.Close();
}
_client2.Dispose();
}
Console.Write("Checking data...");
var dataOK = true;
for (var i = 0; i < data.Length; i++)
{
if (data[i] != recv[i])
{
dataOK = false;
break;
}
}
if (dataOK) Console.WriteLine("CHECK");
else Console.WriteLine("FAIL");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment