Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket.c example: curl_ws_recv returns "Socket not ready for send/recv" (errno 81) #13288

Open
untitaker opened this issue Apr 4, 2024 · 1 comment · May be fixed by #14214
Open

websocket.c example: curl_ws_recv returns "Socket not ready for send/recv" (errno 81) #13288

untitaker opened this issue Apr 4, 2024 · 1 comment · May be fixed by #14214
Assignees

Comments

@untitaker
Copy link

untitaker commented Apr 4, 2024

I did this

  1. copied this example into foo.c
  2. edited the websocket URL in foo.c to be a valid websocket server. wss://ws.postman-echo.com/raw works, ws://localhost:8080 works as well, with websocat being invoked as websocat -s 8080
  3. cloned curl master, moved foo.c into root folder
  4. cmake -DENABLE_WEBSOCKETS=on
  5. make
  6. cc -L lib/ foo.c -lcurl
  7. LD_LIBRARY_PATH=lib/ ./a.out

I expected the following

some kind of ping-pong demo, or a hanging connection, depending on which websocket server is being used.

instead the program immediately quits with this after sending the initial HTTP headers:

ws: curl_ws_recv returned 81, received 0

in the real-world application that is built based on that example code, I get (just posting the strerror for searchability):

code 81: "Socket not ready for send/recv"

curl/libcurl version

git sha 376cd67

I thought this was related to #11443, so I checked curl 7 as well. On tag curl-7_88_1 (046209e), foo.c hangs instead, though websocket.c does not exist on that commit at all anyway

operating system

Ubuntu 23.10

@untitaker
Copy link
Author

untitaker commented Apr 4, 2024

Dug around a little, and I suspect that this is intended behavior due to #10625, and it's just the example that needs updating, similar to #10760

though I'm not sure a crude busy-loop is a good example to follow either

@jay jay self-assigned this Jul 1, 2024
jay added a commit to jay/curl that referenced this issue Jul 17, 2024
- Overhaul the recv_any function to block waiting for a complete
  websocket message from the server.

- Have recv_pong and recv_header call recv_any to retrieve a complete
  message, rather than duplicate the efforts in each function.

- Fix sleep() macro for Windows to treat the parameter as seconds
  instead of milliseconds.

- Improve documentation of curl_ws_recv/send to better explain
  CURLE_AGAIN behavior.

- Use echo.websocket.org instead of example.com as the example websocket
  server since it is a server the example actually works on.

recv_any now calls curl_ws_recv repeatedly, waiting until the socket is
readable and a full websocket message is read from the server.

Prior to this change the example did not handle the non-blocking
behavior of curl_ws_recv. When curl_ws_recv returned CURLE_AGAIN the
example would fail with "socket not ready".

Fixes curl#13288
Closes #xxxx
jay added a commit to jay/curl that referenced this issue Jul 17, 2024
- Add function blocking_ws_recv which blocks waiting for a complete
  websocket message from the server.

blocking_ws_recv calls curl_ws_recv repeatedly, waiting until the socket
is readable and a full websocket message is read from the server.

Prior to this change the example failed when curl_ws_recv returned
CURLE_AGAIN.

Fixes curl#13288
Closes #xxxx
@jay jay linked a pull request Jul 17, 2024 that will close this issue
jay added a commit to jay/curl that referenced this issue Jul 19, 2024
- Overhaul the recv_any function to block waiting for a complete
  websocket message from the server.

- Have recv_pong and recv_header call recv_any to retrieve a complete
  message, rather than duplicate the efforts in each function.

- Fix sleep() macro for Windows to treat the parameter as seconds
  instead of milliseconds.

- Improve documentation of curl_ws_recv/send to better explain
  CURLE_AGAIN behavior.

- Use echo.websocket.org instead of example.com as the example websocket
  server since it is a server the example actually works on.

recv_any now calls curl_ws_recv repeatedly, waiting until the socket is
readable and a full websocket message is read from the server.

Prior to this change the example did not handle the non-blocking
behavior of curl_ws_recv. When curl_ws_recv returned CURLE_AGAIN the
example would fail with "socket not ready".

Fixes curl#13288
Closes #xxxx
jay added a commit to jay/curl that referenced this issue Aug 4, 2024
- Overhaul the recv_any function to block waiting for a complete
  websocket message from the server.

- Have recv_pong and recv_header call recv_any to retrieve a complete
  message, rather than duplicate the efforts in each function.

- Fix sleep() macro for Windows to treat the parameter as seconds
  instead of milliseconds.

- Improve documentation of curl_ws_recv/send to better explain
  CURLE_AGAIN behavior.

- Use echo.websocket.org instead of example.com as the example websocket
  server since it is a server the example actually works on.

recv_any now calls curl_ws_recv repeatedly, waiting until the socket is
readable and a full websocket message is read from the server.

Prior to this change the example did not handle the non-blocking
behavior of curl_ws_recv. When curl_ws_recv returned CURLE_AGAIN the
example would fail with "socket not ready".

Fixes curl#13288
Closes #xxxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants