Solved

Best way of transferring files using socket

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

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 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

16 Experts available now in Live!

Get 1:1 Help Now