Mam kilka pytań związanych z poniższym kodem. Zamieszczone są w komentarzach. Będę wdzięczny za wyjaśnienie tego.
private Socket httpServer;
private int serverPort = 80;
private Thread thread;
serverPort = int.Parse(PortTxtBox.Text.ToString());
if(serverPort > 65535 || serverPort <= 0)
{
throw new Exception("Server port not within the range");
}
thread = new Thread(new ThreadStart(this.connectionThreadMethod)); //po co tutaj wątek?
thread.Start();
StartServerBtn.Enabled = false;
StopServerBtn.Enabled = true;
private void connectionThreadMethod()
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, serverPort); //do czego służy endpoint?
httpServer.Bind(endPoint); //w jakim celu się binduje?
httpServer.Listen(1); //w jakim celu się nasłuchuje?
startListeningForConnection();
}
private void startListeningForConnection()
{
while(true)
{
DateTime time = DateTime.Now;
string data = "";
byte[] bytes = new byte[2048]; //w jakim celu tworzy się tablicę byteów i co one zawierają?
Socket client = httpServer.Accept(); //Blocking Statement
while(true)
{
int numBytes = client.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, numBytes);
if(data.IndexOf("\r\n") > -1)
{
break;
}
}
//data read
ServerLogsTxtBox.Invoke((MethodInvoker)delegate
{
//Runs inside UI Thread
ServerLogsTxtBox.Text += "\r\n\r\n";
ServerLogsTxtBox.Text += data;
ServerLogsTxtBox.Text += "\n\n ----- End of Request -----";
});
//Send back to response
string responseHeader = "HTTP/1.1 200 Everything is fine\nServer: my_csharp_server\n Content-Type: " +
"text/html; charset: UTF-8;";
string responseBody = "<!DOCTYPE html> <html>" +
"<Head><title></title>></head>" +
"<body><h4>" +"Server time is: " + time.ToString() + "</h4></body>" +
"</html>";
string responseString = responseHeader + responseBody;
byte[] responseData = Encoding.ASCII.GetBytes(responseString);
client.SendTo(responseData, client.RemoteEndPoint); //co to jest RemoteEndPoint?
client.Close();
}
```