UDP server

I'm wondering if anyone knows how to create a c++ program to create a UDP server? I've used C# which is easier to use, especially in this instance but I'm stuck on how to start.
both windows and unix have a socket library, and they are almost identical. I think you can literally make a simple program in 5 or 6 lines... gethostbyname(), set socket options, open socket, sendto(..) and I think(?) recievefrom() functions. I don't have a code sample on me but that is the starting point. I don't know how it could be more simple than this, but Ill take your word on it.

You also need to "serialize" your data into a byte stream, there are multiple ways to do this from extracting the char array from a string object to stringstreams and various similar approaches.

I advise that for any complex objects you have, write a method that writes and reads that object to and from a disk file. Re-use this code to serialize it for transmit once you have debugged it.

The same code works for both UDP and TCPIP with just a slight change to the socket options settings. You also need a listener thread, to check for data every so often, so your project should probably be multi-threaded if not already.

Remember this is just a starting point.
Last edited on
Topic archived. No new replies allowed.