Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help - Send a File via Socket

Help - Send a File via Socket

Scheduled Pinned Locked Moved C#
sysadminhelptutorial
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FlyOnIT
    wrote on last edited by
    #1

    I have a Client and a Sever, i connect the client to the server via Socket, now i'd like to send a file from the Client to the Server, i read about Socket.SendFile(fileName), but can't find how, from the server i have to receive the file using it (sendfile) in the client, i'd like to see an example, thank you!

    A 1 Reply Last reply
    0
    • F FlyOnIT

      I have a Client and a Sever, i connect the client to the server via Socket, now i'd like to send a file from the Client to the Server, i read about Socket.SendFile(fileName), but can't find how, from the server i have to receive the file using it (sendfile) in the client, i'd like to see an example, thank you!

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      Hy, You don't need to send a file with the Send File method. The send method send a byte array. So use FileStream to read the byte content of the file and then send it to the client/server

      //read the file
      FileStream fstream = File.Open(filename,FileMode.Open);
      byte []buffer = new byte[fstream.Lenght];
      fstream.Read(buffer,0,buffer.Length);
      fstream.Close();

      //send it
      ClientSocket.Send(buffer);

      It works fine on any kind of files. After you have received the file use FileStream to write the file (of course you should know the extension to put to the file). Hope it helps

      Do your best to be the best

      F 1 Reply Last reply
      0
      • A Andrei Ungureanu

        Hy, You don't need to send a file with the Send File method. The send method send a byte array. So use FileStream to read the byte content of the file and then send it to the client/server

        //read the file
        FileStream fstream = File.Open(filename,FileMode.Open);
        byte []buffer = new byte[fstream.Lenght];
        fstream.Read(buffer,0,buffer.Length);
        fstream.Close();

        //send it
        ClientSocket.Send(buffer);

        It works fine on any kind of files. After you have received the file use FileStream to write the file (of course you should know the extension to put to the file). Hope it helps

        Do your best to be the best

        F Offline
        F Offline
        FlyOnIT
        wrote on last edited by
        #3

        I'm having problem in the Server Side; How do i receive the file? and if i need the extension of the file, i have to send it with the client? and then how do i retrieve it? Thanks else if (data.StartsWith("$UploadFile")) { }

        A 1 Reply Last reply
        0
        • F FlyOnIT

          I'm having problem in the Server Side; How do i receive the file? and if i need the extension of the file, i have to send it with the client? and then how do i retrieve it? Thanks else if (data.StartsWith("$UploadFile")) { }

          A Offline
          A Offline
          Andrei Ungureanu
          wrote on last edited by
          #4

          You can create your own communication protocol. Let's say that the client sends a message to the server telling him that he will upload a file with a specified extension and size. The server sends back an acknowledge message and the the client sends the file. Concerning the server. If you know the exact size of the file then you can do something like:

          byte[] buffer = new byte[size];
          client.Receive(buffer);

          If you do not know the size of the file then you can write a sequence that reads data from the network in packets let's say 1024 long, like this:

          ArrayList list = new ArrayList();
          byte[] buffer = new byte[1024];
          int read = 0;
          int length = 0;
          while ((read = client.Receive(buffer) != 0)
          {
          length += read;
          list.Add(buffer);
          buffer = new byte[1024];
          if (read < 1024) // reached the end of file
          break;
          }

          At the end of this code you have inside the array list the entire file stored in 1024 length byte , arrays. After this just extract the byte arrays from the array list, like this.

          buffer = new byte[length];
          byte []temp = null;
          int k = 0;
          for (int i = 0 ; i < list.Count ; i++)
          {
          temp = (byte[])list[i];
          for (int j = 0 ; j < temp.Length ; j++)
          byte[k++] = temp[j];
          }

          Hope it helps.

          Do your best to be the best

          F 1 Reply Last reply
          0
          • A Andrei Ungureanu

            You can create your own communication protocol. Let's say that the client sends a message to the server telling him that he will upload a file with a specified extension and size. The server sends back an acknowledge message and the the client sends the file. Concerning the server. If you know the exact size of the file then you can do something like:

            byte[] buffer = new byte[size];
            client.Receive(buffer);

            If you do not know the size of the file then you can write a sequence that reads data from the network in packets let's say 1024 long, like this:

            ArrayList list = new ArrayList();
            byte[] buffer = new byte[1024];
            int read = 0;
            int length = 0;
            while ((read = client.Receive(buffer) != 0)
            {
            length += read;
            list.Add(buffer);
            buffer = new byte[1024];
            if (read < 1024) // reached the end of file
            break;
            }

            At the end of this code you have inside the array list the entire file stored in 1024 length byte , arrays. After this just extract the byte arrays from the array list, like this.

            buffer = new byte[length];
            byte []temp = null;
            int k = 0;
            for (int i = 0 ; i < list.Count ; i++)
            {
            temp = (byte[])list[i];
            for (int j = 0 ; j < temp.Length ; j++)
            byte[k++] = temp[j];
            }

            Hope it helps.

            Do your best to be the best

            F Offline
            F Offline
            FlyOnIT
            wrote on last edited by
            #5

            Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }

            F 1 Reply Last reply
            0
            • F FlyOnIT

              Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }

              F Offline
              F Offline
              FlyOnIT
              wrote on last edited by
              #6

              Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }

              F 1 Reply Last reply
              0
              • F FlyOnIT

                Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }

                F Offline
                F Offline
                FlyOnIT
                wrote on last edited by
                #7

                Please someone can help me?

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups