// The Server
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Collections;
namespace GrabPageServer
{
class GrabPageServer
{
static void Send(string message, NetworkStream socketStream)
{
char[] theMessage;
theMessage = message.ToCharArray();
for ( int i = 0; i < theMessage.Length; i++ ) socketStream.WriteByte( (byte) theMessage[i] );
}
static void Main()
{
TcpListener server = null;
Socket connection = null;
int port = 80;
NetworkStream socketStream = null;
// BinaryWriter writer = null;
// BinaryReader reader = null;
ArrayList theRequest = new ArrayList();
string tehRequest = "";
// wait for a client connection and display the text the client sends
try
{
// create TcpListener
server = new TcpListener( port );
// start TcpListener to wait for the next connection request
server.Start();
Console.WriteLine("Waiting for connection...\r\n");
// establish connection upon client request
while ( true )
{
// accept an incoming connection
connection = server.AcceptSocket();
// create NetworkStream object associated with socket
socketStream = new NetworkStream( connection );
// chain it to BinaryWriter and BinaryReader
// writer = new BinaryWriter( socketStream );
// reader = new BinaryReader( socketStream );
// get client's IP address and port
IPEndPoint ipend = (IPEndPoint) connection.RemoteEndPoint;
// read the HTTP request from the client
int c = 0;
int d = 0;
while (d != ((int) '\r' + (int) '\n'))
{
d = c;
c = socketStream.ReadByte();
if (c == '\r') d += c;
theRequest.Add((char) c);
}
foreach (char message in theRequest) tehRequest += message;
theRequest.Clear();
// show where the client is from and the HTTP request
Console.WriteLine( "Connection from " + ipend.Address + " via its port " + ipend.Port + " with the following request: ");
Console.Write( tehRequest );
tehRequest = "";
// send the greeting message to the client
Send("HTTP/1.1 200 OK\r\n", socketStream);
Send("Date: " + System.DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT\r\n", socketStream);
Send("Server: CrazyHTTP/1.0\r\n", socketStream);
Send("Connection: close\r\n", socketStream);
Send("Content-Type: text/html\r\n\r\n", socketStream);
Send("<html><h1>Greetings from the Server!</h1></html>\r\n", socketStream);
// close the connection
socketStream.Close();
connection.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
// The Client
using System;
using System.Net.Sockets;
using System.IO;
namespace GrabPageClient
{
class GrabPageClient
{
static void Send(string message, NetworkStream NetIO)
{
char[] theMessage;
theMessage = message.ToCharArray();
for ( int i = 0; i < theMessage.Length; i++ ) NetIO.WriteByte( (byte) theMessage[i] );
}
static void Main()
{
TcpClient client = null;
string hostname = "";
string hostURL = "";
char[] hostURLArray;
int port = 80;
NetworkStream netIO = null;
// BinaryWriter writer = null;
// BinaryReader reader = null;
string message = "";
Console.Write("\r\nEnter server hostname or IP address (. to end): ");
hostname = Console.ReadLine();
while (hostname != ".")
{
try
{
Console.WriteLine( "Attempting connection..." );
client = new TcpClient();
client.Connect( hostname, port );
Console.WriteLine( "\r\nConnected to Web server at " + hostname );
netIO = client.GetStream();
// writer = new BinaryWriter( netIO );
// reader = new BinaryReader( netIO );
Console.WriteLine( "\r\nGot I/O streams." );
try
{
//hostURL = "GET / HTTP/1.0 \r\n\r\n";
//hostURLArray = hostURL.ToCharArray();
//writer.Write( hostURLArray );
Send("GET / HTTP/1.0 \r\n\r\n", netIO);
Console.WriteLine("Sent request..\r\n");
int c = 0;
while ( c != -1 )
{
c = netIO.ReadByte();
message = ((char) c).ToString();
Console.Write( message );
}
}
// handle exception if error in reading server data
catch ( Exception )
{
Console.WriteLine("\r\nThe server stopped the connection!");
System.Environment.Exit( System.Environment.ExitCode );
}
}
// handle exception for error in establishing connection
catch ( Exception )
{
Console.WriteLine( "\r\n" );
}
netIO.Close();
client.Close();
Console.Write("\r\nEnter server hostname or IP address (. to end): ");
hostname = Console.ReadLine();
}
}
}
}