[Libwebsockets] libwebsocket_write () returns -1 and client closes down the connection.

Hemant Kumar hkumar at arubanetworks.com
Fri Nov 8 21:18:06 CET 2013


Hi

I am trying to send constant stream of 34 bytes messages in a while true loop :

  case LWS_CALLBACK_CLIENT_WRITEABLE:
                      lwsl_notice("LWS_CALLBACK_CLIENT_WRITEABLE\n");
                     while(1) {
                           l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l], "%s", str);
                           n = libwebsocket_write(client_socket, &buf[LWS_SEND_BUFFER_PRE_PADDING], l, LWS_WRITE_TEXT);
                           lwsl_notice("%d (%d) :tx %d '%s'\n",msg_count++, n, (int)l, (char *)str);

                         if (n < 0)
                             return -1;
                          if (n < l) {
                             lwsl_notice("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
                              //return -1;
                         }
                        //libwebsocket_callback_on_writable(context, client_socket);
                     }


With every run, client sends different number of messages and then stops abruptly
with this :

[1383939454:4213] NOTICE: 207799 (-1) :tx 34 'This is first libwebsocket program'


In that case, the return value of libwebsocket_write is -1 leading to connection closure.

Clearly the socket is not available for write and so it returned -1. Since , it is a non-blocking socket, is client program supposed to take care of this?

If I dont return -1, connection is not closed but it then sends data with intermittent waits. oR i SLEEP waiting for the socket to get available for write again.

Can you please suggest how to solve this issue, so that there is no in between gaps durung message send?

Trace of message send:

[1383941766:8191] NOTICE: 481390 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8191] NOTICE: 481391 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: 481392 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: 481393 (46) :tx 34 'This is first libwebsocket program'
Stop sending from here onwards:--
[1383941766:8192] NOTICE: 481394 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481395 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481396 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481397 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481398 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481399 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481400 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481401 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8192] NOTICE: 481402 (-1) :tx 34 'This is first libwebsocket program'
[1383941766:8192] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
[1383941766:8443] NOTICE: Partial write LWS_CALLBACK_CLIENT_WRITEABLE
Again resume after a while:--
[1383941766:8444] NOTICE: 481425 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8445] NOTICE: 481426 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8446] NOTICE: 481427 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8447] NOTICE: 481428 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8447] NOTICE: 481429 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8448] NOTICE: 481430 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8449] NOTICE: 481431 (46) :tx 34 'This is first libwebsocket program'
[1383941766:8450] NOTICE: 481432 (46) :tx 34 'This is first libwebsocket program'



Thanks
Hemant


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20131108/24d3aa3f/attachment.html>


More information about the Libwebsockets mailing list