Esempio n. 1
0
 public static void Main()
 {
     using (Socket listeningSocket = new Socket(AddressFamily.InterNetwork,
                                                SocketType.Stream,
                                                ProtocolType.Tcp))
     {
         listeningSocket.Bind(new IPEndPoint(IPAddress.Any, port));
         Debug.Print("Listening for a client...");
         listeningSocket.Listen(1);
         using (Socket communicationSocket = listeningSocket.Accept())
         {
             Debug.Print("Connected to client.");
             using (SslStream sslStream = new SslStream(communicationSocket))
             {
                 X509Certificate serverCert =
                      new X509Certificate(Resources.GetBytes(Resources.BinaryResources.MyServer));
                 X509Certificate rootCA =
                      new X509Certificate(Resources.GetBytes(Resources.BinaryResources.MyRootCA));
                 sslStream.AuthenticateAsServer(serverCert, // To authenticate the server
                                                new X509Certificate[] { rootCA }, // CA certificates
                                                SslVerification.CertificateRequired, // Verify client
                                                SslProtocols.Default // Protocols that may be used
                                                );
                 //wait infinitely to get a response
                 sslStream.ReadTimeout = -1;
                 byte[] inBuffer = new byte[1000];
                 int count = sslStream.Read(inBuffer, 0, inBuffer.Length);
                 string message = new string(Encoding.UTF8.GetChars(inBuffer));
                 Debug.Print("Received '" + message + "'.");
             }
         }
     }
 }
        public MFTestResults RunServer()
        {
            MFTestResults testResult = MFTestResults.Fail;

            SslStream sslStream = null;
            try
            {
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

                sslServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                //sslServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false);
                sslServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 });

                
                sslServer.Bind(localEndPoint);

                sslServer.Listen(1);

                // Buffer for reading data
                Byte[] bytes = new Byte[2048];
                String data = "";

                // Start Listening for a connection.
                Log.Comment("Waiting for a connection... on IPAddress: " + localEndPoint.Address.ToString() + " Port: " +localEndPoint.Port.ToString());

                // Perform a blocking call to accept requests.
                // block in listening mode until the desktop app connects.  Then we know we can continue.
                Socket client = sslServer.Accept();

                client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 });
                //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30000);
                //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 30000);

                Log.Comment("Connected!");

                // Get a stream object for reading and writing
                sslStream = new SslStream(client);

                sslStream.AuthenticateAsServer(certificate, ca, clientCertificateRequired, enabledSslProtocols);

                int i = -1;
                do
                {
                    // Loop to receive all the data sent by the client.
                    i = sslStream.Read(bytes, 0, bytes.Length);

                    // Translate data bytes to a string.
                    // The encoding used is application specific.
                    string newStr = new string(System.Text.Encoding.UTF8.GetChars(bytes), 0, i);
                    Log.Comment("Received: " + newStr);

                    data += newStr;

                    if (data.IndexOf(SslClient.TERMINATOR) != -1)
                        break;
                } while (i != 0);

                // Send back a response.
                sslStream.Write(bytes, 0, i);
                Log.Comment("Sent:     " + data);

                Thread.Sleep(200);
                testResult = MFTestResults.Pass;
            }
            catch (SocketException e)
            {
                if (expectedException)
                    testResult = MFTestResults.Pass;

                Log.Comment("SocketException in StartServer(): " + e.ToString());
                Log.Comment("ErrorCode: " + e.ErrorCode.ToString());
            }
            catch (Exception e)
            {
                if (expectedException)
                    testResult = MFTestResults.Pass;

                Log.Comment("Exception in StartServer(): " + e.ToString());
            }
            finally
            {
                if (sslServer != null)
                {
                    sslServer.Close();
                }
                if (sslStream != null)
                {
                    sslStream.Dispose();
                }
            }

            return testResult;
        }
Esempio n. 3
0
        /// <summary>
        /// Read the messages sent over the socket.
        /// </summary>
         internal string ReadMessage(SslStream sslStream)
        {
            // Read the  message sent by the client.
            // The client signals the end of the message using the
            // "<EOF>" marker.
            byte[] buffer = new byte[2048];
            String messageData = "";
            int bytesRead = -1;
            do
            {
                // Read the client's test message.
                bytesRead = sslStream.Read(buffer, 0, buffer.Length);

                messageData += new String(Encoding.UTF8.GetChars(buffer));
                
                // Check for <EOF> or an empty message.
                if (messageData.IndexOf(TERMINATOR) != -1)
                {
                    break;
                }
            } while (bytesRead > 0);

            return messageData;
        }
