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. File Transfer

File Transfer

Scheduled Pinned Locked Moved C#
sysadminannouncement
6 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.
  • U Offline
    U Offline
    unrealed
    wrote on last edited by
    #1

    Hello everybody, I tryed to make a software that check for updates: Client: Login ( username , password ) , Receive update list , request updating files , get files , execute another process. Server: Receive login , login on data-base if possible , send update list if logged , send requested files , logout. Everythings are done, if I run CLIENT and SERVER on local computer, everythings works fine, but if I run SERVER on my computer, and CLIENT in another computer, the UPDATE doesnt work fine. What happens: Server can send any Text file good, in perfect state, but if I send binary files like JPG images, EXE, or any other things that arent TEXT FILES, it sends wrong BYTES, like if server is encoding all bytes to TEXT format... === CLIENT GET FILE VOID: //Private Void: private void GetFile ( int FileNumber ) { string FilePath = UpdateFileList[FileNumber]; long FileSize = UpdateSizeList[FileNumber]; long CurrentSize = 0; if ( FilePath.IndexOf("\\") > 0 ) { string newDirectory = FilePath.Substring(0,FilePath.IndexOf("\\")); if ( !Directory.Exists(newDirectory) ) Directory.CreateDirectory(newDirectory); } FileStream FS = new FileStream(FilePath,FileMode.Create,FileAccess.Write); while ( CurrentSize < FileSize ) { try { this.Stats_Text.Text = "Downloading updatings ( " + (FileNumber+1) + " / " + UpdateFileList.Length + " )\n" + FilePath + " ( " + Convert.ToByte((Convert.ToDouble(CurrentSize)/Convert.ToDouble(FileSize))*100) + " % completed of " + (FileSize/1024) + " KBs )"; long PacketSize = FileSize-CurrentSize; if ( PacketSize > 128 ) PacketSize = 128; byte[] Packet = new byte[PacketSize]; NS.Read(Packet,0,Packet.Length); NS.Flush(); FS.Write(Packet,0,Packet.Length); FS.Flush(); CurrentSize += PacketSize; FS.Seek(CurrentSize,SeekOrigin.Begin); } catch { try { File.Delete(FilePath); } catch {} this.Stats_Text.Text = "The connection with the Server was lost"; Disconnect(); } } FS.Close(); } === SERVER SEND FILE VOID: //Private Void: private void SendFile ( int FileNumber ) { Console.Write("US - Sending updating file : " + (FileNumber+1) + " / " + Owner.UpdateFileList.Length + " [ " + ClientUserName + " ]\n"); string FilePath = Owner.UpdateFileList[FileNumber]; long FileSize = Owner.UpdateSizeList[FileNumber]; long CurrentSize = 0; FileStream FS = new FileStream(FilePath,FileMode.Open,FileAccess.Read);

    L 1 Reply Last reply
    0
    • U unrealed

      Hello everybody, I tryed to make a software that check for updates: Client: Login ( username , password ) , Receive update list , request updating files , get files , execute another process. Server: Receive login , login on data-base if possible , send update list if logged , send requested files , logout. Everythings are done, if I run CLIENT and SERVER on local computer, everythings works fine, but if I run SERVER on my computer, and CLIENT in another computer, the UPDATE doesnt work fine. What happens: Server can send any Text file good, in perfect state, but if I send binary files like JPG images, EXE, or any other things that arent TEXT FILES, it sends wrong BYTES, like if server is encoding all bytes to TEXT format... === CLIENT GET FILE VOID: //Private Void: private void GetFile ( int FileNumber ) { string FilePath = UpdateFileList[FileNumber]; long FileSize = UpdateSizeList[FileNumber]; long CurrentSize = 0; if ( FilePath.IndexOf("\\") > 0 ) { string newDirectory = FilePath.Substring(0,FilePath.IndexOf("\\")); if ( !Directory.Exists(newDirectory) ) Directory.CreateDirectory(newDirectory); } FileStream FS = new FileStream(FilePath,FileMode.Create,FileAccess.Write); while ( CurrentSize < FileSize ) { try { this.Stats_Text.Text = "Downloading updatings ( " + (FileNumber+1) + " / " + UpdateFileList.Length + " )\n" + FilePath + " ( " + Convert.ToByte((Convert.ToDouble(CurrentSize)/Convert.ToDouble(FileSize))*100) + " % completed of " + (FileSize/1024) + " KBs )"; long PacketSize = FileSize-CurrentSize; if ( PacketSize > 128 ) PacketSize = 128; byte[] Packet = new byte[PacketSize]; NS.Read(Packet,0,Packet.Length); NS.Flush(); FS.Write(Packet,0,Packet.Length); FS.Flush(); CurrentSize += PacketSize; FS.Seek(CurrentSize,SeekOrigin.Begin); } catch { try { File.Delete(FilePath); } catch {} this.Stats_Text.Text = "The connection with the Server was lost"; Disconnect(); } } FS.Close(); } === SERVER SEND FILE VOID: //Private Void: private void SendFile ( int FileNumber ) { Console.Write("US - Sending updating file : " + (FileNumber+1) + " / " + Owner.UpdateFileList.Length + " [ " + ClientUserName + " ]\n"); string FilePath = Owner.UpdateFileList[FileNumber]; long FileSize = Owner.UpdateSizeList[FileNumber]; long CurrentSize = 0; FileStream FS = new FileStream(FilePath,FileMode.Open,FileAccess.Read);

      L Offline
      L Offline
      LighthouseJ
      wrote on last edited by
      #2

      You could get a program like Ethereal and watch the traffic and see which side is corrupting the data for a start. That will let you narrow down which side is causing the problems and give you more information. Other than that, check your properties and make sure your options are set properly. I had to deal with serial communications and it kept on screwing up at a certain character for seemingly no reason. The fix was that I forgot to set some ancient option buried in documentation.

      U 2 Replies Last reply
      0
      • L LighthouseJ

        You could get a program like Ethereal and watch the traffic and see which side is corrupting the data for a start. That will let you narrow down which side is causing the problems and give you more information. Other than that, check your properties and make sure your options are set properly. I had to deal with serial communications and it kept on screwing up at a certain character for seemingly no reason. The fix was that I forgot to set some ancient option buried in documentation.

        U Offline
        U Offline
        unrealed
        wrote on last edited by
        #3

        Hello my friend, thank you very much, I tryed to hex the 2 files, look what happens: Real File: $121C ... 3B 00 CE 8B BE FE ... Downloaded File: $121C ... 3B 00 00 00 00 00 .... CE 8B BE FE ... Its like add '00 00 00 etc' in the mid of file. I am verifying right now if the CLIENT BUFFER is bigger than the packet, I ll check whats going on. Thank you for the help. if you know whats wrong, tell me, thx!

        1 Reply Last reply
        0
        • L LighthouseJ

          You could get a program like Ethereal and watch the traffic and see which side is corrupting the data for a start. That will let you narrow down which side is causing the problems and give you more information. Other than that, check your properties and make sure your options are set properly. I had to deal with serial communications and it kept on screwing up at a certain character for seemingly no reason. The fix was that I forgot to set some ancient option buried in documentation.

          U Offline
          U Offline
          unrealed
          wrote on last edited by
          #4

          Its working now, I had to modify my code: Server: ... NS.Write(Packet,0,Packet.Length); NS.Flush(); Thread.Sleep(5); CurrentSize += PacketSize; ... Client: ... NS.Read(Packet,0,Packet.Length); NS.Flush(); Thread.Sleep(5); CurrentSize += PacketSize; ... How ever, maybe 5 MS wont be enough, maybe 5 MS will be more than enough... Is there a METHOD to wait until the CLIENT receive all the packet? like: NS.Wait(); Thank you. -- modified at 11:31 Sunday 5th March, 2006

          L 1 Reply Last reply
          0
          • U unrealed

            Its working now, I had to modify my code: Server: ... NS.Write(Packet,0,Packet.Length); NS.Flush(); Thread.Sleep(5); CurrentSize += PacketSize; ... Client: ... NS.Read(Packet,0,Packet.Length); NS.Flush(); Thread.Sleep(5); CurrentSize += PacketSize; ... How ever, maybe 5 MS wont be enough, maybe 5 MS will be more than enough... Is there a METHOD to wait until the CLIENT receive all the packet? like: NS.Wait(); Thank you. -- modified at 11:31 Sunday 5th March, 2006

            L Offline
            L Offline
            LighthouseJ
            wrote on last edited by
            #5

            When I did serial communications, the hardest part was to efficiently handle the data which is what you're against, but that was in VC++ not C# but I'll take a shot in the dark. According to the NetworkStream documentation (which I assume you are using), there's a property called DataAvailable. Why not poll that property, when it's true, read the data? I don't know how you make sure you transmit all the bytes efficiently, but think about this: on a client connection: 1. server -> open file, capture file length as a 64-bit (8-byte) unsigned integer 2. server -> transmit the 8-bytes, client expects the same 8 bytes (at this point, the client knows how many bytes it'll receive) 3. client -> simply loop until the recv'd byte count matches what it's expecting Loop these 3 steps for each file you want to send. The most efficient communications code I've written is code that's very flexible but powerful. This includes making sure both sides are constantly at the same step in communication. In VC++, I used the timeout features of events inside of threads. Good luck.

            U 1 Reply Last reply
            0
            • L LighthouseJ

              When I did serial communications, the hardest part was to efficiently handle the data which is what you're against, but that was in VC++ not C# but I'll take a shot in the dark. According to the NetworkStream documentation (which I assume you are using), there's a property called DataAvailable. Why not poll that property, when it's true, read the data? I don't know how you make sure you transmit all the bytes efficiently, but think about this: on a client connection: 1. server -> open file, capture file length as a 64-bit (8-byte) unsigned integer 2. server -> transmit the 8-bytes, client expects the same 8 bytes (at this point, the client knows how many bytes it'll receive) 3. client -> simply loop until the recv'd byte count matches what it's expecting Loop these 3 steps for each file you want to send. The most efficient communications code I've written is code that's very flexible but powerful. This includes making sure both sides are constantly at the same step in communication. In VC++, I used the timeout features of events inside of threads. Good luck.

              U Offline
              U Offline
              unrealed
              wrote on last edited by
              #6

              Thank you my friend, I ll think about it :) I ll test this property called DataAvaible. Bye and really thank you.

              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