Solved

Best way of transferring files using socket

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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 …

749 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