Last active
December 23, 2015 07:30
-
-
Save katopz/1a519147c7022d4bfb3f to your computer and use it in GitHub Desktop.
POC C# TCP capture for .NET 4.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Threading; | |
using System.Diagnostics; | |
namespace TR.WebRTC.Services | |
{ | |
public class MiniServer | |
{ | |
private TcpListener mListener; | |
private class Client | |
{ | |
private readonly TcpClient tcpClient; | |
private readonly byte[] buffer; | |
public Client(TcpClient tcpClient) | |
{ | |
this.tcpClient = tcpClient; | |
int bufferSize = tcpClient.ReceiveBufferSize; | |
buffer = new byte[bufferSize]; | |
} | |
public TcpClient TcpClient | |
{ | |
get { return tcpClient; } | |
} | |
public byte[] Buffer | |
{ | |
get { return buffer; } | |
} | |
public NetworkStream NetworkStream | |
{ | |
get { return tcpClient.GetStream(); } | |
} | |
} | |
private class ReadWrapper | |
{ | |
public TcpClient client; | |
public Stream s; | |
public byte[] buffer; | |
public int offset; | |
public int totalRead; | |
public ReadWrapper(TcpClient c, byte[] data) | |
{ | |
buffer = data; | |
offset = 0; | |
totalRead = 0; | |
client = c; | |
s = client.GetStream(); | |
} | |
} | |
public MiniServer() | |
{ | |
List<TcpClient> clientsList = new List<TcpClient>(); | |
Debug.WriteLine("[MiniServer] Start"); | |
mListener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3001)); | |
mListener.Start(); | |
mListener.BeginAcceptTcpClient(AcceptTcpClientCallback, null); | |
} | |
private void AcceptTcpClientCallback(IAsyncResult asyncResult) | |
{ | |
Debug.WriteLine("[MiniServer] AcceptTcpClientCallback"); | |
TcpClient tcpClient; | |
try | |
{ | |
tcpClient = mListener.EndAcceptTcpClient(asyncResult); | |
} | |
catch (Exception ex) | |
{ | |
OnError(mListener, ex); | |
//Stop(); | |
return; | |
} | |
mListener.BeginAcceptTcpClient(AcceptTcpClientCallback, null); | |
Client client = new Client(tcpClient); | |
//connectedClients.Add(client); | |
NetworkStream networkStream = client.NetworkStream; | |
networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, ReadCallback, client); | |
} | |
private void ReadCallback(IAsyncResult asyncResult) | |
{ | |
Debug.WriteLine("[MiniServer] ReadCallback"); | |
Client client = asyncResult.AsyncState as Client; | |
if (client != null) | |
{ | |
NetworkStream networkStream = client.NetworkStream; | |
int read; | |
try | |
{ | |
read = networkStream.EndRead(asyncResult); | |
} | |
catch (Exception ex) | |
{ | |
OnError(client, ex); | |
return; | |
} | |
if (read == 0) | |
{ | |
OnClientDisconnected(client.TcpClient); | |
//connectedClients.Remove(client); | |
return; | |
} | |
byte[] data = new byte[read]; | |
Buffer.BlockCopy(client.Buffer, 0, data, 0, read); | |
OnDataRead(client.TcpClient, data); | |
networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, ReadCallback, client); | |
var streamWriter = new StreamWriter(networkStream); | |
streamWriter.Write("Hello there!"); | |
} | |
} | |
private void OnDataRead(TcpClient tcpClient, byte[] data) | |
{ | |
Debug.WriteLine("[MiniServer] OnDataRead"); | |
} | |
private void OnClientDisconnected(TcpClient tcpClient) | |
{ | |
Debug.WriteLine("[MiniServer] OnClientDisconnected"); | |
} | |
private event EventHandler Error; | |
private void OnError(object sender, Exception ex) | |
{ | |
Debug.WriteLine("[MiniServer] OnError"); | |
EventHandler handler = Error; | |
if (handler != null) | |
{ | |
ErrorEventArgs e = new ErrorEventArgs(ex); | |
handler(sender, e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment