Xel
Xel

Reputation: 571

How to properly send and receive bytes/files using sockets?

I have a client and server program. The client sends a file to the server by first converting the file to bytes and then send it to the server. The server will then reconstruct the file using the bytes it receives. I am having a problem with the server program. Sometimes, the bytes it receives are incomplete.

Now, I have already searched through the internet and found out that it is a common problem among beginner programmers like myself. I have tried various solutions that I found but nothing worked. (I've been working on this for about 2 days already)

I was wondering what is the proper way of sending and receiving files/bytes between two programs in a LAN? (One being the server and the other is the client, though of course, there can be more than 1 client program that will connect to the server program)

I hope someone can help solve this problem. Please... I hope someone who is well-versed in socket programming and about bytes can provide helpful information on this.

Some extra information: I actually based my code from this forum topic: DANIWEB. Reading through the thread, the program worked perfectly and even managed to send a 400MB+ video file. In my case, I'm only sending small images and document files less than 10mb in size and my server program fails more often that it succeeds.

I already asked a question related to this problem, tried the answer given to me but my program still fails. I also found something in MSDN that is a little similar to the answer given to me in my question. Tried it as well, but my server program still fails.

Upvotes: 1

Views: 1272

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062945

You say "incomplete" - this usually points to one of two things:

  • the sender may have "nagle" enabled, and the last few bytes may still be held at their end (OS/NIC) until a large enough packet is available. Meaning: even though they called Send/Write/etc - it still hasn't left the client machine. This is not unexpected. They can force it to send by closing the socket (or at least, a send-shutdown), or by disabling nagle (set NoDelay = true) and taking responsibility for not fragmenting too much (BufferedStream can be useful for this)
  • the receiver does not always get entire frames; at any Read/BeginRead/ReceiveAsync/etc all you are guaranteed is "some bytes" or "EOF". As such, you must keep reading until you have a compete frame. The question then, is, how is a frame defined by your protocol? Common approaches are length-prefixed (or length in a header), or a special frame-termination sequence

It is impossible to interpret more without code; however, as noted in another answer - it may help to offload the socket details to a library. As it happens, I'm working on one currently that I intend to release to OSS pretty soon.

Upvotes: 2

Sean
Sean

Reputation: 1822

It's difficult to answer, I strongly recommend you a Socket Framework on .net platform. It will save you lots of time to develop by yourself. SuperSocket

Upvotes: 1

Related Questions