Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 17-19 extracted from chapter Input/Output and streams


Listing 17-18<     > Listing 17-20


This listing can be compiled with the command line:
csc.exe /out:Server.exe /target:exe Example_17_19_to_rename_Server.cs
Errors: 0 Warnings: 0


Example_17_19_to_rename_Server.cs
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Security.Principal;
using System.Security.Authentication;
using System.Threading;
using System.Security.Cryptography.X509Certificates;

class ProgServeur {
   static readonly ushort port = 50000;

   static void Main() {
      IPAddress ipAddress = new IPAddressnew byte[] { 12700} );
      TcpListener tcpL = new TcpListener( ipAddress, port );
      tcpL.Start();
      while (true) {
         try {
            Console.WriteLine( "Waiting for client..." );
            TcpClient tcpClient = tcpL.AcceptTcpClient();
            Console.WriteLine( "Client connected." );
            ProcessClientRequest(tcpClient.GetStream());
            Console.WriteLine( "Client disconnected." );
         catch Exception e ) {
            Console.WriteLine( e.Message );
         }
      }
   }
   private static X509Certificate getServerCert() {
      X509Store store = new X509StoreStoreName.My, 
                                       StoreLocation.CurrentUser);
      store.Open( OpenFlags.ReadOnly );
      X509CertificateCollection certs = store.Certificates.Find(
         X509FindType.FindBySubjectName, "CN=SslSvrCertif"true);
      return certs[0];
   }
   static void ProcessClientRequest( NetworkStream networkStream ) {
      SslStream streamSsl = new SslStream( networkStream );
      streamSsl.AuthenticateAsServer( getServerCert() );
      StreamWriter streamWriter = new StreamWriter( streamSsl );
      StreamReader streamReader = new StreamReader@"C:/Text/File.txt" );
      string sTmp = streamReader.ReadLine();
      try {
         while ( sTmp != null ) {
            Console.WriteLine( "Sending: {0}", sTmp );
            streamWriter.WriteLine( sTmp );
            streamWriter.Flush();
            sTmp = streamReader.ReadLine();
         }
      } finally {
         streamReader.Close();
         streamWriter.Close();
         streamSsl.Close();
         networkStream.Close();
      }
   }
}	
Copyright Patrick Smacchia 2006 2007