Tuesday, July 10, 2007

Can a C code give different outputs on different runs??

At first glance on the topic, your answer immediately would be "NO!!"

Let me clarify my situation to explain the topic better. I am talking about a C code that used network sockets provided by the OS. Many such clones of the program are running simultaneously and these interact among themselves using the BSD sockets.
I really got expected execution sometimes by just repeating the whole process of execution just one more time for confirmation... And what happens, it executes properly!!
With all these experiences of mine, I thought about jotting down some common factors that may affect socket programming..
Pointers for socket programming:
  • Never give up with just one wrong execution of the program. Try at least 2 times.
  • Carefully choose your buffer sizes. No error is flagged and this is a very easy way of losing data while transmitting or receiving.
  • DON'T flood the network with packets continuously with packets from different sources to different destinations. It just does not work!! Give some breathing time for it to transmit the packets successfully.
So... Go on and give your best network code in C :)

2 comments:

Anonymous said...

I am not sure if the issue is so bad. Isn't it possible to be able to check out what is happening when you send messages across ports? Error checking must be possible. Either in the form of assert() statements or by using certain libraries.

Karthik Swaminathan said...

Oh... I am really sorry. I forgot to mention that I was talking mainly about UDP. Though its name says that packet loss is possible, I have never lost even one packet in the process..
And while sending through UDP, you just execute the sendto call and it executes successfully and goes to the transport layer below. You do not get a feedback of what happened to it.!