[Libwebsockets] Handle leak with reconnecting client socket

Ivajlo Donev igdonev at gmail.com
Tue Jul 8 17:39:56 CEST 2014

Hello everyone,

I'm trying to create a self reconnecting client socket using libwebsockets
in Windows. I have the following code as my main loop :

struct libwebsocket_context *context;

user_context_t *uc = (user_context_t *)calloc(1, sizeof(user_context_t));
uc->address = argv[1];
uc->port = atoi(argv[2]);
uc->path = argv[3];
	        	struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
info.protocols = protocols;#ifndef LWS_NO_EXTENSIONS
	info.extensions = libwebsocket_get_internal_extensions();#endif
info.gid = -1;
info.uid = -1;

if (!(context = libwebsocket_create_context(&info)))

   fprintf(stderr, "Failed to create libwebsockets context\n");
   return -1;

while (keepRunning)

{  if (!uc->wsi)
     printf("Connecting to %s:%d%s\n", uc->address, uc->port, uc->path);
     uc->wsi = libwebsocket_client_connect_extended(context,
uc->address, uc->port, 0, uc->path, uc->address, uc->address,
protocols[0].name, -1, (void *)uc);
  uc->poll_return = libwebsocket_service(context, 1000);
  if (uc->call_back_return == LWS_CALLBACK_WSI_DESTROY ||
uc->call_back_return == LWS_CALLBACK_CLOSED)
	uc->call_back_return = 0;
	if (uc->wsi)
	{  	   uc->wsi = NULL;

This code seems to connect correctly when the server is initially down
and also when the server goes down and then up again. However I
noticed that my handle count for the process goes up by one everytime
I call "libwebsocket_client_connect_extended" and I'm not sure on how
to properly close the socket before trying to reconnect.

Is this approach completely wrong ? and if so how could I achieve a
self reconnecting socket and one where the client can be up before the
server, and then connect once the server is available?

Thanks in advance,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20140708/42f897a2/attachment.html>

More information about the Libwebsockets mailing list