Android TCP Socket Timout

Post here your questions about the C++ API for SFS2X

Moderators: Lapo, Bax, MBagnati

Stals
Posts: 1
Joined: 13 Aug 2014, 15:03

Android TCP Socket Timout

Postby Stals » 04 Dec 2015, 09:25

If you have a connection to the wifi and login successfully, join zone, etc. and after that disable routers connection to the internet keeping the devices connection to the wifi - on iOS after 20 seconds Connection Lost will be called. But on Android it just keeps the connection forever (I assume because of the default socket timeout setting that are set to null which equals to infinite wait time) and so when you restore the internet - the device immediately reconnects to the server even after a few minutes of waiting.
Is it possible to set the socket timeout for android so that it will not wait forever? Or maybe even set this timeout for all platforms which will be ideal but not necessary.
User avatar
Lapo
Site Admin
Posts: 20378
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Android TCP Socket Timout

Postby Lapo » 04 Dec 2015, 16:11

Hi,
yes the socket timeout is an OS dependent parameter and its behavior will change on every platform, even across different versions of the same OS sometimes.

About setting the socket timeout... we'll look into it. I am not sure if it's a socket related settings, or if there's an OS-wide parameter. There may be different approaches depending on the target platforms.

I'll keep you posted.
Lapo
--
gotoAndPlay()
...addicted to flash games
MBagnati
Posts: 125
Joined: 12 Feb 2013, 10:57

Re: Android TCP Socket Timout

Postby MBagnati » 17 Dec 2015, 07:02

Hi,
I think that the only way to get the real status of a socket, and therefore to discover a fall-down, is its usage.
A periodical transmission over the socket could be the key to check the end-to-end connectivity.
We can use the PingPong Request provided by SmartFox API as application message for a periodical transmission.

As first thing, I suggest to implement the PingPong Request in your Android application and then verify the behaviour disabling the routers connection to the internet.

Today Smartfox API uses a default value as socket transmission timeout.
In other words, the API will be notified about the failure of a socket transmission when the socket retries performed with the Operating System's default values will expire.
If PingPong Request on Android platform takes a long time to detect the unreachable status of SmartFox Server, we can think to extend API
to provide a new method that will be able to set a custom value for the socket transmission timeout.

As second thing, I suggest to manually emulate this scenario. Please set a socket transmission timeout to investigate its behaviour with PingPong Request.
To get this goal please edit the TCPClient.cpp file contained into Core\Sockets folder
Move at the end of the method

void TCPClient::OnBoostAsioConnect(const boost::system::error_code& code)


and add these statements

Code: Select all

// the timeout value
unsigned int timeout_milli = 3000;

// platform-specific switch
#if defined _WIN32 || defined WIN32 || defined OS_WIN64 || defined _WIN64 || defined WIN64 || defined WINNT
  // use windows-specific time
  int32_t timeout = timeout_milli;
  setsockopt(boostTcpSocket.native(), SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
  setsockopt(boostTcpSocket.native(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
#else
  // assume everything else is posix
  struct timeval tv;
  tv.tv_sec  = timeout_milli / 1000;
  tv.tv_usec = timeout_milli % 1000;
  setsockopt(boostTcpSocket.native(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
  setsockopt(boostTcpSocket.native(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
#endif


Please, let me know the result of your test

Return to “SFS2X C++ API”

Who is online

Users browsing this forum: No registered users and 2 guests