chrysn / aiocoap

The Python CoAP library
Other
267 stars 120 forks source link

How to use port 5683 for both client and server to send and receive device messages #328

Closed wh1982 closed 1 year ago

wh1982 commented 1 year ago

How to use port 5683 for both client and server to send and receive device messages, as the device is connected to the internal network through NAT~~~

chrysn commented 1 year ago

aiocoap by default (on platforms where it can be supported well, i.e. on Linux) runs server and client on port 5683, provided you only create a single context -- a context created through create_server_context can be used both for client and for server side.

NAT traversal is something that is generally finicky, especially if you are not in control of the NAT (and if you were, you'd hopefully deploy IPv6 instead of running a NAT). Using a single server context will ensure that there is a consistent port used, but you'd have to find out depending on the precise implementation of NAT how often you have to send requests just to keep the NAT alive, or whether any peers other than the one you sent the first outgoing request to will be able to connect to you. Moreover, the NAT itself may change your local port.

If you are behind a NAT and can not replace it with modern technology, the recommended usage pattern is to establish an outgoing TCP connection ("coap+tcp://your-server"). That, too, can serve role reversed requests, and will not suffer that much from the effects of the NAT.

wh1982 commented 1 year ago

Thank you very much for resolving my doubts.