[Libwebsockets] Got "WSI_STATE_DEAD_SOCKET" when try to connect"echo.websocket.org"

"Andy Green (林安廸)" andy at warmcat.com
Thu Jan 17 08:03:43 CET 2013


On 17/01/13 13:47, the mail apparently from Peter Young(杨世玲) included:
> No, I didn't use ./configure.
>
> I got an error when I use the `make` command.
> I run these command in Terminal in order.
> ./autogen.sh //no error here
> ./configure //no error here
> make
>
> bogon:libwebsockets young40$ make
> make  all-recursive
> Making all in lib
> /bin/sh ../libtool  --tag=CC   --mode=compile gcc -DHAVE_CONFIG_H -I. -I..    -Wall -std=gnu99 -pedantic  -rdynamic -fPIC -Werror -c -DINSTALL_DATADIR=\"/usr/local/share\" -DLWS_OPENSSL_CLIENT_CERTS=\"/etc/pki/tls/certs/\" -g -O2 -D_DEBUG -MT libwebsockets_la-libwebsockets.lo -MD -MP -MF .deps/libwebsockets_la-libwebsockets.Tpo -c -o libwebsockets_la-libwebsockets.lo `test -f 'libwebsockets.c' || echo './'`libwebsockets.c
> libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I.. -Wall -std=gnu99 -pedantic -rdynamic -fPIC -Werror -c -DINSTALL_DATADIR=\"/usr/local/share\" -DLWS_OPENSSL_CLIENT_CERTS=\"/etc/pki/tls/certs/\" -g -O2 -D_DEBUG -MT libwebsockets_la-libwebsockets.lo -MD -MP -MF .deps/libwebsockets_la-libwebsockets.Tpo -c libwebsockets.c  -fno-common -DPIC -o .libs/libwebsockets_la-libwebsockets.o
> cc1: warnings being treated as errors
> libwebsockets.c: In function '_lws_log':
> libwebsockets.c:2511: warning: format '%04ld' expects type 'long int', but argument 6 has type 'int'

I see... it's a portability bug on my part, I just pushed this which 
should solve it

http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=eff73742bc215f78e4b30ea180bbeb71c11ec33b

> make[2]: *** [libwebsockets_la-libwebsockets.lo] Error 1
> make[1]: *** [all-recursive] Error 1
> make: *** [all] Error 2
>
> So, I added all codes(*.c, *.h) to Xcode, and it works.
> I'm a newbie to c/c++.

You're coping well!

-Andy

