Problem summary: I am currently playing with writing custom applications for http servers. I found out that when any web browser connected to my server application will have 0.5-1 seconds of “latency” (according to Google Chrome) before the request is processed [which will take milliseconds]
In the end, I tried to create a dummy program to identify the problem:
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SlowHTTPServer
{
class FailServer
{
static void Main()
{
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenerSocket.Bind(new IPEndPoint(IPAddress.Any, 80));
listenerSocket.Listen(64);
while (true)
{
Socket clientConn = listenerSocket.Accept();
System.DateTime startTime = System.DateTime.Now;
byte[] buffer = new byte[1024];
clientConn.Receive(buffer);
string reqHeader = Encoding.ASCII.GetString(buffer);
if (reqHeader.IndexOf("script1") != -1)
clientConn.Send(Encoding.ASCII.GetBytes("document.title='hai dere';"));
else if (reqHeader.IndexOf("script2") != -1)
clientConn.Send(Encoding.ASCII.GetBytes("window.onload=function(){document.body.style.backgroundColor='#FF99FF';};"));
else if (reqHeader.IndexOf("iframe") != -1)
clientConn.Send(Encoding.ASCII.GetBytes("blargh zargh nargh dargh pikachu tangerine zombie destroy annihilate"));
else
clientConn.Send(Encoding.ASCII.GetBytes("<html><head><script src='script1.js'></script><script src='script2.js'></script></head><body>mainPage<iframe src='iframe.html'>u no haz iframe</iframe></body></html>"));
clientConn.Close();
System.Console.WriteLine((System.DateTime.Now - startTime).TotalMilliseconds);
}
}
}
}
... , + script + iframe - 2- [ localhost localhost, Windows + ], , Apache, [ , ] 100 .
, , , , http-. , .
, 30 + JavaScript script, 20 [ , < 10 kb]
script, , , , 1-10 , , http -, [ , ?]
.
.
:
- "" , , , , .
- 80, , http://127.0.0.1/ http://localhost/
:
Chrome,
:
, / ... , 1 .
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SlowSocketAccept
{
class Program
{
static DateTime connectionBeginTime;
static bool listening = false;
static void Main(string[] args)
{
new Thread(ClientThread).Start();
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenerSocket.Bind(new IPEndPoint(IPAddress.Any, 80));
listenerSocket.Listen(80);
listening = true;
Socket newConn = listenerSocket.Accept();
byte[] reqHeader = new byte[1024];
newConn.Receive(reqHeader);
newConn.Send(Encoding.ASCII.GetBytes("Response Header\r\n\r\nContent"));
newConn.Close();
Console.WriteLine("Elapsed time: {0} ms", (DateTime.Now - connectionBeginTime).TotalMilliseconds);
}
static void ClientThread()
{
while (listening == false) ;
System.Threading.Thread.Sleep(10);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectionBeginTime = DateTime.Now;
s.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80));
s.Send(Encoding.ASCII.GetBytes("Request Header"));
byte[] response = new byte[1024];
s.Receive(response);
}
}
}
[a b - ]
a) 'b'
b) , A , ,
a) , 80, , B,
b) , 127.0.0.1:80 [localhost: 80]
a)
b) [ ]
a) , [ + ]
a)
1 =/