CodeXchange Wednesday, October 17, 2018
Home
What's it?
Download
Register
My Snippets
Add Snippet
Search
Faq
Contact Us
Sponsors

Website hosted by


Code Snippet Preview

Review the code snippet before inserting it on your project.

Snippet Metadata
Summary: FileShareClient.cs
Language: C#
Author: Robert Wagner
Author snippets RSS:
Culture: en-US
Bytes: 31784
Visual Studio 2005 Snippet:

Snippet Stats
Downloads: 4
Overall rating : 4
Average rating : Snippet rating

Snippet Preview
Code:
namespace App_NameFileShare {
    
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.WinForms;
    using System.Net.Sockets;
    using System.Net ;
    using System.Threading ;
    using System.IO ;
    /// 
    ///    This is the client Form 
    /// 
    public class FileShareClient : System.WinForms.Form {
    ///  
    ///    Required by the Win Forms designer 
    /// 
   private System.ComponentModel.Container components;
    private System.WinForms.StatusBar statusBar1;
    private System.WinForms.RichTextBox clientlogt;
    private System.WinForms.Label logl;
    private System.WinForms.ListBox downlistBox;
    private System.WinForms.Label downl;
    private System.WinForms.ListBox uploadlistBox;
    private System.WinForms.Label uploadl;
    private System.WinForms.ToolBarButton toolBarButton4;
    private System.WinForms.ToolBarButton downloadb;
    private System.WinForms.ToolBarButton toolBarButton3;
    private System.WinForms.ToolBarButton uploadb;
    private System.WinForms.ToolBarButton toolBarButton2;
    private System.WinForms.ToolBarButton dicsob;
    private System.WinForms.ToolBarButton toolBarButton1;
    private System.WinForms.ToolBarButton connectb;
    private System.WinForms.ToolBar toolBar1;
    private System.WinForms.MainMenu mainMenu1;
    private int port=4455 ;
    private Socket clientsocket=null ;
   private string user ;
   private string address ;
   private string updir ;
   private string dldir ;
   private clientdialog ctdl ;
   private Thread clientthread ;
   private bool connected=false ;
   private bool locked=false ;
    private string readwrite ;  
    private File[] upfiles ;
    /// 
    ///     This is the Constructor
    /// 
   public FileShareClient() 
   {
            // Required for Win Form Designer support
            InitializeComponent();
    }
    /// 
    ///    Clean up any resources being used
    /// 
    public override void Dispose() {
      if(clientsocket!=null)
        {
         SendMessage(clientsocket,"QUIT "+user) ;
         clientlogt.Text+="Disconnected !!" ;
         clientsocket=null;
        }
        base.Dispose();
        components.Dispose();
   }
        /// 
        ///    The main entry point for the application.
        /// 
        public static void Main(string[] args) {
            Application.Run(new FileShareClient());
        }

