Coder Social home page Coder Social logo

Comments (9)

eerimoq avatar eerimoq commented on July 20, 2024

Yes you're right, two threads may not use the same socket simultaneously. The application will crash.

Here are two alternative implementations:

  1. Create two sockets, one for receiving data and one for transmitting data. Bind the receiving socket to the local address, and do not bind the transmitting socket. I have not tested it but I think it will work. Depends on how LwIP handles this use case.
  2. Create two sockets and bind them to different local ports, if that works with your use case.

So the key is to create multiple sockets and somehow make that fit into the application. I think it's hard to allow multiple threads to use the same socket. Maybe it is, but I have to think about it. =)

from simba.

wuwx avatar wuwx commented on July 20, 2024

The client is at the back of the NAT server, which means that I have to use the same listening port, to allow the server to respond to the data back to the client to handle correctly
If the client uses a different socket or is not bound to a fixed address
Can cause the server to respond directly to the data can not be received, so I also feel very difficult

from simba.

eerimoq avatar eerimoq commented on July 20, 2024

Hmm, tricky... How is this normally implemented?

Would it work to have one thread and check if there is input data interleaved with sending packets? socket_size() should return a non-zero value if there is a UDP packet available in the input queue. Unfortunately there is no possibility to configure a socket as non-blocking as of today.

from simba.

wuwx avatar wuwx commented on July 20, 2024

how about direct use udp_sendto method to send the message?
this method from lwip api, and not change any thing about socket ....

from simba.

eerimoq avatar eerimoq commented on July 20, 2024

Is it allowed to call LwIP functions, like allocating a pbuf or sending a udp packet, from all threads in an application? I'm not comfortable doing that from the Simba thread as I do not know how LwIP works. Maybe it is ok?

from simba.

wuwx avatar wuwx commented on July 20, 2024

I tested it, it seems to work, but this practice is not beautiful, It's really not so comfortable
, but it can only be so, or I have to switch to a espconn_sendto function

thanks ~~~~~

from simba.

eerimoq avatar eerimoq commented on July 20, 2024

Another alternative is to put thrd_p on the stack of the sending/receiving thread. That way it will be unique for each thread.

I'll have a look at it later. It should be easy to implement.

from simba.

eerimoq avatar eerimoq commented on July 20, 2024

Okay, I just pushed a fix! Let me know if there is still a problem!

Note: Only one sender and one reader may be using the socket at the same time. Two or more senders and/or receivers are not allowed.

Implemented in: ff0df96

from simba.

wuwx avatar wuwx commented on July 20, 2024

yes, it's working now
nice ~~~, thanks 💯

from simba.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.