> -Peter
>
>
> ------------------ Original ------------------
> From:  ""Andy Green (林安廸)""<andy at warmcat.com>;
> Date:  Thu, Jan 17, 2013 01:24 PM
> To:  ""Peter Young(杨世玲)""<young40 at qq.com>;
> Cc:  "libwebsockets"<libwebsockets at ml.libwebsockets.org>;
> Subject:  Re: [Libwebsockets] Got "WSI_STATE_DEAD_SOCKET" when try to connect"echo.websocket.org"
>
> On 17/01/13 12:59, the mail apparently from Peter Young(杨世玲) included:
>
> Hi -
>
>> I got lws_set_log_level() works now.
>>
>> I just add a new `Preprocessor Macros`, `_DEBUG=1` in Xcode.
>
> I see, not having that is the same as --disable-debug.
>
> However the autotools / configure stuff should have provided it, the
> code is there in configure.ac.
>
> Is it that you didn't use ./configure?
>
> -Andy
>
>> ^_^.
>>
>> Best Regards.
>>
>> -Peter
>>
>>
>> ------------------ Original ------------------
>> From:  ""Andy Green (林安廸)""<andy at warmcat.com>;
>> Date:  Thu, Jan 17, 2013 10:21 AM
>> To:  ""Peter Young(杨世玲)""<young40 at qq.com>;
>> Cc:  "libwebsockets"<libwebsockets at ml.libwebsockets.org>;
>> Subject:  Re: [Libwebsockets] Got "WSI_STATE_DEAD_SOCKET" when try to connect"echo.websocket.org"
>>
>> On 17/01/13 02:26, the mail apparently from Peter Young(杨世玲) included:
>>> Thank you Andy, Great! It works!!!
>>>
>>> BTW, there are some small problem I met.
>>>
>>> I'm using Xcode(4.5.2) in OS X(10.8.2). using the lastest libwebsockets from the git.
>>
>> Thanks a lot for the feedback, for platforms other than Linux / gcc /
>> glibc I am reliant on hearing what's busted and get advice how to
>> workaround it.  Over time it has been used on many platforms including
>> OSX so it's generally close to working already, but the new work being
>> done to add to it only gets tested for sure on Linux / gcc / glibc and
>> so continually adds new litte dings on the other platforms.
>>
>>> 1)
>>> in file private-libwebsockets.h
>>> /* some Linux toolchains don't have this from the above, some do */
>>> #ifndef PATH_MAX
>>> #include <linux/limits.h> // error here
>>> #endif
>>
>> Yes let's just do what you're doing, everything has limits.h
>>
>> http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=26757a75bc6521bde81e7ee8c374eb85c51f65bd
>>
>>> I changed it to:
>>> #include <limits.h> //works for me.
>>>
>>> 2)
>>> in file libwebsockets.h
>>> #include <assert.h> //I need to insert this code, or there will be an error
>>
>> Okay, it's workable on gcc / Linux without it, but obviously it's legit
>>
>> http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=27b3237392e6b8dfc412d70132114e9091180fbf
>>
>>> 3)
>>> I add these code in main(), but no debug info from the libwebsockets. (only my own debug info).
>>> int main(int argc, const char * argv[])
>>> {
>>>        lws_set_log_level(511, NULL);
>>>        ........
>>>
>>> outputs:
>>> client established
>>> loop next 0
>>> send msg [test msg 1], 0
>>> rx 12 '[test msg 1]'
>>> loop next 0
>>> send msg [test msg 2], 0
>>> rx 12 '[test msg 2]'
>>> loop next 0
>>>
>>> I'm using stderr for output like this:
>>> fprintf(stderr, "send msg %s, %d\n", tbuf2, iret);
>>>
>>> did I call lws_set_log_level() in wrong?
>>
>> No it's correct alright.  In the library by default, it's also doing
>> fprintf(stderr
>>
>> static void lwsl_emit_stderr(const char *line)
>> {
>> fprintf(stderr, "%s", line);
>> }
>>
>> Is it possible you configured the library with --disable-debug?  It
>> doesn't seem likely but it would give this effect.
>>
>> -Andy
>>
>>> Thank you, again.
>>>
>>> -Peter
>>>
>>> ------------------ Original ------------------
>>> From:  ""Andy Green (林安廸)""<andy at warmcat.com>;
>>> Date:  Jan 17, 2013
>>> To:  "杨世玲"<young40 at qq.com>;
>>> Cc:  "libwebsockets"<libwebsockets at ml.libwebsockets.org>;
>>> Subject:  Re: [Libwebsockets] Got "WSI_STATE_DEAD_SOCKET" when try to connect"echo.websocket.org"
>>>
>>> On 17/01/13 00:40, the mail apparently from "Andy Green (林安廸)" included:
>>>> On 17/01/13 00:35, the mail apparently from "Andy Green (林安廸)" included:
>>>>> On 16/01/13 23:19, the mail apparently from 杨世玲 included:
>>>>>> Hello everyone,
>>>>>>
>>>>>> I'm try to write a client of websocket.
>>>>>> There is a web demo from websocket.org,
>>>>>> http://www.websocket.org/echo.html.
>>>>>> It using "ws://echo.websocket.org/"
>>>>>>
>>>>>> but the instance of libwebsocket has a state "WSI_STATE_DEAD_SOCKET"
>>>>>> when call libwebsocket_write.
>>>>>>
>>>>>> did I miss something important? thank you very much.
>>>>>
>>>>> If you add this -->
>>>>>
>>>>> lws_set_log_level(511, NULL);
>>>>>
>>>>> you'll get maximum debug from the library coming on stderr
>>>>>
>>>>> The problem is it doesn't like the headers coming back from that
>>>>> server...
>>>>>
>>>>> [1358352836:7753] PARSER: libwebsocket_client_handshake missing required
>>>>> header(s)
>>>>>
>>>>> ... and that's because until now libwebsockets insists to select a
>>>>> protocol.
>>>>>
>>>>> Here is a small patch on libwebsockets that gets you past that, I'll do
>>>>> it properly tomorrow
>>>
>>> I cleaned the patch and pushed it to git
>>>
>>> http://git.libwebsockets.org/cgi-bin/cgit/libwebsockets/commit/?id=d32bb9128b8742b386ff070d7c24253899f27312
>>>
>>> so you can just update the library from git to solve that part.
>>>
>>> -Andy
>>>
>>>>> diff --git a/lib/client.c b/lib/client.c
>>>>> index 027fb77..473b144 100644
>>>>> --- a/lib/client.c
>>>>> +++ b/lib/client.c
>>>>> @@ -341,9 +341,10 @@ lws_client_interpret_server_handshake(struct
>>>>> libwebsocket_context *context,
>>>>>                     if (!wsi->utf8_token[WSI_TOKEN_HTTP].token_len ||
>>>>>                             !wsi->utf8_token[WSI_TOKEN_UPGRADE].token_len ||
>>>>>
>>>>> !wsi->utf8_token[WSI_TOKEN_CHALLENGE].token_len ||
>>>>> -
>>>>> !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
>>>>> -
>>>>> (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
>>>>> -                       wsi->c_protocol != NULL)) {
>>>>> +                       !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len
>>>>> // ||
>>>>> +//
>>>>> (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
>>>>> +//                     wsi->c_protocol != NULL)
>>>>> +                       ) {
>>>>>                             lwsl_parser("libwebsocket_client_handshake "
>>>>>                                             "missing required header(s)\n");
>>>>>                             pkt[len] = '\0';
>>>>> @@ -396,11 +397,12 @@ lws_client_interpret_server_handshake(struct
>>>>> libwebsocket_context *context,
>>>>>                 !wsi->utf8_token[WSI_TOKEN_CONNECTION].token_len ||
>>>>>                 !wsi->utf8_token[WSI_TOKEN_ACCEPT].token_len ||
>>>>>                 (!wsi->utf8_token[WSI_TOKEN_NONCE].token_len &&
>>>>> -                                  wsi->ietf_spec_revision == 4) ||
>>>>> -           (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
>>>>> -                                                   wsi->c_protocol !=
>>>>> NULL)) {
>>>>> +                                  wsi->ietf_spec_revision == 4) //||
>>>>> +//         (!wsi->utf8_token[WSI_TOKEN_PROTOCOL].token_len &&
>>>>> +//                                                 wsi->c_protocol !=
>>>>> NULL)
>>>>> +) {
>>>>>                     lwsl_parser("libwebsocket_client_handshake "
>>>>> -                                       "missing required header(s)\n");
>>>>> +                                       "missing required header(s)
>>>>> revision=%d\n", wsi->ietf_spec_revision);
>>>>>                     pkt[len] = '\0';
>>>>>                     lwsl_parser("%s", pkt);
>>>>>                     goto bail3;
>>>>>
>>>>>
>>>>> Your code is able to loop and send then, but there are some problems.
>>>>>
>>>>>      - you can't print the buffer you sent after sending it.  It's modified
>>>>> in place and destroyed by the send action.  You can print the copy in
>>>>> tbuf2 instead
>>>>>
>>>>>      - it's ok for initial test but sending should be done using the
>>>>> LWS_CALLBACK_CLIENT_WRITEABLE callback and
>>>>> libwebsocket_callback_on_writable(this, wsi); to guarantee it can't block
>>>>>
>>>>>      - since no protocol is defined it will use protocol 0 for callbacks
>>>>>
>>>>>      - I didn't see any rx coming, I'll look further into it.
>>>>
>>>> Oh I see the problem immediately now I look
>>>>
>>>> You need the callback
>>>>
>>>> case LWS_CALLBACK_CLIENT_RECEIVE:
>>>>
>>>> LWS_CALLBACK_RECEIVE is for server rx.
>>>>
>>>> With that changed (and the library patch to allow no protocol...) you
>>>> are working ^^
>>>>
>>>> send msg [test msg 45], 0
>>>> rx 13 '[test msg 44]'
>>>> loop next 0
>>>> [1358354296:9755] PARSER: written 19 bytes to client
>>>> send msg [test msg 46], 0
>>>> rx 13 '[test msg 45]'
>>>> loop next 0
>>>> [1358354297:4861] PARSER: written 19 bytes to client
>>>> send msg [test msg 47], 0
>>>> rx 13 '[test msg 46]'
>>>> loop next 0
>>>> [1358354297:4862] PARSER: written 19 bytes to client
>>>> send msg [test msg 48], 0
>>>> rx 13 '[test msg 47]'
>>>> loop next 0
>>>> [1358354298:0000] PARSER: written 19 bytes to client
>>>> send msg [test msg 49], 0
>>>> rx 13 '[test msg 48]'
>>>> loop next 0
>>>> [1358354298:0001] PARSER: written 19 bytes to client
>>>> send msg [test msg 50], 0
>>>> ...
>>>>
>>>> -Andy
>>>>
>>>>> -Andy
>>>>>
>>>>>> so here is my code in C++ using libwebsockets.
>>>>>>
>>>>>> #include <iostream>
>>>>>>
>>>>>> #include <libwebsockets.h>
>>>>>>
>>>>>> static int
>>>>>>
>>>>>> callback_socketio(struct libwebsocket_context *_this,
>>>>>>
>>>>>>                       struct libwebsocket *wsi,
>>>>>>
>>>>>>                       enum libwebsocket_callback_reasons reason,
>>>>>>
>>>>>>                       void *user, void *in, size_t len)
>>>>>>
>>>>>> {
>>>>>>
>>>>>>         switch (reason) {
>>>>>>
>>>>>>             case LWS_CALLBACK_CLOSED:
>>>>>>
>>>>>>                 fprintf(stderr, "closed \n");
>>>>>>
>>>>>>                 break;
>>>>>>
>>>>>>
>>>>>>
>>>>>>             case LWS_CALLBACK_CLIENT_ESTABLISHED:
>>>>>>
>>>>>>                 libwebsocket_callback_on_writable(_this, wsi);
>>>>>>
>>>>>>                 fprintf(stderr, "client established\n");
>>>>>>
>>>>>>                 break;
>>>>>>
>>>>>>             case LWS_CALLBACK_RECEIVE:
>>>>>>
>>>>>>                 fprintf(stderr, "rx %d '%s' \n", (int)len, (char *)in);
>>>>>>
>>>>>>                 break;
>>>>>>
>>>>>>             default:
>>>>>>
>>>>>>                 break;
>>>>>>
>>>>>>         }
>>>>>>
>>>>>>
>>>>>>
>>>>>>         return 0;
>>>>>>
>>>>>> }
>>>>>>
>>>>>>
>>>>>>
>>>>>> static struct libwebsocket_protocols protocols[] = {
>>>>>>
>>>>>>         {
>>>>>>
>>>>>>             "socketio-protocol",
>>>>>>
>>>>>>             callback_socketio,
>>>>>>
>>>>>>             0,
>>>>>>
>>>>>>         },
>>>>>>
>>>>>>         {
>>>>>>
>>>>>>             NULL,
>>>>>>
>>>>>>             NULL,
>>>>>>
>>>>>>             0,
>>>>>>
>>>>>>         }
>>>>>>
>>>>>> };
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> int main(int argc, const char * argv[])
>>>>>>
>>>>>> {
>>>>>>
>>>>>>         struct libwebsocket_context *context;
>>>>>>
>>>>>>         struct libwebsocket *wsi_socketio;
>>>>>>
>>>>>>         int port = 80;
>>>>>>
>>>>>>         int use_ssl = 0;
>>>>>>
>>>>>>         const char *address = "echo.websocket.org";
>>>>>>
>>>>>>         //const char *address = "127.0.0.1";
>>>>>>
>>>>>>
>>>>>>
>>>>>>         int iret = 0;
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>         context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
>>>>>>
>>>>>>                                               protocols,
>>>>>>
>>>>>>
>>>>>> libwebsocket_internal_extensions,
>>>>>>
>>>>>>                                               NULL, NULL, NULL,
>>>>>>
>>>>>>                                               -1, -1, 0, NULL);
>>>>>>
>>>>>>
>>>>>>
>>>>>>         if (context == NULL)
>>>>>>
>>>>>>         {
>>>>>>
>>>>>>             fprintf(stderr, "Create context error\n");
>>>>>>
>>>>>>             return 1;
>>>>>>
>>>>>>         }
>>>>>>
>>>>>>
>>>>>>
>>>>>>         wsi_socketio = libwebsocket_client_connect(context, address,
>>>>>> port, use_ssl, "/", address, address, protocols[0].name, -1);
>>>>>>
>>>>>>
>>>>>>
>>>>>>         if (wsi_socketio == NULL)
>>>>>>
>>>>>>         {
>>>>>>
>>>>>>             fprintf(stderr, "connect error");
>>>>>>
>>>>>>
>>>>>>
>>>>>>             return -1;
>>>>>>
>>>>>>         }
>>>>>>
>>>>>>
>>>>>>
>>>>>>         char tbuf[200] = {0};
>>>>>>
>>>>>>         char tbuf2[200] = {0};
>>>>>>
>>>>>>         int index = 0;
>>>>>>
>>>>>>
>>>>>>
>>>>>>         while (true)
>>>>>>
>>>>>>         {
>>>>>>
>>>>>>             iret = libwebsocket_service(context, 2000);
>>>>>>
>>>>>>             fprintf(stderr, "loop next %d \n", iret);
>>>>>>
>>>>>>
>>>>>>
>>>>>>             if (wsi_socketio)
>>>>>>
>>>>>>             {
>>>>>>
>>>>>>                 memset(tbuf, 0, sizeof(tbuf));
>>>>>>
>>>>>>                 memset(tbuf2, 0, sizeof(tbuf2));
>>>>>>
>>>>>>                 snprintf(tbuf2, sizeof(tbuf2), "[test msg %d]", ++index);
>>>>>>
>>>>>>                 strcpy(&tbuf[LWS_SEND_BUFFER_PRE_PADDING], tbuf2);
>>>>>>
>>>>>>                 iret = libwebsocket_write(wsi_socketio,
>>>>>>
>>>>>>                                           (unsigned
>>>>>> char*)&tbuf[LWS_SEND_BUFFER_PRE_PADDING],
>>>>>>
>>>>>>
>>>>>> strlen(tbuf+LWS_SEND_BUFFER_PRE_PADDING),
>>>>>>
>>>>>>                                           LWS_WRITE_TEXT);
>>>>>>
>>>>>>                 fprintf(stderr, "send msg %s, %d\n",
>>>>>> (char*)(&tbuf[LWS_SEND_BUFFER_PRE_PADDING]), iret);
>>>>>>
>>>>>>            }
>>>>>>
>>>>>>         }
>>>>>>
>>>>>>         libwebsocket_close_and_free_session(context, wsi_socketio,
>>>>>> LWS_CLOSE_STATUS_NORMAL);
>>>>>>
>>>>>>         libwebsocket_context_destroy(context);
>>>>>>
>>>>>>         // insert code here...
>>>>>>
>>>>>>         std::cout << "Hello, World!\n";
>>>>>>
>>>>>>         return 0;
>>>>>>
>>>>>> }
>>>>>> _______________________________________________
>>>>>> Libwebsockets mailing list
>>>>>> Libwebsockets at ml.libwebsockets.org
>>>>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Libwebsockets mailing list
>>>>> Libwebsockets at ml.libwebsockets.org
>>>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>>>
>>>> _______________________________________________
>>>> Libwebsockets mailing list
>>>> Libwebsockets at ml.libwebsockets.org
>>>> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
>>>
>>> .
>>>
>>
>> .
>>
>
> .
>




More information about the Libwebsockets mailing list