?
Solved

creating and  sending and recieving UDP packets

Posted on 2005-04-08
11
Medium Priority
?
388 Views
Last Modified: 2010-04-15
Hello all,

I am having real trouble writing a basic client/server program in C that passes UDP packets and wonder if someone could please help?

I fully understand how to get the client to send a string to the listening server (this part already works) but my question is how can I impliment the following?

   I need to make and send with each packet a udp packet header (struct) that holds a sequence number and a flag to mark the start and end.

   I need to delcare a packet payload size (say 10 bytes) and split whatever file is passed, into packets of this size and send them to the server.

   I need to display the number of packets passed to server (on the server) and check for out of sequence packets

The connection need only be one way. I guess this is like a simulation of  tcp (without acknowledgement). Unfortuantly I can not use TCP.

I hope someone is an experienced socket programmer or alike!!

This is killing me :(

Thanks

Ren
0
Comment
Question by:Beam
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 13734433
This might be from one of your fellow students:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21378237.html

We cannot do your coursework for you. Please make a reasonable effort and post your code. We can then help much more.

Paul
0
 

Author Comment

by:Beam
ID: 13734635
It might well be?

But the basis of my question is very different.

I am sorry if it came across like the other question (please write me client/ server software from the start!!) that is certainly not the not the case here. I like the bit when he/she siad 'I had function like that but called a different name!!' hmmmm!

Basically I fully understand binding to the socket and sending data and then recieving it on the server end. I can write a packet header (just a simple struct), but the problem I am having is spliting the data passed in. I passed in the localhost (agrc) and the data agrv[2].  (agrv[] is char array)

Now I need to split that into to payloads (say 10 bytes) and send them to the server.

So I want to :

take the 10 bytes at a time from agrv[2] and send them. When I get to the last packet I want to send what ever size that is.

The rest of the question is not such a problem.

If you want any code samples I can post :)

Sorry for starting on the wrong foot :)

Ren





0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13734702
Ren,

Take a look at my first response to Phil. I am doing almost exactly what you are asking, even, by coincidence, taking 10 bytes at a time.

Paul
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13735160

Hi Ren,

Whenever someone posts a question about writing a client/server application using UDP I instinctively ask "Why?"

UDP is kind of a special purpose protocol that is not applicable for most applications.  If you need to know that the data arrived at the other end or that the packets arrived in the order sent (i.e. your data is intact) then you'll need to use TCP.  Most of the programming and interface is the same, but there are few subtle differences.


Kent
0
 
LVL 22

Expert Comment

by:grg99
ID: 13737214
It's your typical make-work kind of assignment.  I hope the teacher also mentions that "see how hard it is to do this?  Plus , your code probably performs poorly on a congested network.  Use TCP, it handles all that hard stuff"

Basically what the poor guy has to do is define a struct:  { int BytesInThisPacket; int SequenceNumber; char Body[10000] };

Then move the data into Body, set the fields, do a udpwrite of the correct length, then do the opposite on the other end.


0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13737714

Hi grg99,

Yeah.  We see a lot of these kinds of questions.  It's disappointing how few of them even understand the difference between UDP and TCP.


Kent
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13744141
Hi Kdo,

Yes, you're right. It's also annoying to see the very same question again and again (like the currency converter).

Stefan
0
 

Author Comment

by:Beam
ID: 13748771
Ok, firstly sorry to bore you Stefan with the 'same question'.

Secondly, In the future Kent you should read the questions a little more carefully. I fully understand the difference between the unreliable UDP and the reliable( but with higher overhead) TCP procotol. I fully understand where they fit into to the OSI layers. I understand issues such as lack of acknowledge and in what situation they should be used etc etc etc.

Maybe Kent should think about why someone would want to try and simulate a  reliable(ish!!) UDP packet instead of just suggesting the 'soft options'. People who ask questions often have a reason after all!!!!! In this case it was for acdemic reasons, to fully understand the how to pass datagrams and construct packets of this type.

Thanks Paul for the push in the right direction. I have now constructed my client/server programs and met the specification.

:) Ren
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13748828
Hi Ren!

Sorry - it's just that this question is a déjà vu...
Sure, there are uses for UDP. They're rare, most people just need a data stream. And UDP packages are not guaranteed to arrive in the same order as you sent them. UDP is helpful when you have a stateless protocol (isn't that a contradiction in itself?).

For example, you could have a machine which is a sophisticated random generator. It could send its output by UDP, as it doesn't matter in which order the packages arrive.

Stefan
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 13751168
Kent/Stefan,

I totally agree with you. It deeply saddens me to meet people who cannot think beyond that which they already know and therefore end up repeating the same mistakes. The deepest core of this problem, I believe, is our current inability to encode knowledge in a truly portable and available fashion. In the age of the computer and fabulous graphics systems that can generate incredibly realistic moving images at a cost within the price range of a child, how come we still code in text?

Ren,

I appreciate the thanks.

A word, however, might be appropriate here. Kent is an intelligent, wise and deeply knowledgable person who has helped many on this board. I think you may have misinterpreted his words. It may be worthwhile re-reading his comments in this light to be sure that he deserved the remarks you made.

Paul
0
 

Author Comment

by:Beam
ID: 13751283
I guess you are right Paul, I meant no offence to Kent. It is just do not like it when people pigeon hole you. I am sure most of the people who post this sort of question understand the difference between UDP and TCP, its only takes a  basic level of intelligence!!

What they dont understand maybe (like me in this case) is the c syntax!!! and this is where you guys come in :)

Sorry to stir up any bad feeling.

Take it easy (including Kent) and thanks again.

Ren :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

830 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