Solved

Best way of transferring files using socket

Posted on 2010-09-14
4
341 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now