Super simple Socket example
This example uses HTTP 1.1 to retrieve a web page. Code, and then wireshark it to see what the network traffic it generates looks like.
1 //Libraries used
2 using System.Net.Sockets;
3 using System.Text;
4
5 //Request Information
6 string IP = "128.119.245.12";
7 string request = "GET /wireshark-labs/INTRO-wireshark-file1.html\r\n" +
8 "Host: gaia.cs.umass.edu\r\n\r\n";
9
10 //Sending the Request
11 TcpClient socket = new TcpClient();
12 socket.Connect(IP, 80);
13 NetworkStream stream = socket.GetStream();
14 stream.Write(ASCIIEncoding.ASCII.GetBytes(request));
15
16 //Reading the response
17 StringBuilder sb = new StringBuilder();
18 byte[] buffer = new byte[65536];
19 stream.ReadTimeout = 15000; //15 seconds
20 try
21 {
22 do
23 {
24 stream.Read(buffer, 0, buffer.Length);
25 sb.Append(ASCIIEncoding.ASCII.GetString(buffer));
26 } while (stream.DataAvailable);
27 }
28 catch (Exception ex)
29 {
30 sb.Append(ex.Message);
31 }
32 Console.WriteLine(sb.ToString());