Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


FileShare Client / Server

Add Comment
 

 
<div align="center"> <table border="0" width="70%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a href="../../file/fileshare.zip" class="wbox">fileshare.zip</a> (37kb)</td> <td width="50%" class="outline">beta1</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> I was on the look out for some concrete example on TCP Sockets. I found a very good example of a POP and SMTP server written by my friend Pramod Singh. I have used the logic from his example to build this example. Thank You Pramod !! <br> This program is sort of a Peer to Peer application where there is transfer of files between two peers.<br> This example uses the &quot;System.Net.Sockets&quot; namespace to create a Client / Server. The server acts as a File Sharing Server, it listens for multiple clients to connect to it. The Clients have a option to either download any of the files available with the server for download. Also the Clients can upload files to the Server (if the Server allows it).<br> The example uses a Command based talking between them i.e. The Server sends a command to the client , the client decodes the command and acts according to the command. If the Client sends a command, the Server acts according to the command sent by the server. Note all commands I have used here are of 4 characters and only in Capital letters.</p> <p><span class=wboxheado>Requirements</span><br> 1) .NET SDK Beta 1 (Note: This example may not work on other versions of the SDK).<br> 2) This application will not work though firewalls.</p> <p ><span class=wboxheado>Procedure of client connecting to the server.</span><br> 1) Server starts to listen on the given port.<br> 2) Client connects to the Server on the given port.<br> 3) Server sends a &quot;<i>CONN Welcome to File Share Server</i>&quot; message to the client.<br> 4) The Client decodes the message and sends a &quot;<i>USER guest</i>&quot; message to the server where guest is the username of the client.<br> 5) Server reads the username and adds it to the User ListBox and sends<br> &quot;<i>LIST c:\FileShare\server\download\mydown.txt@c:\FileShare\server\download\myfile.exe</i>&quot;<br> i.e. a list of all the files available for download.<br> 6) The client updates its download file ListBox with the list of files sent by the server, and exits the thread.<br> <br> <span class=wboxheado>Procedure of client downloading a file form the server.</span><br> 1) Client Sends a &quot;<i>DOWN myfile.exe</i>&quot; command to the server.<br> 2) The Server has 2 options here<br> &nbsp;&nbsp; a) If file fame is valid an Server permits download then the server sends<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;<i>SIZE 35346</i>&quot; (the SIZE command with the size of the file requested for download).<br> &nbsp;&nbsp; b) If the file name is incorrect or the Server does not allow downloading the server sends<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;<i>NOPE FileNotFound</i>&quot; or &quot;<i>NOPE Download not Allowed!</i>&quot; .<br> 3) If the NOPE command is received then the client sends a &quot;<i>OHHH No Problem</i>&quot;<br> 4) If the SIZE command is received then the client sends a &quot;<i>SEND myfile.exe</i>&quot; to indicate the server to start sending the file.<br> 5) On receiving a &quot;<i>SEND</i>&quot; command the Server opens a stream to the file and sends the file over the socket to the client.<br> 6) After successful receipt of file the client sends a &quot;<i>RECD File!!</i>&quot; notification.</p> <p ><span class=wboxheado>Procedure of client uploading a file to the server</span><br> 1) Client sends a &quot;<i>UPFL myUpload.txt@65345</i>&quot; i.e. it sends the file name and the size of the file .<br> 2) If the Server does not allow upload it sends a &quot;<i>NOPE Server Upload Disabled</i>&quot; ;<br> 3) Otherwise a &quot;<i>SEND File</i>&quot; command is sent.<br> 4) On getting the &quot;<i>SEND</i>&quot; command the client opens a stream&nbsp; to the file and sends it over the socket to the Server.<br> 5) After successful receipt of file the server sends a &quot;<i>RECD File Received Properly</i>&quot; notification to the Client.<br> <br> <span class=wboxheado>Execution</span> <br> 1) Run the FileShareServer.exe first to start the server .(The server by default listens for connection on port 4455).<br> 2) Run the FileShareClient.exe which will connect to the server.</p> <p ><span class=wboxheado>Screen Shots</span></p> <p ><img border="0" src="../../img/fileshareserver.gif" width="500" height="459"><br> <b>Figure 1: </b>FileShare Server</p> <p ><img border="0" src="../../img/fileshareclient.gif" width="500" height="459"><br> <b>Figure 2:</b> FileShare Client</p>

Comments

Add Comment