        /// 
        ///    Required method for Designer support 
        /// 
    private void InitializeComponent()
   {
      this.components = new System.ComponentModel.Container();
      this.logl = new System.WinForms.Label();
      this.uploadlistBox = new System.WinForms.ListBox();
      this.downloadb = new System.WinForms.ToolBarButton();
      this.downl = new System.WinForms.Label();
      this.uploadb = new System.WinForms.ToolBarButton();
      this.statusBar1 = new System.WinForms.StatusBar();
      this.downlistBox = new System.WinForms.ListBox();
      this.uploadl = new System.WinForms.Label();
      this.clientlogt = new System.WinForms.RichTextBox();
      this.dicsob = new System.WinForms.ToolBarButton();
      this.connectb = new System.WinForms.ToolBarButton();
      this.toolBar1 = new System.WinForms.ToolBar();
      this.toolBarButton4 = new System.WinForms.ToolBarButton();
      this.toolBarButton3 = new System.WinForms.ToolBarButton();
      this.toolBarButton2 = new System.WinForms.ToolBarButton();
      this.mainMenu1 = new System.WinForms.MainMenu();
      this.toolBarButton1 = new System.WinForms.ToolBarButton();
      
      
      MenuItem FileMenu = new MenuItem("File");
       mainMenu1.MenuItems.Add(FileMenu);
      FileMenu.MenuItems.Add(new MenuItem("Connect", new EventHandler(clientconnect)));
         FileMenu.MenuItems.Add(new MenuItem("-"));
        FileMenu.MenuItems.Add(new MenuItem("Disconnect", new EventHandler(clientdisconnect)));
        FileMenu.MenuItems.Add(new MenuItem("-"));
        FileMenu.MenuItems.Add(new MenuItem("Exit", new EventHandler(clientexit)));
        MenuItem TransferMenu = new MenuItem("File Transfer");
        mainMenu1.MenuItems.Add(TransferMenu);
      TransferMenu.MenuItems.Add(new MenuItem("Donwload File", new EventHandler(downfile))) ;
      TransferMenu.MenuItems.Add(new MenuItem("-"));
      TransferMenu.MenuItems.Add(new MenuItem("Upload File", new EventHandler(uploadfile))) ;
      MenuItem ContactMenu = new MenuItem("Contact Me");
        mainMenu1.MenuItems.Add(ContactMenu);
      ContactMenu.MenuItems.Add(new MenuItem("Contact", new EventHandler(contactme))) ;
         
      //@design this.TrayHeight = 90;
      //@design this.TrayLargeIcon = false;
      //@design this.TrayAutoArrange = true;
      logl.Location = new System.Drawing.Point(224, 40);
      logl.Text = "Client Connection Log";
      logl.Size = new System.Drawing.Size(151, 16);
      logl.AutoSize = true;
      logl.Font = new System.Drawing.Font("Arial", 10f, System.Drawing.FontStyle.Bold);
      logl.TabIndex = 0;
      logl.BackColor = System.Drawing.Color.DarkOrange;
      
      uploadlistBox.Location = new System.Drawing.Point(8, 64);
      uploadlistBox.Size = new System.Drawing.Size(200, 139);
      uploadlistBox.HorizontalScrollbar = true;
      uploadlistBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Bold);
      uploadlistBox.TabIndex = 5;
      
      downloadb.Text = "Download File";
      downloadb.ToolTipText = "Click to download selected file from server";
            
      downl.Location = new System.Drawing.Point(8, 216);
      downl.Text = "Files to Download";
      downl.Size = new System.Drawing.Size(122, 16);
      downl.AutoSize = true;
      downl.Font = new System.Drawing.Font("Arial", 10f, System.Drawing.FontStyle.Bold);
      downl.TabIndex = 3;
      downl.BackColor = System.Drawing.Color.DarkOrange;
      
      uploadb.Text = "Upload File";
      uploadb.ToolTipText = "Click here to upload the selected file to server";
         
      statusBar1.BackColor = System.Drawing.SystemColors.Control;
      statusBar1.Location = new System.Drawing.Point(0, 393);
      statusBar1.Size = new System.Drawing.Size(492, 20);
      statusBar1.TabIndex = 0;
      statusBar1.Text = "Ready to Connect";
      
      downlistBox.Location = new System.Drawing.Point(8, 240);
      downlistBox.Size = new System.Drawing.Size(200, 139);
      downlistBox.HorizontalScrollbar = true;
      downlistBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Bold);
      downlistBox.TabIndex = 6;
      
      uploadl.Location = new System.Drawing.Point(8, 40);
      uploadl.Text = "Files to Upload";
      uploadl.Size = new System.Drawing.Size(103, 16);
      uploadl.AutoSize = true;
      uploadl.Font = new System.Drawing.Font("Arial", 10f, System.Drawing.FontStyle.Bold);
      uploadl.TabIndex = 0;
      uploadl.BackColor = System.Drawing.Color.DarkOrange;
      
