[Libwebsockets] client always receive strange response from server

hanguofu hanguofu at 163.com
Mon Oct 31 17:16:56 CET 2016


>On Mon, 2016-10-31 at 01:04 +0800, hanguofu wrote:
>> I played with the latest libwebsockets-test-client  and
>> libwebsockets-test-server examples at local host environment .
>> But the client always receive empty response from server    at "case
>> LWS_CALLBACK_CLIENT_RECEIVE"   :
>> 
>> rx 0 ''
>> 
>> 
>> After the client sends sth. to the server , the client received a lot
>> of response too :
>> rx 8 'lient 1 '
>> 
>> What are they on earth ?
>
>Dunno, but they do not sound like the actions of the test server or
>client... are you connecting to the port you think you are?  For
>example in the whole of lws, only the ping client has the text "lient
>%" in it.
>
>$ git grep lient\ %
>test-server/test-ping.c:                        lwsl_err("client %d
>failed to connect\n", n);
>
>For the test server, in the simplest case you can run it like this
>
>$ libwebsockets-test-server
>
>and visit http://localhost:7681 to see HTML + websocket demo.
>
>$ libebsockets-test-client
>
>will spam the test server with mirror protocol data, drawing a lot of
>circles quickly in the canvas.
>
>You're doing something different when testing?  What?  And what is your
>OS?
>
>-Andy
>
>> 
>> Regards !
>> 
>> 
>> Guofu Han
>> _______________________________________________
>> Libwebsockets mailing list
>> Libwebsockets at ml.libwebsockets.org
>> http://libwebsockets.org/mailman/listinfo/libwebsockets



Hi , Andy : 

I made some modification on test-server-dumb-increment.c so that it can pass messages between clients.
There are 2 clients connected with a ws server , which passes messages between them . At first , I 
overlooked the test-server.c , which calls lws_callback_on_writable_all_protocol periodically.
After I deleted the code , most of the empty replies has gone .But there is still  one left :

(Please refer to the following log files from the ws server and a client . )
The test involves a ws server and the client ( only one )  . After the client connected to the server successfully ,
I found out the server sent an empty message to the client somehow , which should not happen. 
The modification ( please refer to the following test-server-dumb-increment.c ) will only send message to 
the other client only when the server receives the message from its counterpart.

I have no idea on if there is any other codes call lws_callback_on_writable of dumb-increment-protocol .
Currently there is only
one  lws_callback_on_writable statement in test-server-dumb-increment.c as below and the periodical call at the 
original test-server.c was deleted.

Could you please shed some light on it ? Thankyou very much !



Guofu Han

======================= log file from the ws server ==================================
~/libwebsockets/build/bin $ ./libwebsockets-test-server
lwsts[12874]: libwebsockets test server - license LGPL2.1+SLE
lwsts[12874]: (C) Copyright 2010-2016 Andy Green <andy at warmcat.com>
Using resource path "/usr/local/share/libwebsockets-test-server"
lwsts[12874]: Initial logging level 7
lwsts[12874]: Libwebsockets version: 2.1.0 root at CubeCloud-2016102150-v2.0.0-181-gc3b676b
lwsts[12874]: IPV6 not compiled in
lwsts[12874]: libev support not compiled in
lwsts[12874]: libuv support not compiled in
lwsts[12874]:  Threads: 1 each 65536 fds
lwsts[12874]:  mem: platform fd map: 524288 bytes
lwsts[12874]:  Compiled with OpenSSL support
lwsts[12874]:  SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
lwsts[12874]: Creating Vhost 'default' port 7681, 2 protocols, IPv6 off
lwsts[12874]:  Listening on port 7681
lwsts[12874]:  mem: per-conn:          512 bytes + protocol rx buf
lwsts[12874]:  canonical_hostname = CubeCloud-2016102150
lwsts[12874]: lws_protocol_init
    get  = /
    host: = localhost
    connection: = Upgrade
    upgrade: = websocket
    origin: = http://localhost
    sec-websocket-protocol: = dumb-increment-protocol,fake-nonexistant-protocol
    http/1.1  = HTTP/1.1
    pragma: = no-cache
    cache-control: = no-cache
    sec-websocket-key: = DfI8KrOkwpXFhQHeFrKy+g==
    sec-websocket-version: = 13
lwsts[12874]: 0x18eb870 new partial sent 30 from 204 total
lwsts[12874]: INFO : client id 1 is grant to new incomging client 
lwsts[12874]: INFO : call back established 
lwsts[12874]: INFO : send msg  to client 1    <<<<<<<<<<<<<<<<<<<<< here is the problem. Someone calls lws_callback_on_writable at test-server-dumb-increment.c
^Clwsts[12874]: lws_context_destroy
lwsts[12874]: INFO : client 1 is disconnedted ! 
lwsts[12874]: libwebsockets-test-server exited cleanly


=========================  log file of test client ============================
~/libwebsockets/build/bin $  ./libwebsockets-test-client  localhost
[2016/10/31 11:23:34:4912] NOTICE: libwebsockets test client - license LGPL2.1+SLE
[2016/10/31 11:23:34:4913] NOTICE: (C) Copyright 2010-2016 Andy Green <andy at warmcat.com>
[2016/10/31 11:23:34:4913] NOTICE:  SSL disabled
[2016/10/31 11:23:34:4913] NOTICE:  Cert must validate correctly (use -s to allow selfsigned)
[2016/10/31 11:23:34:4913] NOTICE:  Requiring peer cert hostname matches
[2016/10/31 11:23:34:4914] NOTICE: Initial logging level 7
[2016/10/31 11:23:34:4914] NOTICE: Libwebsockets version: 2.1.0 root at CubeCloud-2016102150-v2.0.0-181-gc3b676b
[2016/10/31 11:23:34:4914] NOTICE: IPV6 not compiled in
[2016/10/31 11:23:34:4914] NOTICE: libev support not compiled in
[2016/10/31 11:23:34:4914] NOTICE: libuv support not compiled in
[2016/10/31 11:23:34:4915] NOTICE:  Threads: 1 each 65536 fds
[2016/10/31 11:23:34:4926] NOTICE:  mem: platform fd map: 524288 bytes
[2016/10/31 11:23:34:4926] NOTICE:  Compiled with OpenSSL support
[2016/10/31 11:23:34:4926] NOTICE:  SSL disabled: no LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
[2016/10/31 11:23:34:4926] NOTICE: Creating Vhost 'default' port -1, 1 protocols, IPv6 off
[2016/10/31 11:23:34:4927] NOTICE:  mem: per-conn:          512 bytes + protocol rx buf
[2016/10/31 11:23:34:4927] NOTICE:  canonical_hostname = CubeCloud-2016102150
[2016/10/31 11:23:34:4927] NOTICE: using  mode (ws)
[2016/10/31 11:23:34:4927] NOTICE: dumb: connecting
[2016/10/31 11:23:34:4927] NOTICE: lws_protocol_init
[2016/10/31 11:23:34:4987] ERR: dumb: LWS_CALLBACK_CLIENT_ESTABLISHED
[2016/10/31 11:23:34:4988] ERR: rx 0 ''     <<<<<<<<<<<<<<<<<<<<<<<<<<<<< the client receives an empty message from server
^C[2016/10/31 11:23:45:4172] ERR: Exiting
[2016/10/31 11:23:45:4172] NOTICE: lws_context_destroy
[2016/10/31 11:23:45:4172] NOTICE: dumb: LWS_CALLBACK_CLOSED


// test-server-dumb-increment.c
  case LWS_CALLBACK_SERVER_WRITEABLE:
                n= strlen (pss->msg_to_other ) ; 

                strncpy ( p , pss->msg_to_other , n ) ;
                m = lws_write(wsi, (unsigned char *)p, n, LWS_WRITE_TEXT);
                if (m < n) {
                        lwsl_err("ERROR : failed to write string %s to the other! \n", pss->msg_to_other );
                        return -1;
                }
                else
                    lwsl_err("INFO : send msg %s to client %d \n", pss->msg_to_other , pss->client_id ) ;
                
                break;

 case LWS_CALLBACK_RECEIVE:
                if (len < 6)
                        break;
                if (strcmp((const char *)in, "reset\n") == 0)
                        pss->number = 0;
                if (strcmp((const char *)in, "closeme\n") == 0) {
                        lwsl_notice("dumb_inc: closing as requested\n");
                        lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
                                         (unsigned char *)"seeya", 5);
                        return -1;
                }

                lwsl_err("INFO : receive msg %s wth len %d \n",  in , len );
                

                // otherwise , pass the msg to the other client
                //for (int  ii = 0 ; ii < 2 ; ii= ii + 1 )
                for ( ii = 0 ; ii < 2 ; ii= ii + 1 )
                {
                        if ( client_list[ii].client_id == pss->client_id )
                                continue ;

                        // this is the dest. client 
                        memset( client_list[ii].p_psd_dumb->msg_to_other , 0 , 1024 ) ;
                        strncpy (client_list[ii].p_psd_dumb->msg_to_other ,
                                in , len );

                        lwsl_err("INFO : send msg \n");
 
                        lwsl_err("INFO : received msg %s from  client %d\n", client_list[ii].p_psd_dumb->msg_to_other , 
                                         pss->client_id );
                        lws_callback_on_writable( client_list[ii].p_wsi);
                }
                break;


///////////////////////////////////////////////////////
// test-server.c  now only has 2 protocols :
///////////////////////////////////////////////////////
/* list of supported protocols and callbacks */

static struct lws_protocols protocols[] = {
        /* first protocol must always be HTTP handler */

        {
                "http-only",            /* name */
                callback_http,          /* callback */
                sizeof (struct per_session_data__http), /* per_session_data_size */
                0,                      /* max frame size / rx buffer */
        },
        {
                "dumb-increment-protocol",
                callback_dumb_increment,
                sizeof(struct per_session_data__dumb_increment),
                10, /* rx buf size must be >= permessage-deflate rx size */
        },
        { NULL, NULL, 0, 0 } /* terminator */
};
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20161101/0bfb7caa/attachment-0001.html>


More information about the Libwebsockets mailing list