-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSharpServer.cs
27 lines (26 loc) · 1.15 KB
/
CSharpServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Barebones_Web_Server
{
class Program
{
static void Main(string[] args)
{
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress iPAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(iPAddress, 8000);
listener.Bind(localEndPoint);
listener.Listen(10);
Socket connection = listener.Accept();
Console.WriteLine("Connected by: " + ((IPEndPoint)connection.RemoteEndPoint).Address.ToString());
byte[] data = new Byte[1024];
int bytesReceived = connection.Receive(data);
string response = "HTTP/1.1 200 OK\r\nServer: My CSharpic Server\r\nAccept-Ranges: bytes\r\nContent-Length: " + data.Length.ToString() + "\r\nContent-Type: text/html\r\n\r\n" + Encoding.UTF8.GetString(data, 0, bytesReceived);
connection.Send(Encoding.UTF8.GetBytes(response));
Console.WriteLine("Connection terminated");
connection.Close();
}
}
}