Esempio n. 4
0
        public bool RunServer()
        {
            //MFTestResults testResult = MFTestResults.Fail;
            bool result = false;

            SslStream sslStream = null;
            Socket client = null;
            try
            {
                if (sslServer == null)
                {
                    IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

                    sslServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    sslServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false);
                    sslServer.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 });


                    sslServer.Bind(localEndPoint);

                    sslServer.Listen(1);
                }

                // Buffer for reading data
                Byte[] bytes = new Byte[2048];
                String data = "";

                // Start Listening for a connection.
                //Log.Comment("Waiting for a connection... on IPAddress: " + localEndPoint.Address.ToString() + " Port: " +localEndPoint.Port.ToString());

                // Perform a blocking call to accept requests.
                // block in listening mode until the desktop app connects.  Then we know we can continue.
                 client = sslServer.Accept();

                //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new byte[] { 0, 0, 0, 0 });
                //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
                //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000);

                //Log.Comment("Connected!");

                // Get a stream object for reading and writing
                 sslStream = new SslStream(client);          //SK call of SSL_ServerInit in CLR-Code

                sslStream.AuthenticateAsServer(certificate, ca, clientCertificateRequired, enabledSslProtocols);

                sslStream.ReadTimeout = 5000;
                sslStream.WriteTimeout = 5000;
                int i = -1;
                do
                {
                    // Loop to receive all the data sent by the client.
                    try
                    {
                        i = sslStream.Read(bytes, 0, bytes.Length);
                    }
                    catch (Exception)
                    {
                        
                        //throw;
                    }
                   

                    // Translate data bytes to a string.
                    // The encoding used is application specific.
                    string newStr = new string(System.Text.Encoding.UTF8.GetChars(bytes), 0, i);
                    //Log.Comment("Received: " + newStr);

                    data += newStr;

                    if (data.IndexOf(SslClient.TERMINATOR) != -1)
                        break;
                } while (i != 0);

                // Send back a response.
                //sslStream.Write(bytes, 0, i);
                // Return a static HTML document to the client.
                String s =
                    "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><title>.NET Micro Framework Web Server</title></head>" +
                   "<body>";
                sslStream.Write(Encoding.UTF8.GetBytes(s), 0, Encoding.UTF8.GetBytes(s).Length);

                for(int j = 0; j < 1000; j++)
                {
                    s = "<p><bold><a href=\"http://www.microsoft.com/netmf/\">Learn more about the .NET Micro Framework by clicking here</a></bold></p>";
                    sslStream.Write(Encoding.UTF8.GetBytes(s), 0, Encoding.UTF8.GetBytes(s).Length);
                }

                s = "</body></html>";
                sslStream.Write(Encoding.UTF8.GetBytes(s), 0, Encoding.UTF8.GetBytes(s).Length);
                //Log.Comment("Sent:     " + data);

                Thread.Sleep(200);
                result = true;
            }
            catch (SocketException e)
            {
                if (expectedException)
                    result = true;

                //Log.Comment("SocketException in StartServer(): " + e.ToString());
                //Log.Comment("ErrorCode: " + e.ErrorCode.ToString());
            }
            catch (Exception e)
            {
                if (expectedException)
                    result = true;

                //Log.Comment("Exception in StartServer(): " + e.ToString());
            }
            finally
            {
 
                if (sslStream != null)
                {
                    sslStream.Close();
                    sslStream.Dispose();
                    //sslStream.Flush();
                   
                }
                if (sslServer != null)
                {
                  // sslServer.Close();
                }

               
            }
     return result;
        }
Esempio n. 5
0
        public string ReadMessage(SslStream sslStream)
        {
            // Read the  message sent by the server.
            // The end of the message is signaled using the
            // "<EOF>" marker.
            byte[] buffer = new byte[2048];
            String messageData = "";
            int bytesRead = 0;
            do
            {
                if (sslStream.DataAvailable)
                {
                    int len = (int)sslStream.Length;

                    if (len > buffer.Length)
                    {
                        len = buffer.Length;
                    }

                    bytesRead += sslStream.Read(buffer, 0, len);

                    // Convert the bytes to a string
                    messageData += new String(Encoding.UTF8.GetChars(buffer));

                    // Check for EOF.
                    int index = messageData.IndexOf("<EOF>");
                    if (index != -1)
                    {
                        messageData = messageData.Substring(0, index + 5);
                        break;
                    }
                }
                Thread.Sleep(200);
            } while (sslStream.CanRead);

            return messageData.ToString();
        }
Esempio n. 6
0
        public string ReadMessage(SslStream sslStream)
        {
            // Read the  message sent by the server.
            // The end of the message is signaled using the
            // "<EOF>" marker.
            byte[] buffer = new byte[2048];
            String messageData = "";
            int bytes = -1;
            do
            {
                bytes = sslStream.Read(buffer, 0, buffer.Length);

                // Convert the bytes to a string
                messageData += new String(Encoding.UTF8.GetChars(buffer));

                // Check for EOF.
                if (messageData.ToString().IndexOf(TERMINATOR) != -1)
                {
                    break;
                }
            } while (bytes > 0);

            return messageData.ToString();
        }