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-7 extracted from chapter Input/Output and streams
Listing 17-6< > Listing 17-8
This listing can be compiled with the command line: csc.exe /out:Server.exe /target:exe Example_17_7_to_rename_Server.cs Errors: 0 Warnings: 0
Example_17_7_to_rename_Server.cs
using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Text;
class ProgServeur {
static readonly ushort port = 50000;
static void Main() {
IPAddress ipAddress = new IPAddress( new byte[] { 127, 0, 0, 1 } );
TcpListener tcpListener = new TcpListener( ipAddress, port );
tcpListener.Start();
while(true) {
try {
Console.WriteLine( "Main:Waiting for a client..." );
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine( "Main:Client connected." );
ClientRequestProcessing clientRequestProcessing =
new ClientRequestProcessing( tcpClient.GetStream() );
clientRequestProcessing.Go();
}
catch( Exception e ) {
Console.WriteLine( e.Message );
}
}
}
}
// An instance of this class is created for each client.
class ClientRequestProcessing {
static readonly int bufferSize = 512;
private byte [] m_Buffer;
private NetworkStream m_NetworkStream;
private AsyncCallback m_CallbackRead;
private AsyncCallback m_CallbackWrite;
// The constructor initializes:
// - m_NetworkStream: stream to communicate with the client.
// - m_CallbackRead: callback procedure for read.
// - m_CallbackWrite: callback procedure for write.
// - m_Buffer: used both for reading and writing data.
public ClientRequestProcessing( NetworkStream networkStream ) {
m_NetworkStream = networkStream;
m_CallbackRead = new AsyncCallback( this.OnReadDone );
m_CallbackWrite = new AsyncCallback( this.OnWriteDone );
m_Buffer = new byte[ bufferSize ];
}
public void Go() {
m_NetworkStream.BeginRead(
m_Buffer, 0 , m_Buffer.Length , m_CallbackRead , null );
}
// This callback procedure is called when an asynchronous read
// triggered by a call to 'BeginRead()' terminates.
private void OnReadDone( IAsyncResult asyncResult ) {
int nBytes = m_NetworkStream.EndRead(asyncResult );
// Send back the received string to the client.
if( nBytes > 0 ){
string s = Encoding.ASCII.GetString( m_Buffer , 0 , nBytes );
Console.Write(
"Async:{0} bytes received from client: {1}" , nBytes, s );
m_NetworkStream.BeginWrite(
m_Buffer, 0 , nBytes , m_CallbackWrite , null );
}
// If the client didn't send anything, the we discard him.
else{
Console.WriteLine( "Async:Client request processed." );
m_NetworkStream.Close();
m_NetworkStream = null;
}
}
// This callback procedure is called when an asynchronous write
// triggered by a call to 'BeginWrite()' terminates.
private void OnWriteDone( IAsyncResult asyncResult ) {
m_NetworkStream.EndWrite( asyncResult );
Console.WriteLine( "Async:Write done." );
m_NetworkStream.BeginRead(
m_Buffer, 0 , m_Buffer.Length , m_CallbackRead , null );
}
}
Copyright Patrick Smacchia 2006 2007
|