Network Programming
The internet is very popular
in this day and age, and there are many different applications that can be
written to take advantage of that. One of those is a web browser. We’re going to
start off simple by making a web browser for the poor man who can run only
console applications.
The first thing you would
need to do would be to open a connection to the remote host. Such a connection
is made by using a socket. A socket is a two-way connection between two
machines. The socket you create will connect to the remote host on a certain
port number depending on the protocol you need to use – for HTTP, the port
number is 80. Once connected to the remote host, you will be able to send and
receive information to and from the remote host. The information you receive
will be in bytes and since it is strings we want, you are going to have to
convert them into strings. Here’s the code:
|
using System; using System.Text; using System.Collections; using System.Data; using System.Net.Sockets; using System.Threading; using System.IO; using System.Net; namespace PoorMansBrowser { /// <summary> /// Summary description
for Browser. /// </summary> class Browser { /// <summary> /// The main entry point
for the application. /// </summary> [STAThread] static void Main(string[] args) { Console.WriteLine
("Please enter the url you wish to see the html for"); string readIn; readIn
= Console.ReadLine(); TcpClient
myBrowser = new TcpClient (readIn, 80); NetworkStream
clientStream = myBrowser.GetStream (); string command = "GET /index.html
HTTP/1.0\n\n"; BinaryWriter
clientWriter = new BinaryWriter
(clientStream); BinaryReader
clientReader = new BinaryReader (clientStream); byte [] writeByte = new
Byte [command.Length]; writeByte
= Encoding.ASCII.GetBytes(command); clientWriter.Write(writeByte); byte [] readByte = new
Byte [1024]; clientReader.Read
(readByte, 0, 1024); string html = Encoding.ASCII.GetString(readByte); Console.WriteLine(html); } } } |
In order to set up a socket in C#, you will need to tell the compiler that you are using System.Net.Sockets thus importing System.Net.Sockets. Now, let’s create and set up our socket. For this lab we will be using TcpClient, a type of socket that is found in System.Net. First we need to declare a TcpClient and set it equal to new TcpClient (readIn, 80). The variable readIn is used to hold the URL for the website the user wishes to visit, and the 80 tells the application that the TcpClient is to try to connect on port 80. Next, we need to set up a NetworkStream to be able to write to and read from the host machine. When we create our NetworkStream, we need to set it equal to NameOfTcpClient.GetStream so that the NetworkStream will know which open socket to read from and write to. Now, we need to create a BinaryWriter and a BinaryReader which facilitates our reading from and writing to the NetworkStream which is established by setting the writer and reader equal to a new BinaryWriter or BinaryWriter (clientStream) so that the writer and reader will know which NetworkStream to write to and read from. Also in order for us to send commands to and receive data from the host machine, we will need to create two byte arrays. One we will have the size of 1024 and the other we will set to the length of the command we will be sending to the host. Now, we need to convert the command we will be sending to the host into bytes. We do this by using Encoding.ASCII.GetBytes(command). Now we can pass the command to the BinaryWriter to send the command to the host machine. Now we need to pass in the byte array we created to the BinaryReader along with a 0 to tell it to start inserting data into the byte array at the 0th element of the array, and we also need to pass in 1024 which tells the BinaryReader the size of the byte array. Now we need to convert the data stored in the byte array to a string (since is the type of data we need). This is accomplished through the use of the command string readByte = Encoding.ASCII.GetString(byteMe). Now that we have converted the returned data from bytes to a string we can display the returned information to the user and end the application.