using System;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
namespace Server
{
public static class Program
{
private static byte [] buffer = new byte [512 * 1024];
private static void OnSocketSend(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
client.EndSend(ar);
client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnSocketSend, client);
}
public static void Main( string [] args)
{
Socket server = new Socket(AddressFamily.InterNetwork, SocketType. Stream , ProtocolType.Tcp);
server.Bind( new IPEndPoint(IPAddress.Loopback, 5000));
server.Listen(10);
while ( true )
{
Socket client = server.Accept();
client.SendBufferSize = 2 * buffer.Length;
client.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnSocketSend, client);
Console .WriteLine( "CONNECTED" );
}
}
}
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
namespace Client
{
public static class Program
{
private static byte [] buffer = new byte [512 * 1024];
private static Stopwatch watch = new Stopwatch();
private static long traffic = 0;
private static int step = 0;
private static void OnSocketReceive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
traffic += client.EndReceive(ar);
step++;
if ((step % 1000) == 0)
{
watch.Stop();
Console .WriteLine(
"{0} MB/s" ,
(1000 * (traffic >> 20)) / watch.ElapsedMilliseconds);
watch.Start();
}
client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnSocketReceive, client);
}
public static void Main( string [] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType. Stream , ProtocolType.Tcp);
client.ReceiveBufferSize = 2 * buffer.Length;
client.Connect( new IPEndPoint(IPAddress.Loopback, 5000));
watch.Start();
client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, OnSocketReceive, client);
Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
namespace Server
{
public static class Program
{
private static byte [] buffer = new byte [256 * 1024];
private static void OnPipeSend(IAsyncResult ar)
{
NamedPipeServerStream server = (NamedPipeServerStream)ar.AsyncState;
server.EndWrite(ar);
server.BeginWrite(buffer, 0, buffer.Length, OnPipeSend, server);
}
public static void Main( string [] args)
{
NamedPipeServerStream server = new NamedPipeServerStream( "TestPipe" , PipeDirection.InOut, 1, PipeTransmissionMode. Byte , PipeOptions.Asynchronous, 1024 * 1024, 1024 * 1024);
server.WaitForConnection();
server.BeginWrite(buffer, 0, buffer.Length, OnPipeSend, server);
Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
namespace Client
{
public static class Program
{
private static byte [] buffer = new byte [256 * 1024];
private static Stopwatch watch = new Stopwatch();
private static long traffic = 0;
private static int step = 0;
private static void OnPipeReceive(IAsyncResult ar)
{
NamedPipeClientStream client = (NamedPipeClientStream)ar.AsyncState;
traffic += client.EndRead(ar);
step++;
if ((step % 1000) == 0)
{
watch.Stop();
Console .WriteLine(
"{0} MB/s" ,
(1000 * (traffic >> 20)) / watch.ElapsedMilliseconds);
watch.Start();
}
client.BeginRead(buffer, 0, buffer.Length, OnPipeReceive, client);
}
public static void Main( string [] args)
{
NamedPipeClientStream client = new NamedPipeClientStream( "." , "TestPipe" , PipeDirection.InOut, PipeOptions.Asynchronous);
client.Connect();
watch.Start();
client.BeginRead(buffer, 0, buffer.Length, OnPipeReceive, client);
Console .WriteLine( "CONNECTED" );
Console .ReadLine();
}
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/81067/
All Articles