LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   UDP light bulb - questions (https://www.linuxquestions.org/questions/programming-9/udp-light-bulb-questions-826188/)

scheme 08-14-2010 07:48 AM

UDP light bulb - questions
 
Awhile ago I wanted to make a small program in C, but I scrapped it after a few tries and ugly/inefficient code I didn't myself understand anymore. For the record, I've only just started programming.

There was supposed to be a light bulb icon on two computers, when pressed it'd send a short UDP package to the other computer (ip address given from command line) which would light the light bulb on both ends.

I managed to make the network code (well they were server/client snippets copy-pasted from Berkeley Sockets wikipedia article) and SDL code work separately locally (I chose SDL because I was going to learn it later anyway) but I never managed to put them together. Most importantly I couldn't make the server/client work in the same program. After the intro, here's a few questions:

For above do I have to use two different sockets to handle the sending and receiving?
Can it be done without threading or would I have to put separate parts on threads or would it just make more sense that way (considering the server should be listening all the time to minimize the already possible packet losses from using UDP)?

Also if you have anything to say about the project idea, just go ahead.

paulsm4 08-15-2010 01:11 AM

Hi -

Q: For above do I have to use two different sockets to handle the sending and receiving?

A: No, absolutely not. One socket can do both "sendto()" and "recvfrom()".

Q: Can it be done without threading?

A: Certainly

Q: Considering the server should be listening all the time to minimize the already possible packet losses from using UDP?

A: In this scenario, it doesn't sound like "packet loss" would really be an issue. If it were, the easiest solution is to implement a simple "ACK/Retry" protocol". Or switch from UDP datagrams to TCP connections.

Strong suggestion:
Read Beej's Guide to Network Programming:
http://beej.us/guide/bgnet/

'Hope that helps .. PSM

scheme 08-16-2010 11:46 AM

Thank you for the clear answers!

I wanted to use UDP for the (seemingly) easy implementation and low bandwith (this app is rather trivial afterall).
I'll read through the guide, I've stumbled into it when searching if I should use SDL_network or plain Berkeley Sockets. I find I don't know anything about programming networks. Time to start experimenting and reading more.


All times are GMT -5. The time now is 01:28 AM.