hi .
i want to sl_connect() with timeout. (sl_connect default timeout is 10sec.)
but sl_connect function is not support timeout parameter.
so. i use NonBlocking socket / sl_select for sl_connect timeout.
but, it is not working.
How I use connection timeout ?
< this is my test sequence & code >
i test at tcp_socket example.
i use simple python server ( bind -> accept -> read socket)
this is connection check sequence.
> create socket.
> change socket to non-blocking
> sl_connect() call
> if sl_connect() return value is SL_SOC_OK, then connection success
> if sl_connect() return value is SL_EALREADY, then connecting.
> sl_select - check write fd with 3sec. timeout
> if sl_select return : 0 --> 3sec. timeout (connection fail)
> if sl_select return < 0 --> select function error (connection fail)
> if sl_select return 1 or > 0 --> check socket error code.
--> i want to check GetSockOpt with SL_SOC_ERROR parameter
but, GetSockOpt not support SL_SOC_ERROR.
so, i check with SL_FD_ISSET(iSockID, &writeFd)
> it is passed, but sl_send function return -1;
> change socket to blocking
-------------------------------------------------------------------------------------------------------------------------------------
i modify BsdTcpClient() function like this.
int BsdTcpClient(unsinged char usPort)
{
......
#if 0
// this is original code. // blocking sl_connection
// connecting to TCP server
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
#else
//this is my code, non-blocking connection
// non blocking setting
long nonBlocking = 1;
int iSetOptStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking));
// connect
iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize);
if (iStatus == SL_SOC_OK)
{
Report("sl_connected\r\n");
}
else
{
// connecting
if (iStatus == SL_EALREADY)
{
SlTimeval_t timeout;
SlFdSet_t WriteFds;
timeout.tv_sec = 3; // sec.
timeout.tv_usec= 0; // msec.
SL_FD_ZERO(&WriteFds);
SL_FD_SET(iSockID, &WriteFds);
// select
int result = sl_Select(iSockID + 1, NULL, &WriteFds, NULL, &timeout);
if (result > 0)
{
int so_error = FAILURE;
socklen_t len = sizeof(so_error);
// socket error check
#if 0
//connection check case#1 : sl_GetSockOpt does not support SL_SOC_ERROR Parameter.
sl_GetSockOpt(iSockID, SL_SOL_SOCKET, SL_SOC_ERROR, &so_error, &len);
Report("so_error : %d\r\n", so_error);
#else
//connection check case#2 : just file description setting check
// but in this case, sl_send function error (-1)
if (SL_FD_ISSET(iSockID, &WriteFds))
{
so_error = SL_SOC_OK;
}
#endif
if (so_error == SL_SOC_OK)
{
Report("connection success\r\n");
iStatus = SUCCESS;
}
else
{
Report("connection fail or pending\r\n");
}
}
else if (result == 0)
{
Report("connection timeout \r\n");
}
else
{
Report("connection error (code : %d)\r\n", result);
}
#endif
sl_send() // send data to server
...