0

I'm writing some embedded Linux software where we have two microprocessors on the same PCB and they communicate with each other using UDP through a switch that is embedded on the PCB. Each application has a hard coded IP address. I want to use non blocking send and receive and I found some example code here client and server

Why does one of them have to be a server? I want to do peer to peer communication between the two microprocessors. Can both of my embedded applications use udpClientNonBlock.c style code or does one have to be a server?

It seems the difference between server and client is that server calls bind(), listens for all incoming IP addresses and doesn't seem to be non-blocking on receive. Neither the server or client know their own IP address and in the sendto message sent by the server, it uses a destination IP address that came from the incoming recvfrom function.

The target code runs on Linux but I want to test it on Windows first. Is it possible to have two applications running on the same PC that talk to each other using UDP ?

In the client code in the example above, a port number is specified as part of the server address and the server listens on that port. In the client code recvfrom function, does the client receive all messages regardless of the port number or only from the port specified as part of the server IP address ?

  • If your using a compatible API, windows will work like linux. As to binding, yes its needed in order to hear UDP messages, on both sides. A -> B requires B has bound to a port (become a server) so it can hear A. similarly A must bind in order for B -> A to work. Think of it like TCP except a TCP binding hears connections, where a UDP binding hears messages. You can also broadcast UDP messages over a network segment, assuming the intermediate hub/switch plays along. – Kain0_0 Mar 25 at 4:05
  • If you look at the code you'll see the difference is that the client sends a message first then receives a reply, and the server receives a message then replies to it. You can send and receive whenever you want, though. – immibis Mar 28 at 21:01
  • Thanks all - yes, I discovered that the client has implicit binding as a result of calling sendto. I plan to make it explicit binding at both ends so that either end can send first and they listen on different ports. – Alpha Mar 29 at 22:33
1

UDP does not require either end to be strictly a server or client. Each endpoint can react to incoming messages and send outgoing messages.

Each endpoint that wants to receive messages uses bind(2) to associate the socket with its hard-coded (local) address [that is why a server binds, so clients can use the address to connect to the server].

The challenge is deciding when and how long to wait for incoming messages, and when to send outgoing messages. Based upon your description, you are going to want each endpoint to bind(), and either use select(2) or fcntl(2) to configure the sockets as non-blocking. You can either use threading and have a reader thread block on incoming messages, or select(2)/poll for messages.

You may end up with an event loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.