Skip to content

Instantly share code, notes, and snippets.

@DimaDmiTreX
Created December 27, 2014 15:11
Show Gist options
  • Save DimaDmiTreX/5a041e5854bd782ff2c1 to your computer and use it in GitHub Desktop.
Save DimaDmiTreX/5a041e5854bd782ff2c1 to your computer and use it in GitHub Desktop.
using System.IO;
using System;
using System.Net.Sockets;
class Program
{
static void Main()
{
Connect("88.85.211.202","GET / HTTP/1.0\r\n\r\n");
}
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 80;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256 * 16];
// String to store the response ASCII representation.
String responseData = "";
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment