Skip to content

Instantly share code, notes, and snippets.

@koush koush/Program.cs
Created Apr 28, 2011

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace Proxy
{
class Program
{
static void Main(string[] args)
{
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 8080));
listener.Listen(10);
while (true)
{
var c = listener.Accept();
var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Loopback, 1374));
Thread serverThread = new Thread(o =>
{
byte[] stuff = new byte[1024];
try
{
while (true)
{
int count = s.Receive(stuff);
if (count == 0)
{
s.Close();
return;
}
c.Send(stuff, count, SocketFlags.None);
}
}
catch (Exception)
{
s.Close();
c.Close();
}
});
Thread clientThread = new Thread(o =>
{
try
{
byte[] stuff = new byte[1024];
try
{
while (true)
{
int count = c.Receive(stuff);
if (count == 0)
{
c.Close();
return;
}
s.Send(stuff, count, SocketFlags.None);
}
}
catch (Exception)
{
s.Close();
c.Close();
}
}
catch (Exception)
{
}
});
serverThread.Start();
clientThread.Start();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.