      clientlogt.ReadOnly = true;
      clientlogt.Size = new System.Drawing.Size(256, 320);
      clientlogt.ForeColor = System.Drawing.SystemColors.Window;
      clientlogt.TabIndex = 0;
      clientlogt.AutoSize = true;
      clientlogt.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Bold);
      clientlogt.AcceptsTab = true;
      clientlogt.TabStop = false;
      clientlogt.Location = new System.Drawing.Point(224, 64);
      clientlogt.BackColor = System.Drawing.Color.Orange;
      
      dicsob.Text = "Disconnect";
      dicsob.Pushed = true;
      dicsob.ToolTipText = "Click Here to Disconnect from Server";
      
      connectb.Text = "Connect";
      connectb.ToolTipText = "Click here to connect to Server";
      
      toolBar1.Size = new System.Drawing.Size(492, 30);
      toolBar1.BorderStyle = System.WinForms.BorderStyle.Fixed3D;
      toolBar1.Font = new System.Drawing.Font("Arial", 9f, System.Drawing.FontStyle.Bold);
      toolBar1.DropDownArrows = true;
      toolBar1.TabIndex = 1;
      toolBar1.TabStop = true;
      toolBar1.ShowToolTips = true;
      toolBar1.TextAlign = System.WinForms.ToolBarTextAlign.Right;
      toolBar1.Buttons.All = new System.WinForms.ToolBarButton[] {connectb,
         toolBarButton1,
         dicsob,
         toolBarButton2,
            uploadb,
         toolBarButton3,
         downloadb,
         toolBarButton4};
      toolBar1.ButtonClick+=new ToolBarButtonClickEventHandler(tools) ;
      toolBarButton4.Style = System.WinForms.ToolBarButtonStyle.Separator;
      toolBarButton3.Style = System.WinForms.ToolBarButtonStyle.Separator;
      toolBarButton2.Style = System.WinForms.ToolBarButtonStyle.Separator;
      //@design mainMenu1.SetLocation(new System.Drawing.Point(7, 7));
      toolBarButton1.Style = System.WinForms.ToolBarButtonStyle.Separator;
         
      this.Text = "FileShare Client" ; 
         
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.Menu = mainMenu1;
      this.BackColor = System.Drawing.SystemColors.Desktop;
      this.ClientSize = new System.Drawing.Size(492, 413);
      
      this.Controls.Add(statusBar1);
      this.Controls.Add(clientlogt);
      this.Controls.Add(logl);
      this.Controls.Add(downlistBox);
      this.Controls.Add(downl);
      this.Controls.Add(uploadlistBox);
      this.Controls.Add(uploadl);
      this.Controls.Add(toolBar1);
   }
      /// 
      ///      This method gets called when the user clicks on the connect Menu
      ///      This method shows a Custom Dialog which contains all the 
      ///      client settings. 
      ///      It then gets the value from the dialog and starts connecting to the 
      ///      Speficied Server .
      /// 
      
      private void clientconnect(object sender, EventArgs e)
      {
         //Make a instance of our custom Dialog
         ctdl = new clientdialog() ;
         //Get the result pressed by the user on the Dialog
         DialogResult ret = ctdl.ShowDialog(this) ;
         //If the user pressed 'Ok' then assign the values to
         //our variables.
         if(ret == DialogResult.OK) {
               this.port=int.Parse(ctdl.portt.Text) ;
               this.address=ctdl.servert.Text ;
               this.updir = ctdl.fileupt.Text ;
               this.dldir=ctdl.filedlt.Text ;
               this.user=ctdl.usert.Text ;
            if(!Directory.DirectoryExists(dldir))
            {
               MessageBox.Show(this,"The directory to Download Files Does not Exist \n Try Connecting Again");
            }
            else if(!Directory.DirectoryExists(updir))
            {
               MessageBox.Show(this,"The directory to Upload Files Does not Exist \n Try Connecting Again");
            }
            else
            {
               //clear the log screen
               clientlogt.Text="";
               //set the buttons
                  dicsob.Pushed=false ;
               connectb.Pushed=true ;
               statusBar1.Text="Connecting to Server... ";
               //to get the Upload and Download directories
               //call the Method which gets the files and updates
               //them in our ListBoxes
                  GetDirs();
               //Call the method which will connect to the 
               //server.
                  InitilizeConn();
            }
            }
      }
      /// 
      ///      This method is called when the client clicks the 
      ///      Disconnect menuitem. It closes the connection to the server
      ///      and resets the buttons and listboxes. 
      /// 
      private void clientdisconnect(object sender, EventArgs e)
      {
         //If a socket connection Exisis then close it
         if(clientsocket!=null)
         {
            SendMessage(clientsocket, "QUIT "+user) ;
            clientsocket.Close();
         }
         //Abort the client thread
         if(clientthread!=null&&clientthread.IsAlive)
         {
            clientthread.Abort();
         
         }
         //reset the Buttons
         statusBar1.Text="Disconnected!!!";
         clientlogt.Text+="Disconnected !!" ;
         downlistBox.Items.Clear();
         dicsob.Pushed=true;
         downloadb.Pushed=false ;
         uploadb.Pushed=false ;
         connectb.Pushed=false ;
      }
      /// 
      ///      This method is called when the user clicks the Exit Menuitem.
      ///      It calls the Dispose() method to exit the program
      /// 
      private void clientexit(object sender, EventArgs e)
      {
         Dispose(); 
      }
      /// 
      ///      This is the method called when the user clicks the 
      ///      Download Menuitem.
      ///      It has the code to Download the selected file from the server 
      /// 
      
      private void downfile(object sender, EventArgs e)
      {
         //check for connection and if some other download or
         //upload process is already going on.
         if(connected && !locked)
         {
            //Get the File selected for download
            if(downlistBox.SelectedItem==null)
            {
               MessageBox.Show(this,"Please Select a file to download first and then click download") ;
            }
            else
            {
               //Get the File Selected for doenload from the ListBox
               string selefile =(string)downlistBox.SelectedItem ;
               //Send the Server "DOWN" command with the FileName to download
               //from the server
               SendMessage(clientsocket ,"DOWN "+selefile) ;
               //set a global variable with the Filename
               readwrite=selefile ;
               clientlogt.Text+="Sending Download Request to server for file "+selefile+"\n" ;
               clientthread=null ;
               downloadb.Pushed=true ;
               uploadb.Pushed=true ;
               //Set the buttons and start the Downloading method "DownloadFile" in a 
               //Thread
               Thread downthread = new Thread(new ThreadStart(DownloadFile)) ;
               downthread.Start() ;
            }
         }
      }
      /// 
      ///      This method gets called when the user clicks on the Upload MenuItem
      ///      It takes care of all the Upload procedure
      /// 
      
      private void uploadfile(object sender, EventArgs e)
      {
         //Check if connection is present and if any other upload
         //or download process is going on.
         if(connected && !locked)
         {
            //Get the File Selected for upload from the ListBox
            if(uploadlistBox.SelectedItem==null)
            {
               MessageBox.Show(this,"Please Select a file to upload first and then click Upload") ;
            }
            else
            {
               try{
                  string selefile =uploadlistBox.SelectedItem.ToString() ;
                  File ftemp = new File(selefile) ;
                  //Construct a string to send to the server with the Command "UPFL"
                  //With the Command the client also sends the FileName and the 
                  //File Length
                  string ttt = "UPFL "+ftemp.FullName+"@"+ftemp.Length.ToString() ;
                  //Send the message
                  SendMessage(clientsocket ,ttt) ;
                  //Set a global variable for the Selected file
                  readwrite=selefile ;
                  //Do some buttons and Log settings
                  clientlogt.Text+="Sending Upload Request to server for file "+selefile+"\n" ;
                  downloadb.Pushed=true ;
                  uploadb.Pushed=true ;
                  //Start a thread on the Method "UploadFile" 
                  Thread upthread = new Thread(new ThreadStart(UploadFile)) ;
                  upthread.Start() ;
               }
               catch(Exception eg)
               {
                  MessageBox.Show(this,"Exception occured in upload click "+eg.ToString());
                  downloadb.Pushed=false ;
                  uploadb.Pushed=false ;
               }
            }
         }
      }
      /// 
      ///      Method called when the Help MenuItem is clicked
      /// 
      private void contactme(object sender, EventArgs e)
      {
         MessageBox.Show(this,"This is the FileShare Client") ; 
      }
      /// 
      ///      This method is the event handler for the the ToolBar buttons.
      ///      The code here is the same repeat of the Above "Connect", "Disconnect"
      ///      ,"Download" and "Upload" MenuItem Methods 
      /// 
      
      private void tools(object sender,  ToolBarButtonClickEventArgs e)
      {
         if(e.button==connectb)
         {
            
            ctdl = new clientdialog() ;
            DialogResult ret = ctdl.ShowDialog(this) ;
            if(ret == DialogResult.OK) {
                  this.port=int.Parse(ctdl.portt.Text) ;
                  this.address=ctdl.servert.Text ;
                  this.updir = ctdl.fileupt.Text ;
                  this.dldir=ctdl.filedlt.Text ;
                  this.user=ctdl.usert.Text ;
               if(!Directory.DirectoryExists(dldir))
               {
                  MessageBox.Show(this,"The directory to Download Files Does not Exist \n Try Connecting Again");
               }
               else if(!Directory.DirectoryExists(updir))
               {
                  MessageBox.Show(this,"The directory to Upload Files Does not Exist \n Try Connecting Again");
               }
               else
               {
                  clientlogt.Text="";
                     dicsob.Pushed=false ;
                  connectb.Pushed=true ;
                  statusBar1.Text="Connecting To Server ...";
                     GetDirs();
                     InitilizeConn();
               }
               }
         }
         else if(e.button==dicsob)
         {
            if(clientsocket!=null)
            {
               SendMessage(clientsocket, "QUIT "+user) ;
               clientsocket.Close();
            }
            if(clientthread!=null&&clientthread.IsAlive)
            {
               clientthread.Abort();
         
            }
            statusBar1.Text="Disconnected!!";
            clientlogt.Text+="Disconnected !!";
            downlistBox.Items.Clear();
            connectb.Pushed=false ;
            downloadb.Pushed=false ;
            uploadb.Pushed=false ;
            dicsob.Pushed=true;
         }
         else if(e.button==uploadb)
         {
            if(connected && !locked)
            {
               if(uploadlistBox.SelectedItem==null)
               {
                  MessageBox.Show(this,"Please Select a file to upload first and then click Upload") ;
               }
               else
               {
                  string selefile =uploadlistBox.SelectedItem.ToString() ;
                  File ftemp = new File(selefile) ;
                  SendMessage(clientsocket ,"UPFL "+ftemp.FullName+"@"+ftemp.Length) ;
                  readwrite=selefile ;
                  clientlogt.Text+="Sending Upload Request to server for file "+selefile+"\n" ;
                  downloadb.Pushed=true ;
                  uploadb.Pushed=true ;
                  Thread upthread = new Thread(new ThreadStart(UploadFile)) ;
                  upthread.Start() ;
               }
            }
         }
         else if(e.button==downloadb)
         {
            if(connected && !locked)
            {
               if(downlistBox.SelectedItem==null)
               {
                  MessageBox.Show(this,"Please Select a file to download first and then click download") ;
               }
               else
               {
                  string selefile =(string)downlistBox.SelectedItem ;
                  SendMessage(clientsocket ,"DOWN "+selefile) ;
                  readwrite=selefile ;
                  clientlogt.Text+="Sending Download Request to server for file "+selefile+"\n" ;
                  downloadb.Pushed=true ;
                  uploadb.Pushed=true ;
                  Thread downthread = new Thread(new ThreadStart(DownloadFile)) ;
                  downthread.Start() ;
               }
            }
         }
      }
      
      /// 
      ///      This method Will connect to the Server using the settings
      ///      specified by the client properties dialog.
      /// 
      public void InitilizeConn()
      {
         try
         {
            //Initilize a socket of the Typr TCP
            clientsocket = new Socket(AddressFamily.AfINet,SocketType.SockStream,ProtocolType.ProtTCP);
            clientlogt.Text+="Trying to connect to Server ......\r" ;
            //Resolve the DNS of the server
            IPAddress host_addr = DNS.Resolve(address);
            //Create a IPEndPoint to the Server
            IPEndPoint ep = new IPEndPoint(host_addr, port);
            //Check if the client could connect to the Server
            //If the client is connected Zero is returned
            if(clientsocket.Connect(ep)==0)
            {
               //Start a Thread on the Method "doTalk" whic will handle
               //futher talking with the server
               statusBar1.Text="Connected !!";
               clientlogt.Text+="Connected to Server: "+ep.ToString()+" \r" ;
               clientthread =  new Thread(new ThreadStart(doTalk)) ;
               clientthread.Start() ;
            }
            else
            {
               //Else there was a error Connecting
               dicsob.Pushed=true;
               downloadb.Pushed=false ;
               uploadb.Pushed=false ;
               connectb.Pushed=false ;
               statusBar1.Text="Error" ;
               clientlogt.Text="Error: Cannot Connect to Server !!";
            }
         }
         catch(Exception ed)
         {
            dicsob.Pushed=true;
            downloadb.Pushed=false ;
            uploadb.Pushed=false ;
            connectb.Pushed=false ;
            statusBar1.Text="Error" ;
            Console.WriteLine("Exception occured in Connecting to Server :"+ed.ToString()) ;
         }
      }
         /// 
         ///      A Method which will resolve the Files in the Upload and 
         ///      download directory and add it to the ListBox
         /// 
         private void GetDirs()
         {
            //check the dirctory
            if(Directory.DirectoryExists(updir)) {
               //Make a global variable containing all the files in the Upload Directory
               upfiles = Directory.GetFilesInDirectory(updir) ;
               //add the files to the ListBox
               foreach (File f in upfiles)
               {
                  uploadlistBox.InsertItem(0,f);
               }
            }  
         }
      /// 
      ///      This Method runs in a Thread called from the InitilizeConn method.
      ///      This method does the Sending and receiving of normal commands with the server
      /// 
      private void doTalk()
      {
         //Bool variable to indicate if initial talk between the Client
         //and server is going on
         bool StayConnected=true ;
         while(StayConnected)
         {
            string ServerCommand ;
            //Declare a Byte receive buffer
            byte[] recs = new byte[2048];
            try{
               //Receive Bytes from the Server           
               int scount = clientsocket.Receive(recs , recs.Length,0) ;
               //Decode the Server Message to String
               string servermessage = System.Text.Encoding.ASCII.GetString(recs) ;
               //check if the server sent more than one Byte
               if(scount >0)
               {
                  //Call a method ParseMessage with the Server Message as a Parameter
                  // This Method will extract the Server Comand from the Server Message
                  ServerCommand = ParseMessage(servermessage) ;
               }
               else
               {
                  //If no message is received then assume a custom Command
                  ServerCommand="NOOP" ;
               }
               //Call a method ParseComand with parameters as the Server Command and the Server Message
               //This method does action depending on the server command
               //It returns a Bool value depending on the server Command
               StayConnected = ParseCommand(ServerCommand , servermessage) ;
            }
            catch(Exception ec)
            {
               statusBar1.Text="Error!!" ;
               StayConnected = false ; 
               MessageBox.Show(this,"Exception Occured while Talking to Server :"+ec.ToString()) ;
            }
         }
      }
      /// 
      ///      This Method is Used to send Messages to the Server using the 
      ///      Socket connection
      /// 
      private void SendMessage(Socket sendsock , string message)
      {
         //Fill up a Byte array with the Command to send to the server encoded
         //in ASCII format
         byte[] sender = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray()) ;
         //Send the Message
         sendsock.Send(sender, sender.Length,0) ;
      }
      /// 
      ///      This method takes Server Message as a Parameter and 
      ///      send back the server command from the message
      /// 
      private string ParseMessage(string clientlogt)
      {
         string ServerCommand ;
         if(clientlogt=="")
         {
            return "NOOP" ;
         }
         //The Command Length is 4 so we substring the first 4 
         //Bytes from the Message
         ServerCommand = clientlogt.Substring(0,4) ;
         return ServerCommand ;
      }
      /// 
      ///      This message Get the Server Command and Server Message as Parameters
      ///      It Then Takes Care of all Executing to be done depending on the Server Command 
      /// 
      private bool ParseCommand(string ServerCommand , string servermessage ) 
      {
         if(ServerCommand=="CONN")
         {
            //This is the First Message Sent by the server upon
            //receiving the Client Connection request
            string temp = servermessage.Substring(4) ;
            string mes= temp.Trim() ;
            clientlogt.Text+="Server :"+mes+" \r";
            //In response to this Command we Reply with the "USER" Command
            //along with it We send our Username to the Server
            SendMessage(clientsocket,"USER "+user) ;
            connected=true ;
            return true ;
         }
         if(ServerCommand=="NOOP") 
         {
            //A command to do Nothing
            Thread.Sleep(100);
            return true ;
         }
         if(ServerCommand=="LIST")
         {
            //This Command is sent by the server along wiht a list of all the files it is
            //offering for download
            //Individual FileNames are Seperated by a "@" sign
            string temp = servermessage.Substring(6) ;
            string mes= temp.Trim() ;
            string[] temp2= mes.Split(new char[]{'@'}) ;
            //Breakup the Server string and Fillup the ListBox with the
            //FileName only of the Files available for download
            foreach(string s in temp2)
            {
               int i = s.LastIndexOf("\\") ;
               downlistBox.InsertItem(0,s.Substring(i+1)) ;
            }           
            return false ;
         }
         if(ServerCommand=="RECD") 
         {
            //This command is sent by the Server to Indicate that it has
            //Received the Uploaded file
            return true ;
         }
         //If some other command is sent the return False
         return false ;
      }
      /// 
      ///      This method takes care of download the file from the Server
      /// 
      private void DownloadFile()
      {
         //Declare some variable that will be used later
         bool done =false ;
         bool check= false ;
         locked=true ;
         long size=0 ;
         long rby=0 ;
         while(!done)
         {
            //declare a buffer
            byte[] rce = new byte[2048] ;
            //Recive a Sever Message
            int i = clientsocket.Receive(rce,rce.Length,0) ;
            //Convert it to string
            string servermessage = System.Text.Encoding.ASCII.GetString(rce);
            if(i>0)
            {
               //Parse the Message to get the Server Command
               string command = ParseMessage(servermessage);
               //Check if the command is SIZE 
               //this command is sent along with the Size of the File 
               //Which the client Requested for download
               if(command=="SIZE")
               {
                  string temp = servermessage.Substring(4) ;
                  string mes= temp.Trim() ;
                  //store the File Size 
                  size=Int64.Parse(mes) ;
                  clientlogt.Text+="Receiving File of "+size+" bytes \n";
                  //Send a "SEND" command to the server which will
                  //make the server send the File
                  SendMessage(clientsocket , "SEND "+readwrite );
                  done=true ;
                  check=true ;
               }
               if(command=="NOPE")
               {
                  //a "NOPE" command is returned when the File asked if not availabe or
                  //if server has restricted downloads
                  string temp = servermessage.Substring(4) ;
                  string mes= temp.Trim() ;
                  clientlogt.Text+="File Receive Error "+mes+" \n" ;
                  downloadb.Pushed=false ;
                  uploadb.Pushed=false ;
                  done=true ;
                  //We send a "OHHH" command in response
                  SendMessage(clientsocket ,"OHHH No Problem") ;
                  check=false ;
               }              
            }
         }
         //The File Size has been received the continue 
         done=false ;
         //Make a File with the same name as the File that is being downloaded
         //Also open a Network Stream to the Server
         FileStream fout = new FileStream(dldir+readwrite, FileMode.OpenOrCreate , FileAccess.Write) ;         
         NetworkStream nfs = new NetworkStream(clientsocket) ;
         byte[] buffer = new byte[4096] ;
         statusBar1.Text="Downloading File from Server..." ;
         while(!done&&check)
         {
            try{
               long v=0 ;
               //loop till the Full bytes have been read
               while(rby<size)
               {
                  //Read from the Network Stream
                  int i = nfs.Read(buffer,0,buffer.Length) ;
                  if(i>0)
                  {
                     //Some checking done to detremine the number of Bytes to be written
                     if(i>=4096&&(size-rby)>=4096)
                     {
                        v=4096 ;
                     }
                     else if(i<4096 &&(size-rby)>=4096)
                     {
                        v= i;
                     }
                     else
                     {
                        v=(size-rby) ;
                     }
                     //Write the Bytes received to the File
                     fout.Write(buffer,0,(int)v) ;
                     rby=rby+v ;
                     
                  }
               }
                  
               clientlogt.Text+="File Received sucessfully "+rby+"bytes \n";
               //Send a "RECD" Command to the Server in response
               statusBar1.Text="File Downloaded" ;
               SendMessage(clientsocket,"RECD File!!") ;
               downloadb.Pushed=false ;
               uploadb.Pushed=false ;
               fout.Close() ;
               done=true ;
               locked=false ;
            }
            catch(Exception ed)
            {
               statusBar1.Text="Error!!" ;
               MessageBox.Show(this,"A Exception occured in file transfer"+ed.ToString());
            }
         }
      }
         /// 
         ///      This method taks care of Uploading the Selected File to the Server
         /// 
         private void UploadFile()
         {  
            //Set some Variables
            bool check =true ;
            locked=true ;
            bool done =false ;
            int  i=0 ;
            long rdby=0 ;
            File ftemp = new File(readwrite) ;
            long total = ftemp.Length ;
            ftemp=null ;
            byte[] rce = new byte[2048] ;
            string servermessage ;
            while(!done)
            {
               //Receive Bytes from the Server
               i = clientsocket.Receive(rce,rce.Length,0) ;
               servermessage = System.Text.Encoding.ASCII.GetString(rce);
               if(i>0)
               {
                  //Parse the Server Message to get the Command
                  string command = ParseMessage(servermessage);
                  if(command=="SEND")
                  {
                     //Server Sends a "SEND" command if it allows the client to upload files
                     string temp = servermessage.Substring(4) ;
                     string mes= temp.Trim() ;
                     clientlogt.Text+="Server Ready to Accept File \n";
                     done=true ;
                     check=true ;
                  }
                  if(command=="NOPE")
                  {
                     //A "NOPE" Command is sent if the Server does not allow Uploading 
                     string temp = servermessage.Substring(4) ;
                     string mes= temp.Trim() ;
                     clientlogt.Text+="File Send Error "+mes+" \n" ;
                     downloadb.Pushed=false ;
                     uploadb.Pushed=false ;
                     done=true ;
                     //Send a Response to the Server
                     SendMessage(clientsocket ,"OHHH No Problem") ;
                     check=false ;
                  }
               }                       
            }
            //If the Server has Requested to Send the file then open up the 
            //FileStreams to the file to be Uploaded
            statusBar1.Text="Uploading File to Server" ;
            i=0;
            FileStream fin = new FileStream(readwrite , FileMode.Open , FileAccess.Read) ;
            byte[] reader = new byte[4096] ;
            //Loop till the File is totaly read
            while(rdby<total&&check)
            {
               //Read from the File
               i = fin.Read(reader,0,reader.Length) ;
               //Send the Bytes to the Server
               clientsocket.Send(reader,i,0) ;
               rdby=rdby+i ;
            }
            fin.Close();
            i=0;
            done=false ;
            while(!done){
               //After finishing Sending the File Wait for a Server Command
               i = clientsocket.Receive(rce,rce.Length,0) ;
               servermessage = System.Text.Encoding.ASCII.GetString(rce);
               if(i>0)
               {
                  string command = ParseMessage(servermessage);
                  if(command=="RECD")
                  {
                     //Server Sends a "RECD" Command if the file was receive sucessfully
                     statusBar1.Text="File Uploaded" ;
                     string temp = servermessage.Substring(4) ;
                     string mes= temp.Trim() ;
                     Console.WriteLine("Server Received File :"+mes) ;
                     downloadb.Pushed=false ;
                     uploadb.Pushed=false ;
                     clientlogt.Text+="Server Received File \n";
                     locked=false ;
                     done=true ;
                  }
                  
               }
               
            }
            
         }
    }//client class
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
   /// 
   ///      This is a Class which will be our Client Dialog Class
   /// 
    public class clientdialog : System.WinForms.Form {
        ///  
        ///    Required by the Win Forms designer 
        /// 
   private System.ComponentModel.Container components;
    private System.WinForms.ToolTip toolTip1;
    private System.WinForms.Button defaultb;
   public System.WinForms.TextBox filedlt;
    private System.WinForms.Label fdll;
    public System.WinForms.TextBox usert;
    private System.WinForms.Label userl;
    public System.WinForms.TextBox servert;
    private System.WinForms.Label serverl;
    private System.WinForms.Button cancelb;
    private System.WinForms.Button okb;
    public System.WinForms.TextBox fileupt;
    private System.WinForms.Label fupl;
    public System.WinForms.TextBox portt;
    private System.WinForms.Label portl;
    private System.WinForms.Label lb1;
    
    /// 
    ///     Constructor of the Class
    /// 
    
      public clientdialog() {
         // Required for Win Form Designer support
         InitializeComponent();
        }
        /// 
        ///    Clean up any resources being used
        /// 
        public override void Dispose() {
            base.Dispose();
            components.Dispose();
        }
        /// 
        ///    Required method for Designer support - do not modify
        ///    the contents of this method with an editor
        /// 
        private void InitializeComponent()
      {
      this.components = new System.ComponentModel.Container();
      this.servert = new System.WinForms.TextBox();
      this.fupl = new System.WinForms.Label();

Snippet Comments
Comments:
No comments for this snippet

Other snippets that may interest you..
Related Snippets
TDT - GridPage - Page_Init (C#)
CustomValitaor (C#)
(C#)
Get the file extension from a file path or file name (VB.NET)
grab record from db and subtracts it from variable (VB.NET)



Copyright ©2009-2018 CodeXchange. Server version 1.0.3720.32855 Client Version 0.9.0.0

With from Barcelona

Most Helpful members
These are the members who have received the most points for their helpful samples
Zepho Zep
Robert Wagner
Galen Taylor

All time 'Hall of fame'
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
INI File Access (VB.NET)
Read XML from string into DataSet (C#)
Create Manifest File for your Application (VB.NET)
Round function to avoid banker's rounding (VB.NET)
Get Short and Long Path Names (VB.NET)
Sending Mail through authenticated SMTP server (C#)
One Way Hash for strings (C#)
Formating a file size and adding the B, KB, MB and GB extension appropriately with string.Format (C#)
How do I load an image from a URI address? (VB.NET)
Use our easy to use Visual Studio.NET addin client and start sharing code snippets with the CodeXchange community!
Refreshed: Wednesday, October 17, 2018