keith-cullen / FreeCoAP

An implementation of a CoAP client, a CoAP server and a HTTP/CoAP proxy with full DTLS support.
132 stars 38 forks source link

second request can't recv data #19

Closed leon0625 closed 6 years ago

leon0625 commented 6 years ago

I write a server, but I find a problem, when client second request , my server response empty mseeage.

log: Debug : Sent to address 192.168.242.10 and port 50833 Debug : Found existing transaction at index 0 Debug : Received from address 192.168.242.10 and port 50833 Info : Received confirmable request from address 192.168.242.10 and port 50833 Info : Request URI path requires a piggy-backed response to address 192.168.242.10 and port 50833 Info : Responding to address 192.168.242.10 and port 50833

why print "Found existing transaction at index 0"?

keith-cullen commented 6 years ago

The coap_server has one UDP socket that handles all communication. It can be in the process of talking to several clients at the same time. Whenever it receives a CoAP message, it checks the IP address and port number of the client to determine if the server has already communicated with that client. If it recognises the client then it restores state information like timers and DTLS session etc. In the trace above, the server logs the fact that it found an existing transaction at index 0 because the last time the server communicated with that client, it used transaction 0 to store the sate information for that client.

leon0625 commented 6 years ago

I have a new problem when my server callback function takes a long time (eg sleep (120)), then the client will always retransmit and timeout. Is there any way for the server to reply to a separate ACK, wait for the callback function to be processed before sending a response?

keith-cullen commented 6 years ago

Use coap_server_add_sep_resp_uri_path() API to register a request path, e.g. '/separate' that requires a separate response, then the server will receive a request from the client containing the URI path, e.g. '/separate' and send an acknowledgement, the client will wait, the server will send a separate response and the client will send an acknowledgement. There is an example of this in test_coap_server. Also, increase the response timeout in the client by setting COAP_CLIENT_RESP_TIMEOUT_SEC in coap_client.c to a large value, e.g. 180 secs.