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 "System.Net.Sockets" 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 "<i>CONN Welcome to File Share Server</i>" message to the client.<br> 4) The Client decodes the message and sends a "<i>USER guest</i>" 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> "<i>LIST c:\FileShare\server\download\mydown.txt@c:\FileShare\server\download\myfile.exe</i>"<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 "<i>DOWN myfile.exe</i>" command to the server.<br> 2) The Server has 2 options here<br> a) If file fame is valid an Server permits download then the server sends<br> "<i>SIZE 35346</i>" (the SIZE command with the size of the file requested for download).<br> b) If the file name is incorrect or the Server does not allow downloading the server sends<br> "<i>NOPE FileNotFound</i>" or "<i>NOPE Download not Allowed!</i>" .<br> 3) If the NOPE command is received then the client sends a "<i>OHHH No Problem</i>"<br> 4) If the SIZE command is received then the client sends a "<i>SEND myfile.exe</i>" to indicate the server to start sending the file.<br> 5) On receiving a "<i>SEND</i>" 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 "<i>RECD File!!</i>" notification.</p> <p ><span class=wboxheado>Procedure of client uploading a file to the server</span><br> 1) Client sends a "<i>UPFL myUpload.txt@65345</i>" 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 "<i>NOPE Server Upload Disabled</i>" ;<br> 3) Otherwise a "<i>SEND File</i>" command is sent.<br> 4) On getting the "<i>SEND</i>" command the client opens a stream to the file and sends it over the socket to the Server.<br> 5) After successful receipt of file the server sends a "<i>RECD File Received Properly</i>" 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>