I would like to create a simple Proxy Server, but I'm in trouble.
I don't know how to show page content in a web browser.
In that example a web browser recives only previously taken code (s.Receive(b)).
The code is interpreted by the web browser and that is why does not appear along with a page.
class Program
{
static void Main(string[] args)
{
Program program = new Program();
IPAddress ipAddress = IPAddress.Any;
IPEndPoint adress = new IPEndPoint(ipAddress, 8000);
TcpListener listener = new TcpListener(adress);
string message = "";
listener.Start();
Console.WriteLine("Server Proxy - start");
Console.WriteLine("Connect...");
while (true)
{
Socket s = listener.AcceptSocket();
Console.WriteLine("RemoteEndPoint: " + s.RemoteEndPoint);
byte[] b = new byte[65535];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
message += Convert.ToChar(b);
Console.WriteLine("Message {0}", message);
program.sendmessage(s, message);
Console.WriteLine("\nSend to : " + s.RemoteEndPoint);
s.Close();
}
}
private void sendmessage(Socket s, string message)
{
Byte[] Buffer = new Byte[message.Length + 1];
UTF8Encoding encoding = new UTF8Encoding();
int length = encoding.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}
}
I don't know how to show page content in a web browser.
In that example a web browser recives only previously taken code (s.Receive(b)).
The code is interpreted by the web browser and that is why does not appear along with a page.
class Program
{
static void Main(string[] args)
{
Program program = new Program();
IPAddress ipAddress = IPAddress.Any;
IPEndPoint adress = new IPEndPoint(ipAddress, 8000);
TcpListener listener = new TcpListener(adress);
string message = "";
listener.Start();
Console.WriteLine("Server Proxy - start");
Console.WriteLine("Connect...");
while (true)
{
Socket s = listener.AcceptSocket();
Console.WriteLine("RemoteEndPoint: " + s.RemoteEndPoint);
byte[] b = new byte[65535];
int k = s.Receive(b);
for (int i = 0; i < k; i++)
message += Convert.ToChar(b);
Console.WriteLine("Message {0}", message);
program.sendmessage(s, message);
Console.WriteLine("\nSend to : " + s.RemoteEndPoint);
s.Close();
}
}
private void sendmessage(Socket s, string message)
{
Byte[] Buffer = new Byte[message.Length + 1];
UTF8Encoding encoding = new UTF8Encoding();
int length = encoding.GetBytes(message, 0, message.Length, Buffer, 0);
s.Send(Buffer, length, 0);
}
}