7 years ago.

is there only one UDP socket per thread ?

Hi there,

we have a mbed project where we listen to 3 different UDP sockets. So our main.cpp includes these libraries to get there

  1. include "EthernetInterface.h"
  2. include "NetworkAPI/buffer.hpp"
  3. include "NetworkAPI/ip/address.hpp"
  4. include "NetworkAPI/udp/socket.hpp"

Each socket (since we'd like to receive data) is wrapped inside a thread, using osThreadCreate(...) and everything worked so far.

Now, we are in a situation where socket #3 receives something from a client and needs to reply, so I use the same socket, modify the endpoint (set the port) and send a reply. This works also well.

But our protocol requires, that we frequently notify the client that just talked to us via socket #3 and so we tried to use the same socket we opened and used in socket-thread-#3. It seems that the socket only works, when called from this thread #3 context.

Next we tried to open a second socket #3b and use this one to reply only. Even the socket.open() failed.

Also when I tried to use a global socket, declared as static on the main level, the open() failed.

Because we need to do some real-time things, we use the RtosTimer to execute a routine and we were not able to send anything via the socket #3 from this routine.

It seems to us, that we can have only one socket per thread and only this thread may use it.

Since we do RTOS and multithreading, is there something we need to be aware of ? I know a similar behaviour from java, but I never seen it in C/C++.

Would the correct way, to have one more communication thread, and we need to create a message queue or pipeline or something to feed messages from other threads into this one ?

any comments appreciated thanks for your help, Matthias

1 Answer

6 years, 1 month ago.

Hi Matthias,

Did you come up with an answer to your question? I'm about to deal with a similar scenario.

Regards David G