Introduction
I was on a look out for some concrete example on 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 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 command I have used here have 4 characters and in Capital letters.
Prodecure of Connection with Server for File Sharing
- Server starts to listen on the given port.
- Client connects to the Server on the given port.
- Server sends a "CONN Welcome to File Share Server" message.
- The Client decodes the message and sends a "USER guest" message to the server.
- Server reads the User Name 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 al the files it has for download.
- The client updates its Download File ListBox with the List of files sent by the server, and exists the Thread.
Procedure of Downloading File by the Client
- Client Sends a "DOWN myfile.exe" command to the server.
- The Server has 2 options here
- If File Name is Valid an Server permits Download then the server sends "SIZE 35346" (the SIZE command with the size of the File Requested for download.
- If the File Name is incorrect or the Server does not allow downloading the server sends "NOPE FileNotFound" or "NOPE Download not Allowed!".
- If the NOPE command is Received then the client sends a "OHHH No Problem".
- If the SIZE command is received then the client sends a "SEND myfile.exe" to indicate the server to start sending the file.
- On Receiving a "SEND" command the Server opens a stream to the File and sends the File over the socket to the client.
- After successful receipt of file the server sends a "RECD File!!" command.
Procedure of Uploading File to the Server
- Client sends a "UPFL myUpload.txt@65345" i.e. it sends the File Name and the Size of the File .
- If the Server does not allow Upload it sends a "NOPE Server upload Disabled".
- Otherwise a "SEND File" command is sent.
- On getting a "SEND" command the client opens a stream to the file and sends it over the Socket to the Server.
- After successful receipt of file the server sends a "RECD File Received Properly" command to the Client.
Execution of the File Sharing Service
- Run the FileShareServer.exe first to start the server (The server by default listens for connection on port 4455).
- Run the FileShareClient.exe which will connect to the server.