FileShare Client / Server
Add Comment| Download File | SDK |
| fileshare.zip (37kb) | beta1 |
Introduction
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 !!
This program is sort of a Peer to Peer
application where there is transfer of files between two peers.
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).
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.
Requirements
1) .NET SDK Beta 1 (Note: This example may not work on other
versions of the SDK).
2) This application will not work though firewalls.
Procedure of client
connecting to the server.
1) Server starts to listen on the given port.
2) Client connects to the Server on the given port.
3) Server sends a "CONN Welcome to File Share Server"
message to the client.
4) The Client decodes the message and sends a "USER guest"
message to the server where guest is the username of the client.
5) Server reads the username and adds it to the User ListBox and sends
"LIST c:\FileShare\server\download\mydown.txt@c:\FileShare\server\download\myfile.exe"
i.e. a list of all the files available for download.
6) The client updates its download file ListBox with the list of
files sent by the server, and exits the thread.
Procedure of client downloading a file form
the server.
1) Client Sends a "DOWN myfile.exe" command to the server.
2) The Server has 2 options here
a) If file fame is valid an Server permits download
then the server sends
"SIZE 35346" (the
SIZE command with the size of the file requested for download).
b) If the file name is incorrect or the Server does not
allow downloading the server sends
"NOPE FileNotFound" or
"NOPE Download not Allowed!" .
3) If the NOPE command is received then the client sends a "OHHH
No Problem"
4) If the SIZE command is received then the client sends a
"SEND myfile.exe" to indicate the server to start sending
the file.
5) On receiving a "SEND" command the Server opens a stream
to the file and sends the file over the socket to the client.
6) After successful receipt of file the client sends a "RECD
File!!" notification.
Procedure of client
uploading a file to the server
1) Client sends a "UPFL myUpload.txt@65345" i.e. it sends the
file name and the size of the file .
2) If the Server does not allow upload it sends a "NOPE Server
Upload Disabled" ;
3) Otherwise a "SEND File" command is sent.
4) On getting the "SEND" command the client opens a stream
to the file and sends it over the socket to the Server.
5) After successful receipt of file the server sends a "RECD
File Received Properly" notification to the Client.
Execution
1) Run the FileShareServer.exe first to start the server .(The
server by default listens for connection on port 4455).
2) Run the FileShareClient.exe which will connect to the server.
Screen Shots

Figure 1: FileShare Server

Figure 2: FileShare Client

