Created
January 2, 2013 23:26
-
-
Save gogsbread/4439329 to your computer and use it in GitHub Desktop.
Creating sockets to raise a HTTP request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using System.Net; | |
using System.Net.Sockets; | |
namespace dotNetPlayGround | |
{ | |
class Sockets | |
{ | |
static Socket s = null; | |
const string TEXT = "GET /transformer.html HTTP/1.1\r\nHost: localhost:8080\r\nConnection: keep-alive\r\n\r\n"; | |
static Sockets() | |
{ | |
IPHostEntry localhost = Dns.GetHostEntry("localhost"); | |
IPAddress address = localhost.AddressList[0]; | |
IPEndPoint endpoint = new IPEndPoint(address, 8080); | |
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
s.Connect(endpoint); | |
} | |
static void Main() | |
{ | |
NetworkStream ns = new NetworkStream(s,FileAccess.ReadWrite,true); | |
ns.Write(System.Text.Encoding.UTF8.GetBytes(TEXT),0,System.Text.Encoding.UTF8.GetByteCount(TEXT)); | |
byte[] buffer = new byte[1024]; | |
ns.Read(buffer,0,buffer.Length); | |
Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer)); | |
} | |
static void UsingSocketSend() | |
{ | |
s.Send(System.Text.Encoding.UTF8.GetBytes(TEXT)); | |
byte[] buffer = new byte[1024]; | |
s.Receive(buffer); | |
Console.WriteLine(System.Text.Encoding.UTF8.GetString(buffer)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment