Re-connection to TCP server


Just reviewed some code on this forum and I couldn’t find example where reconnection could take place.

I am using WIP lib to connect to server over gprs connection.
My modem has to be all time connected and in case of connection drop I need to reestablish connection.

Where is proper place to establish connection to server?


Firstly, you can’t connect to a “server” using a GPRS connection. The GPRS bearer connection is the data connection the the cellular network. You need another protocol to actually create the connection to the server. The most common options are:

  • TCP :create a TCP server on the server, and a TCP client on the device
  • UDP: A connectionless, un-guaranteed protocol that requires a UDP listener on the server side
  • HTTP: For connecting to websites etc

All three are easily handled by the Internet Library Plugin (as WIP Lib is now called). In my experience, the easiest to set up and get working is the TCP client/server option (see this post for more details: [url]])

If you want the connection to be permanently established, the best way is to periodically communicate with the server and make sure you get the correct response. If the communications fail, or if you receive any error events in your TCP handlers, you can shut down the connection and recreate it.


Thanks tomridl for answer , I forgot to mention that I am making TCP client connection to server over GPRS data.
For recreating connection to TCP server in case of conntion drop, is it fine if I place function startClient(); in WIP_CEV_PEER_CLOSE and IP_CEV_ERROR block?
see code below:

  case WIP_CEV_ERROR: {
    wip_debug( "[CONN.E.INFO] Error %i on socket. Closing.\n", ev->content.error.errnum);
	wip_shutdown ( ev->channel, TRUE, TRUE );
    //wip_close( ev->channel);
    wip_debug( "[CONN. INFO] Connection closed by peer\n");
	wip_shutdown ( ev->channel, TRUE, TRUE );
    //wip_close( ev->channel);

void startClient(){
   wip_channel_t client = wip_TCPClientCreate(SERVER_ADDRESS,SERVER_PORT,evh,NULL);

Another thing for keeping connection alive, shouldn’t I set WIP_COPT_KEEPALIVE in order to maintain connection all the time?
Instead of periodically commincating to server?

WIP_COPT_KEEPALIVE - “Sends a NOOP command every n tenth of seconds, so that the server and any NAT on the way won’t shut down the connection”


I think you should check the error type(ev->content.error.errnum) in WIP_CEV_ERROR case to decide creating the socket again. The error type may not be related with socket disconnection. In this case you dont need to create a new socket.
And you can handle WIP_CEV_DONE event also just in case of your application closes the socket somehow.
WIP_COPT_KEEPALIVE option will work for your case of keeping the connection alive. But you can also implement your own method so that you can have control on that methot.