/// <summary> /// Event handler for when a client connects /// </summary> /// <param name="sender">sender of the event</param> /// <param name="e">ClientEventArgs containing the Client</param> static void server_OnClientConnect(object sender, ClientEventArgs e) { Client client = e.Client; //Get the client /* * Example of adding objects to a Tag for identification and extensible functionality. * What this does is add a string to identify the client to a List of objects (the list is not strictly necessary, but it allows room for expansion) * This can be expanded to include a class to periodically ping the client, or anything else */ client.Tag = new List<object> { "test" }; }
/// <summary> /// Event handler for when a client sends a message. Access the message with e.client.Message /// </summary> /// <param name="sender">sender of the event</param> /// <param name="e">ClientEventArgs containing the Client</param> static void server_OnMessageReceived(object sender, ClientEventArgs e) { Client client = e.Client; //Get the Client Server server = sender as Server; string msg = Encoding.ASCII.GetString(client.Message); //Get the message as a string string html = File.ReadAllText("index.html"); //Read the HTML file if (msg.Contains("GET")) //Is this a HTTP GET? { if (msg.Contains("favicon")) client.SendData("HTTP/1.1 404\r\n"); //There isn't a favicon for this site else //Send a HTTP response with the HTML data client.SendData( "HTTP/1.1 200 OK\r\nServer: test-b\r\nContent-Type: text/html\r\nAccept-Ranges: bytes\r\nContent-Length: " + html.Length + "\r\n\r\n" + html); if (server != null) server.CloseConnection(client); } else client.SendData("I heard you! " + Encoding.ASCII.GetString(client.Message) + "\n"); //If it is not a HTTP connection (e.g. telnet), handle it differently }
/// <summary> /// Event handler for there are too many clients. This event is fired when a client tries to connect. Immediately afterwards, the connection is terminated. /// </summary> /// <param name="sender">sender of the event</param> /// <param name="e">ClientEventArgs containing the Client</param> static void server_OnTooManyClients(object sender, ClientEventArgs e) { Client client = e.Client; client.SendData("Sorry, too many clients!\n"); }