|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Product Reviews A File Transfer Application
from Professional .NET Network Programming
A File Transfer Application
from Professional .NET Network Programming
By: Christian Nagel; Tim Parker; Srinivasa Sivakumar; Andrew Krowczyk; Ajit Mungale; Nauman Laghari; Vinod Kumar
Jan. 1, 2000 12:00 AM
(March 7, 2003) - Networking is one of the core tasks of enterprise-level programming, and for the programmer familiar with the C# language, Professional .NET Network Programming will provide the information to put network programming at the heart of their .NET applications. The following is an excerpt from this recent book from Wrox Press. A File Transfer Application We have seen how to use the UdpClient class to send and receive datagrams, and we created a chat application using the same principle. Next, we'll see how to transfer a file and serialized object using UdpClient. The sender and receiver programs are divided into two logical parts. In the first part, the sender sends file details (namely the file extension and file size) to the receiver(s) as a serialized object, and in the second part the actual file is sent to the destination. In the receiver, the first part accepts the serialized object with the associated information, and in the second part it creates the file on the destination machine. To make the application more interesting, we'll open the saved file using the associated program (for example, a .doc file might be opened with Microsoft Word, or an .htm file with Internet Explorer). File Server The file server is a simple console application implemented in a class named FileSender. This has a nested class called FileDetails that contains the information about the file - the file size and the file type. We start by importing the necessary namespaces, and declaring the fields for the class. The class has five private fields - an instance of our FileDetails class, a UdpClient object, plus information about the connection to the remote client, and a FileStream object for reading in the file we'll send to the client:
using System; Next we define the FileDetails class. Our FileDetails object will need to be serialized for sending over the network, so we add the [Serializable] attribute. As we've already intimated, the class just has two public fields, to store the type and the size of the file:
// File details (Req. for receiver)Now we come to the Main() method for the server. In this method we invite the user to input a remote IP address to send the file to, and then to enter the path and filename of the file to send. We open up this file with the FileStream object, and check its length. If this is greater than the maximum permitted size of 8,192 bytes, we close the UdpClient and the FileStream, and exit the application. Otherwise, we send the file information, wait two seconds by calling the Thread.Sleep() method, and then send the file itself:
[STAThread] The SendFileInfo() method populates the fields of the FileDetails object, and then serializes this object into a MemoryStream, using an XmlSerializer object. This is then read into a byte array, which is passed into the UdpClient's Send() method, to send the file information to the client:
public static void SendFileInfo() The SendFile() method just reads the file content from the FileStream into a byte array, and then sends this to the client:
private static void SendFile() File Receiver The file receiver is again a console application, and is implemented in a class named FileRecv. Again, we start by importing the necessary namespaces and declaring the fields for the class:
using System; We will need to deserialize the file information sent from the server into a FileDetails object, so we need to define that class within the client application, too:
[Serializable] The Main() method for the application just calls two methods, to get respectively the file details and the file itself:
[STAThread] The GetFileDetails() method calls the Receive() method of the UdpClient object. This receives the serialized FileDetails object from the server, which we save to a MemoryStream. We use an XmlSerializer object to deserialize this stream back into a FileDetails object, and display the retrieved information in the console:
private static void GetFileDetails() The ReceiveFile() method retrieves the file from the server and saves it to disk with the filename temp, plus the extension retrieved from the FileDetails object. We then call the Process.Start() static method to open the document with the associated program:
public static void ReceiveFile() Here's the output on the client and server when we run this program: See Figure 1.
This application has some limitations; firstly, the size of the file depends upon the size of the internal message buffer or network limit. The default buffer size is 8,192 bytes, so we cannot send files larger than 8,192 bytes. This could be overcome by dividing the file into multiple segments, each with a buffer size of 8,192 bytes. By calling the Read() method with the required buffer size, we can divide the file into multiple segments. As UDP does not use acknowledgement signals, we would have to implement a separate mechanism to check whether each segment was received correctly or not before sending the next segment. This can be achieved by creating another instance of UdpClient in both the sender and receiver, which will check for acknowledgement messages. Professional .NET Network Programming, by Christian Nagel, Srinivasa Sivakumar, Andrew Krowczyk, Tim Parker, Ajit Mungale, Nauman Laghari, and Vinod Kumar was published by Wrox Press in October 2002. ISBN: 1-861007-35-3. 496 pages, $49.99. For more information, see http://www.wrox.com/books/1861007353.htm ">http://www.wrox.com/books/1861007353.htm. Reader Feedback: Page 1 of 1
Latest Cloud Developer 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 Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||