Dev/C, C++
socket 타임아웃처리
newtype
2006. 7. 7. 10:03
Unix socket 에서 타임아웃 처리를 하는 방법중에 select를 이용한 방법입니다.
[CODE type=c++]
/********************************************************************
* 데이타를 보낸다.
*
* return : int Error Code
* parameter :
* [in] int sock : 연결할 소켓 핸들
* [in] void *data : 전송할 데이타
* [in] unsigned int size : 데이타의 크기
* [in] int timeout : 타임아웃 처리를 할 시간
********************************************************************/
int gaSend(int sock, void *data, unsigned int size, int timeout)
{
int sockNum = sock +1;
fd_set sockSet;
struct timeval selTimeout = { timeout, 0 }; /* 초, 마이크로초 */
int result;
if ( sock == 0 )
return CONNECTION_REFUSED;
FD_ZERO(&sockSet);
FD_SET(sock, &sockSet);
result = select( sockNum, NULL, &sockSet, NULL, &selTimeout );
if ( result == 0 )
{
return CONNECTION_TIMEOUT;
}
else if ( result ==-1 )
{
return CONNECTION_REFUSED;
}
result = socketSend( sock, (char*)data, size) ;
if ( result < 0 )
{
return CONNECTION_REFUSED;
}
return SUCCESS;
}
/********************************************************************
* 데이타를 받는다.
*
* return : int Error Code
* parameter :
* [in] int sock : 연결할 소켓 핸들
* [in] void *data : 전송받을 데이타
* [in] unsigned int size : 데이타의 크기
********************************************************************/
int gaRecive(int sock, void *data, unsigned int size, int timeout)
{
int sockNum = sock +1;
fd_set sockSet;
struct timeval selTimeout = { timeout, 0 };
int result;
if ( sock == 0 )
return CONNECTION_REFUSED;
FD_ZERO(&sockSet);
FD_SET(sock, &sockSet);
result = select( sockNum, &sockSet, NULL, NULL, &selTimeout );
if ( result == 0 )
{
return CONNECTION_TIMEOUT;
}
else if ( result ==-1 )
{
return CONNECTION_REFUSED;
}
result = socketRecv(sock, (char*)data, size, timeout);
if( result < 0 )
{
return CONNECTION_REFUSED;
}
return SUCCESS;
}
[/HTML][/CODE]
[CODE type=c++]
/********************************************************************
* 데이타를 보낸다.
*
* return : int Error Code
* parameter :
* [in] int sock : 연결할 소켓 핸들
* [in] void *data : 전송할 데이타
* [in] unsigned int size : 데이타의 크기
* [in] int timeout : 타임아웃 처리를 할 시간
********************************************************************/
int gaSend(int sock, void *data, unsigned int size, int timeout)
{
int sockNum = sock +1;
fd_set sockSet;
struct timeval selTimeout = { timeout, 0 }; /* 초, 마이크로초 */
int result;
if ( sock == 0 )
return CONNECTION_REFUSED;
FD_ZERO(&sockSet);
FD_SET(sock, &sockSet);
result = select( sockNum, NULL, &sockSet, NULL, &selTimeout );
if ( result == 0 )
{
return CONNECTION_TIMEOUT;
}
else if ( result ==-1 )
{
return CONNECTION_REFUSED;
}
result = socketSend( sock, (char*)data, size) ;
if ( result < 0 )
{
return CONNECTION_REFUSED;
}
return SUCCESS;
}
/********************************************************************
* 데이타를 받는다.
*
* return : int Error Code
* parameter :
* [in] int sock : 연결할 소켓 핸들
* [in] void *data : 전송받을 데이타
* [in] unsigned int size : 데이타의 크기
********************************************************************/
int gaRecive(int sock, void *data, unsigned int size, int timeout)
{
int sockNum = sock +1;
fd_set sockSet;
struct timeval selTimeout = { timeout, 0 };
int result;
if ( sock == 0 )
return CONNECTION_REFUSED;
FD_ZERO(&sockSet);
FD_SET(sock, &sockSet);
result = select( sockNum, &sockSet, NULL, NULL, &selTimeout );
if ( result == 0 )
{
return CONNECTION_TIMEOUT;
}
else if ( result ==-1 )
{
return CONNECTION_REFUSED;
}
result = socketRecv(sock, (char*)data, size, timeout);
if( result < 0 )
{
return CONNECTION_REFUSED;
}
return SUCCESS;
}
[/HTML][/CODE]
반응형