Adobe Flex 2 - Answering
Tough Questions About
Enterprise Development
A Correct Person wrote:
Denis Roebrt commented on
the 21 Aug 2006
"Tough Que...
May. 16, 2008 06:28 PM
|
|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS .NET Security
How To Implement Secure TCP Communications in Microsoft .NET 2.0
Microsoft .NET Framework 2.0 promises to be the first major upgrade to Windows and Web development tools
Jul. 29, 2005 12:15 AM
Digg This!
Page 2 of 2
« previous page
Listing 1: A simple TCP client application based on Microsoft sample code. [C#] //This code is modified Microsoft .NET Framework SDK Code
using System.IO; using System.Net; using System.Text; using System.Net.Sockets;
{class Client {[STAThread] static void Main(string[] args) {string server = “localhost”; string message = “This is a test”; int port = 13000; NetworkStream stream = null; try {// Step 1. // Instantiate a TcpClient with the target server TcpClient client = new TcpClient(server, port);
// Convert the data to send into a byte array Byte[] data = System.Text.Encoding.ASCII.GetBytes
// Get the NetworkStream for the TcpClient for stream = client.GetStream(); // Step 4. // Send the message to the server. stream.Write(data, 0, data.Length);
Console.WriteLine(“Sent: {0}”, message);// Buffer to hold data returned from the server. data = new Byte[256];
//String responseData = String.Empty; // Step 5. // Read the response from the server up to the size int bytes = stream.Read(data, 0, data.Length); // Step 6. //Convert the received bytes into a string string responseData = System.Text.Encoding.ASCII. // Write out what was received to the console - Console.WriteLine(“Received: {0}”, responseData);} catch (SocketException ex) {Console.WriteLine(ex.Message); } catch (IOException ex) {Console.WriteLine(ex.Message); } finally {// Step 7. // Make sure that the NetworkStream is closed. if (stream != null) stream.Close(); } Console.WriteLine(“\n Press Enter to continue...”); Console.Read(); } } }
‘This code is modified Microsoft .NET Framework SDK Code
Imports System.IO Imports System.Net Imports System.Text Imports System.Net.Sockets Module Client
Dim server As String = “localhost” Dim message As String = “This is a test” Dim port As Integer = 13000 Dim stream As NetworkStream = Nothing Try
‘ Instantiate a TcpClient with the target server and Dim client As New TcpClient(server, port)
‘ Convert the data to send into a byte array Dim data As Byte() = System.Text.Encoding.ASCII. ‘ Step 3. ‘ Get the NetworkStream for the TcpClient for sending stream = client.GetStream() ‘ Step 4. ‘ Send the message to the server. stream.Write(Data, 0, Data.Length)
Console.WriteLine(“Sent: {0}”, message)‘ Buffer to hold data returned from the server. data = New [Byte](256) {}
‘String responseData = String.Empty
‘ Read the response from the server up to the size of Dim bytes As Integer = stream.Read(data, 0, data.Length)
‘ Convert the received bytes into a string Dim responseData As String = System.Text.Encoding.
Console.WriteLine(“Received: {0}”, responseData)Catch ex As SocketException
‘ Make sure that the NetworkStream is closed. If stream IsNot Nothing Then stream.Close() End If Console.WriteLine(“Press Enter to continue...”) Console.Read() End Try End Sub
Listing 2: A simple TCP listener application based on Microsoft example code. [C#] //This code is modified Microsoft .NET Framework SDK Code using System; using System.IO; using System.Net; using System.Text; using System.Net.Sockets;
{
{[STAThread] static void Main() {NetworkStream stream = null; try {int port = 13000; IPAddress localAddr = IPAddress.Loopback;
// Instantiate an instance of the TcpListener TcpListener server = new TcpListener
// Start listening for incoming connections. server.Start();
Byte[] bytes = new Byte[256]; String data = null;
// Loop while waiting for a connection. while (true) { Console.Write("Waiting for a TcpClient
// Accept connection request in a blocking TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!");
// Get a stream object for reading and writing stream = client.GetStream(); int i;
// Loop to receive all the data sent by the while ((i = stream.Read(bytes, 0, {// Convert the received bytes into a string. data = System.Text.Encoding.ASCII.GetString // Write the received data to the console. Console.WriteLine("Received: {0}", data);
data = data.ToUpper(System.Globalization.
byte[] msg = System.Text.Encoding.ASCII.
// Send back the data that was received, stream.Write(msg, 0, msg.Length); // Write the sent data to the console. Console.WriteLine("Sent: {0}", data);} } } catch (SocketException ex) {Console.WriteLine(ex.Message); } catch (IOException ex) {Console.WriteLine(ex.Message); } finally {// Step 8. // Make sure the NetworkStream is closed. if (stream != null) stream.Close(); }
Console.Read(); } } } [Visual Basic] 'This code is modified Microsoft .NET Framework SDK Code Imports System Imports System.IO Imports System.Net Imports System.Text Imports System.Net.Sockets
Dim stream As NetworkStream = Nothing Try
Dim localAddr As IPAddress = IPAddress.Loopback
' Instantiate an instance of the TcpListener Dim Server As New TcpListener(localAddr, port)
' Start listening for incoming connections. Server.Start()
Dim bytes(256) As Byte
' Loop while waiting for a connection. While (True)
' Accept connection request in a blocking manner Dim client As TcpClient = Server.AcceptTcpClient()
' Get a stream object for reading and writing Stream = client.GetStream() Dim i As Integer
' Loop to receive all the data sent by the client. i = stream.Read(bytes, 0, bytes.Length) While (i <> 0)
data = System.Text.Encoding.ASCII.GetString
Console.WriteLine("Received: {0}", data)
data = data.ToUpper(System.Globalization.
Dim msg() As Byte = System.Text.Encoding.
' Send back the data that was received, but stream.Write(msg, 0, msg.Length)
Console.WriteLine("Sent: {0}", data)
End While End While
' Make sure the NetworkStream is closed. If stream IsNot Nothing Then stream.Close() End If
Console.Read() End Try End Sub
Listing 3: A secure TCP client application that uses SslStream to encrypt the transmission and authenticate the server. [C#] //This code is modified Microsoft .NET Framework SDK Code
using System.IO; using System.Net; using System.Text; using System.Net.Sockets; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates;
{class SecureClient {[STAThread] static void Main() {string server = "localhost"; string message = "This is a test"; int port = 13000; SslStream stream = null; try {// Step 1. // Instantiate a TcpClient with the target server TcpClient client = new TcpClient(server, port);
// Convert the data to send into a byte array Byte[] data = System.Text.Encoding.ASCII.GetBytes
// Specify the callback function that will act as the RemoteCertificateValidationCallback callback = new RemoteCertificateValidationCallback(OnCertificateValidation); // Step 4. // Instantiate an SslStream with the NetworkStream stream = new SslStream(client.GetStream(), false,
// As a client, you can authenticate the server // This is the host name of the server you are stream.AuthenticateAsClient(server); if (stream.IsAuthenticated) {// Indicates whether the authentication was Console.WriteLine("IsAuthenticated: {0}", // Indicates whether both the client and server // In this example only the server is Console.WriteLine("IsMutuallyAuthenticated: // Indicates whether the SslStream uses data Console.WriteLine("IsEncrypted: {0}", stream.// Indicates whether the data sent is signed. Console.WriteLine("IsSigned: {0}", stream.// Indicates whether the current side of the Console.WriteLine("IsServer: {0}", stream.}
// Send the message to the server. stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);// Buffer to hold data returned from the server. data = new Byte[256];
// Read the response from the server up to the int bytes = stream.Read(data, 0, data.Length); // Step 8. // Convert the received bytes into a string string responseData = System.Text.Encoding.ASCII. // Write out what was received to the console - Console.WriteLine("Received: {0}", responseData);} catch (AuthenticationException ex) {Console.WriteLine(ex.Message); } catch (SocketException ex) {Console.WriteLine(ex.Message); } catch (IOException ex) {Console.WriteLine(ex.Message); } finally {// Step 9. // Make sure that the SslStream is closed. if (stream != null) stream.Close(); } Console.WriteLine("\n Press Enter to continue...");Console.Read(); } // Check the certificate for errors and to make sure it meets private static bool OnCertificateValidation(object sender, { Console.WriteLine("Server Certificate Issued To: {0}", Console.WriteLine("Server Certificate Issued By: {0}",
// The certificate can also be manually verified to if (errors != SslPolicyErrors.None) { Console.WriteLine("Server Certificate Validation Console.WriteLine(errors.ToString()); return false; } else { Console.WriteLine("No Certificate Validation Errors");return true; } }
}
'This code is modified Microsoft .NET Framework SDK Code
Imports System.IO Imports System.Net Imports System.Text Imports System.Net.Sockets Imports System.Net.Security Imports System.Security.Authentication Imports System.Security.Cryptography.X509Certificates Module SecureClient
Dim server As String = "localhost" Dim message As String = "This is a test" Dim port As Integer = 13000 Dim Stream As SslStream = Nothing
' Instantiate a TcpClient with the target server and Dim client As New TcpClient(server, port)
'Convert the data to send into a byte array Dim data As [Byte]() = System.Text.Encoding.ASCII.
' Specify the callback function that will act as the Dim callback As New RemoteCertificateValidationCallback
' Instantiate an SslStream with the NetworkStream Stream = New SslStream(client.GetStream(), False, callback)
' As a client, you can authenticate the server and ' This is the host name of the server you are connecting ' to connect to the server when TcpClient is instantiated. Stream.AuthenticateAsClient(server) If Stream.IsAuthenticated Then
Console.WriteLine("IsAuthenticated: {0}", Stream.' Indicates whether both the client and server has ' In this example only the server is authenticated. Console.WriteLine("IsMutuallyAuthenticated: {0}", ' Indicates whether the SslStream uses data encryption. Console.WriteLine("IsEncrypted: {0}", Stream.Is' Indicates whether the data sent is signed. Console.WriteLine("IsSigned: {0}", Stream.IsSigned)' Indicates whether the current side of the connection is Console.WriteLine("IsServer: {0}", Stream.IsServer)End If
' Send the message to the server. Stream.Write(data, 0, data.Length)
Console.WriteLine("Sent: {0}", message)' Buffer to hold data returned from the server. data = New [Byte](256) {}
' Read the response from the server up to the size of the buffer. Dim bytes As Integer = Stream.Read(data, 0, data.Length)
' Convert the received bytes into a string Dim responseData As String = System.Text.Encoding.ASCII.
Console.WriteLine("Received: {0}", responseData)
' Make sure that the SslStream is closed. If Stream IsNot Nothing Then Stream.Close() End If Console.WriteLine("Press Enter to continue...")Console.Read() End try End Sub 'Check the certificate for errors and to make sure it meets your Private Function OnCertificateValidation(ByVal sender As Object, Page 2 of 2 « previous page
MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
||||||||||||||||||||||||||||||||||||||