Programming C# 2nd Edition phần 10 pptx

55 313 0
Programming C# 2nd Edition phần 10 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Programming C#, 2nd Edition 526 if (bytesRead > 0) { // write it out to the client networkStream.BeginWrite( buffer, 0, bytesRead, callbackWrite, null); } If OnFileCompletedRead was called and no bytes were read, this signifies that the entire file has been sent. The server reacts by closing the NetworkStream and socket, thus letting the client know that the transaction is complete: networkStream.Close( ); socket.Close( ); networkStream = null; socket = null; When the network write completes, the OnWriteComplete( ) method is called, and this kicks off another read from the file: private void OnWriteComplete( IAsyncResult ar ) { networkStream.EndWrite(ar); Console.WriteLine( "Write complete"); inputStream.BeginRead( buffer, // holds the results 0, // offset buffer.Length, // (BufferSize) myFileCallBack, // call back delegate null); // local state object } The cycle begins again with another read of the file, and the cycle continues until the file has been completely read and transmitted to the client. The client code simply writes a filename to the network stream to kick off the file read: string message = @"C:\test\source\AskTim.txt"; System.IO.StreamWriter writer = new System.IO.StreamWriter(streamToServer); writer.Write(message); writer.Flush( ); The client then begins a loop, reading from the network stream until no bytes are sent by the server. When the server is done, the network stream is closed. Start by initializing a Boolean value to false and creating a buffer to hold the bytes sent by the server: bool fQuit = false; while (!fQuit) { char[] buffer = new char[BufferSize]; You are now ready to create a new StreamReader from the NetworkStream member variable streamToServer: System.IO.StreamReader reader = new System.IO.StreamReader(streamToServer); Programming C#, 2nd Edition 527 The call to Read( ) takes three parameters: the buffer, the offset at which to begin reading, and the size of the buffer: int bytesRead = reader.Read(buffer,0, BufferSize); Check to see if the Read( ) returned any bytes; if not you are done and you can set the Boolean value fQuit to true, causing the loop to terminate: if (bytesRead == 0) fQuit = true; If you did receive bytes, you can write them to the console, or write them to a file, or do whatever it is you will do with the values sent from the server: else { string theString = new String(buffer); Console.WriteLine(theString); } } Once you break out of the loop, close the NetworkStream. streamToServer.Close( ); The complete annotated source for the server is shown in Example 21-12, with the client following in Example 21-13. Example 21-12. Implementing an asynchronous network file server using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file name from the client // open the file and send the // contents from the server to the client public class AsynchNetworkFileServer { class ClientHandler { // constructor public ClientHandler( Socket socketForClient ) { // initialize member variable socket = socketForClient; // initialize buffer to hold // contents of file buffer = new byte[256]; // create the network stream networkStream = new NetworkStream(socketForClient); Programming C#, 2nd Edition 528 // set the file callback for reading // the file myFileCallBack = new AsyncCallback(this.OnFileCompletedRead); // set the callback for reading from the // network stream callbackRead = new AsyncCallback(this.OnReadComplete); // set the callback for writing to the // network stream callbackWrite = new AsyncCallback(this.OnWriteComplete); } // begin reading the string from the client public void StartRead( ) { // read from the network // get a filename networkStream.BeginRead( buffer, 0, buffer.Length, callbackRead, null); } // when called back by the read, display the string // and echo it back to the client private void OnReadComplete( IAsyncResult ar ) { int bytesRead = networkStream.EndRead(ar); // if you got a string if( bytesRead > 0 ) { // turn the string to a file name string fileName = System.Text.Encoding.ASCII.GetString( buffer, 0, bytesRead); // update the console Console.Write( "Opening file {0}", fileName); // open the file input stream inputStream = File.OpenRead(fileName); // begin reading the file inputStream.BeginRead( buffer, // holds the results 0, // offset buffer.Length, // BufferSize myFileCallBack, // call back delegate null); // local state object } Programming C#, 2nd Edition 529 else { Console.WriteLine( "Read connection dropped"); networkStream.Close( ); socket.Close( ); networkStream = null; socket = null; } } // when you have a buffer-full of the file void OnFileCompletedRead(IAsyncResult asyncResult) { int bytesRead = inputStream.EndRead(asyncResult); // if you read some file if (bytesRead > 0) { // write it out to the client networkStream.BeginWrite( buffer, 0, bytesRead, callbackWrite, null); } else { Console.WriteLine("Finished."); networkStream.Close( ); socket.Close( ); networkStream = null; socket = null; } } // after writing the string, get more of the file private void OnWriteComplete( IAsyncResult ar ) { networkStream.EndWrite(ar); Console.WriteLine( "Write complete"); // begin reading more of the file inputStream.BeginRead( buffer, // holds the results 0, // offset buffer.Length, // (BufferSize) myFileCallBack, // call back delegate null); // local state object } private const int BufferSize = 256; private byte[] buffer; private Socket socket; private NetworkStream networkStream; private Stream inputStream; private AsyncCallback callbackRead; private AsyncCallback callbackWrite; private AsyncCallback myFileCallBack; } Programming C#, 2nd Edition 530 public static void Main( ) { AsynchNetworkFileServer app = new AsynchNetworkFileServer( ); app.Run( ); } private void Run( ) { // create a new TcpListener and start it up // listening on port 65000 TcpListener tcpListener = new TcpListener(65000); tcpListener.Start( ); // keep listening until you send the file for (;;) { // if a client connects, accept the connection // and return a new socket named socketForClient // while tcpListener keeps listening Socket socketForClient = tcpListener.AcceptSocket( ); if (socketForClient.Connected) { Console.WriteLine("Client connected"); ClientHandler handler = new ClientHandler(socketForClient); handler.StartRead( ); } } } } Example 21-13. Implementing a client for an asynchronous network file server using System; using System.Net.Sockets; using System.Threading; using System.Text; public class AsynchNetworkClient { static public int Main( ) { AsynchNetworkClient client = new AsynchNetworkClient( ); return client.Run( ); } AsynchNetworkClient( ) { string serverName = "localhost"; Console.WriteLine("Connecting to {0}", serverName); TcpClient tcpSocket = new TcpClient(serverName, 65000); streamToServer = tcpSocket.GetStream( ); } Programming C#, 2nd Edition 531 private int Run( ) { string message = @"C:\test\source\AskTim.txt"; Console.Write( "Sending {0} to server.", message); // create a streamWriter and use it to // write a string to the server System.IO.StreamWriter writer = new System.IO.StreamWriter(streamToServer); writer.Write(message); writer.Flush( ); bool fQuit = false; // while there is data coming // from the server, keep reading while (!fQuit) { // buffer to hold the response char[] buffer = new char[BufferSize]; // Read response System.IO.StreamReader reader = new System.IO.StreamReader(streamToServer); // see how many bytes are // retrieved to the buffer int bytesRead = reader.Read(buffer,0,BufferSize); if (bytesRead == 0) // none? quite fQuit = true; else // got some? { // display it as a string string theString = new String(buffer); Console.WriteLine(theString); } } streamToServer.Close( ); // tidy up return 0; } private const int BufferSize = 256; private NetworkStream streamToServer; } By combining the asynchronous file read with the asynchronous network read, you have created a scalable application that can handle requests from a number of clients. 21.5 Web Streams Rather than reading from a stream provided by a custom server, you can just as easily read from any web page on the Internet. A WebRequest is an object that requests a Uniform Resource Identifier (URI) such as the URL for a web page. You can use a WebRequest object to create a WebResponse object that [...]... fileStream.Close( ); } } } private int startNumber = 1; private int endNumber; private int[] theSums; Output: Creating first one with new 1, 3, 6, 10, 15, 21, 28, 538 Programming C#, 2nd Edition 36, 45, 55, Serializing completed Creating second one with deserialize 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, The output shows that the object was created, displayed, and then serialized The object was then deserialized... 1, 3, 6, 10, 15, Serializing completed Creating second one with deserialize 1, 3, 6, 10, 15, You can see in the output that the data was successfully serialized to disk and then reconstituted by deserialization The trade-off of disk storage space versus time does not make a lot of sense with five values, but it makes a great deal of sense with five million values 541 Programming C#, 2nd Edition So... do { theEntry = reader.ReadLine( ); Console.WriteLine(theEntry); } while (theEntry != null); } reader.Close( ); configFile.Close( ); Output: Last access: 5/2/2001 10: 00:57 AM Last position = 27,35 544 Programming C#, 2nd Edition Chapter 22 Programming NET and COM Programmers love a clean slate Although it would be nice if we could throw away all the code we've ever written and start over, this typically... 22-9 Browsing for the imported control Once imported, the control appears on the toolbox menu, as shown in Figure 22 -10 Note that the control may appear at the bottom of the toolbox Figure 22 -10 Viewing the AxCalcControl calculator after importing it into the toolbox 551 Programming C#, 2nd Edition Now you can drag this control onto your Windows Form and make use of its functions, just as you did in the... Registry For example, you can create a new C# DLL project in which you re-create your four-function calculator, as shown in Example 22-8 Example 22-8 The four-function calculator in a DLL using System; using System.Reflection; [assembly: AssemblyKeyFile("test.key")] namespace Programming_ CSharp { public class Calculator { public Calculator( ) { } 560 Programming C#, 2nd Edition } } public Double Add (Double... Console.WriteLine("Creating first one with new "); SumOf app = new SumOf(1 ,10) ; } Console.WriteLine("Creating second one with deserialize "); SumOf newInstance = SumOf.DeSerialize( ); newInstance.DisplaySums( ); public SumOf(int start, int end) { startNumber = start; endNumber = end; ComputeSums( ); DisplaySums( ); Serialize( ); } 537 Programming C#, 2nd Edition private void ComputeSums( ) { int count = endNumber... Visual C# Windows Form project in Visual Studio NET (see Chapter 13), name the project InteropTest, and design a form (such as the TestForm form you created in VB in the preceding section) by dragging and dropping controls onto it Name the form TestForm A complete sample form is shown in Figure 22-6 Figure 22-6 Building a Windows Form to test the CalcControl ActiveX control 549 Programming C#, 2nd Edition. .. StreamWriter on that file: StreamWriter writer = new StreamWriter(configFile); Then write to that stream as you would to any other Example 21-17 illustrates 542 Programming C#, 2nd Edition Example 21-17 Writing to isolated storage namespace { using using using Programming_ CSharp System; System.IO; System.IO.IsolatedStorage; public class Tester { public static void Main( ) { Tester app = new Tester( ); app.Run(... Url.wj4zpd5ni41dynqxx1uz0ix0aoaraftc\files You can read this file with Notepad if what you've written is just text: Last access: 5/2/2001 10: 00:57 AM Last position = 27,35 Or, you can access this data programmatically To do so, reopen the file: 543 Programming C#, 2nd Edition IsolatedStorageFileStream configFile = new IsolatedStorageFileStream ("Tester.cfg",FileMode.Open); Create a StreamReader object:... startNumber; for (int i=1,j=startNumber + 1;i . Programming C#, 2nd Edition 526 if (bytesRead > 0) { // write it out to the client networkStream.BeginWrite(. streamToServer: System.IO.StreamReader reader = new System.IO.StreamReader(streamToServer); Programming C#, 2nd Edition 527 The call to Read( ) takes three parameters: the buffer, the offset at which. // create the network stream networkStream = new NetworkStream(socketForClient); Programming C#, 2nd Edition 528 // set the file callback for reading // the file myFileCallBack =

Ngày đăng: 12/08/2014, 23:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan