File Transfer
-
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);
-
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);
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.
-
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.
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!
-
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.
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 -
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, 2006When 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.
-
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.