FileSharing Server in C# and .NET

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.

For Example this is the procedure of connection:

  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.
  4. The Client decodes the message and sends a "USER guest" message to the server.
  5. 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.

  6. The client updates its Download File ListBox with the List of files sent by the server, and exists the Thread.

Steps Occurring while Downloading File by the Client:

  1. Client Sends a "DOWN myfile.exe" command to the server.
  2. 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!".

  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 server sends a "RECD File!!" command.

    Steps Occurring while Uploading 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 a "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" command 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.

    Up Next
      Ebook Download
      View all
      Learn
      View all