Solved

Best way of transferring files using socket

Posted on 2010-09-14
4
343 Views
Last Modified: 2013-11-05
What is the best of of transferring files through sockets? i am a little confused about it as i don't want to lost my file while transferring.
0
Comment
Question by:muhammadyasir
  • 2
4 Comments
 
LVL 1

Expert Comment

by:golftech
ID: 33675452
What are you trying to do?
0
 
LVL 5

Author Comment

by:muhammadyasir
ID: 33675624
i want to update my client software by sending files to client.
0
 
LVL 5

Author Comment

by:muhammadyasir
ID: 33675656
also i need to send some important files from client side to server.
0
 
LVL 5

Accepted Solution

by:
andr_gin earned 500 total points
ID: 33680832
You should create a protocol for sending the files. I have following convention for data exchange via TCP:

Byte 1: Type of the message implemented as an Enumeration (for example SendFile=4)
Byte 2 to 5: Length of the information
Byte 6 to x: Information

FileContent can be embedded between these messages.
If you are concerned about extra safety you can calculate a checksum while reading.
Use the class MD5CryptoServiceProvider.

Example:
01 (SendFile)
00 00 00 30 (24 Bytes Data)
Filename (24 bytes), encode with System.Text.Encoding.UTF8.GetBytes

02 (FileLength)
00 00 00 08 (8 Bytes Length)
00 00 00 00 00 01 00 00 (64KB file)

64K bytes FileContent

03 (MD5 key)
00 00 00 10 (16 bytes = 128bit MD5 key)
MD5 key of file

The client first checks if byte1 is what he expects. If this is not correct you have lost synchronization and should terminate the connection.
Then the client reads 4 Bytes with the length.
Then he knows how long the filename is.
Then he reads the filename.
The next message is the length
Then the file content is embedded
Then the next message is the md5 key (calculated by server & client while transmitting data).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question