static void Main(string[] args) { byte[] buffer = new byte[1024]; int receivedBytes; X509Certificate2 certificate = new X509Certificate2("./certs/server.pfx"); SecureSocket socket = SecureSocketFactory.CreateSecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, certificate); socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999)); socket.Listen(10); Console.WriteLine("Socket started"); Console.WriteLine("Waiting connections..."); while (true) { var scli = socket.Accept(); Console.WriteLine("Connection received."); while (scli.Connected) { Console.WriteLine("Reading data..."); do { receivedBytes = scli.Receive(buffer); byte[] temp = new byte[receivedBytes]; Array.Copy(buffer, 0, temp, 0, receivedBytes); Console.WriteLine("Data read: \"{0}\"", Encoding.UTF8.GetString(temp)); scli.Send(Encoding.UTF8.GetBytes(string.Format("ACK: \"{0}\"", Encoding.UTF8.GetString(temp)))); }while (receivedBytes < buffer.Length && scli.Available > 0); Thread.Sleep(50); } Console.WriteLine("Disconnected. Waiting new connection..."); } }
/// <summary> /// Starts listening for incoming server connections. /// </summary> /// <param name="ep">The EndPoint on which to listen.</param> /// <param name="sp">The protocol to use.</param> /// <param name="pfxfile">An optional PFX file.</param> /// <param name="password">An optional PFX password.</param> public void StartServer(IPEndPoint ep, SecureProtocol sp, Certificate cert) { // initialize a SecurityOptions instance SecurityOptions options = new SecurityOptions(sp, cert, ConnectionEnd.Server); // create a new SecureSocket with the above security options SecureSocket s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options); // from here on, act as if the SecureSocket is a normal Socket s.Bind(ep); s.Listen(10); Console.WriteLine("Listening on " + s.LocalEndPoint.ToString()); SecureSocket ss; string query = ""; byte[] buffer = new byte[1024]; int ret; while (true) { ss = (SecureSocket)s.Accept(); Console.WriteLine("Incoming socket accepted."); // receive HTTP query Console.WriteLine("Receiving HTTP request..."); ret = 0; query = ""; while (!IsComplete(query)) // wait until we've received the entire HTTP query { try { ret = ss.Receive(buffer, 0, buffer.Length, SocketFlags.None); } catch (Exception e) { Console.WriteLine("Error while receiving data from client [" + e.Message + "]."); Console.WriteLine(e); break; } if (ret == 0) { Console.WriteLine("Client closed connection too soon."); ss.Close(); break; } query += Encoding.ASCII.GetString(buffer, 0, ret); } if (IsComplete(query)) { // Send HTTP reply Console.WriteLine("Sending reply..."); ret = 0; try { while (ret != MentalisPage.Length) { ret += ss.Send(Encoding.ASCII.GetBytes(MentalisPage), ret, MentalisPage.Length - ret, SocketFlags.None); } ss.Shutdown(SocketShutdown.Both); ss.Close(); } catch (Exception e) { Console.WriteLine("Error while sending data to the client [" + e.Message + "]."); Console.WriteLine(e); } } Console.WriteLine("Waiting for another connection..."); } }