?
Solved

Best way of transferring files using socket

Posted on 2010-09-14
4
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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