recv() 함수 문제..
글쓴이: garykwak / 작성시간: 월, 2010/03/08 - 5:24오후
첨 글올리네요
Linux 2.6 사용자 입니다.
Embeded 환경이구요.
TCP/IP 소켓통신을 하는데.
소스는 이렇습니다.
rv =recv(fd, buffer, MAXDATASIZE, 0);
fd 는 socket 지정자 이고요
buffer 는 수신 데이타 저장 버퍼
MAXDATASIZE 는 최대 허용 데이타 Lenght 입니다.
recv 함수에 진입해서 빠져나오지 못하는경우가 있네요.
설사 빠져나오더라도 최악의 경우는 몇분씩 함수 안에 들어가서 대기.....헉...
이러면 사용자 인터페이스에도 문제가 생기는데요.
혹시 방법 없을까요 ?
질문입니다.
recv 에서 일정기간 응답이 없으면 TIME out 으로 빠져 나오게 하거나.
아니면 다른 좋은 방법요.
고수님들 부탁 드립니다.
꾸벅.
Forums:
윈도우예제인데요, 리눅스도 동일합니다.
리눅스도 마찬가지인데, 개별 함수는 찾아보세요.
근데, 시스템이 setsockopt함수를 지원하는지 꼭 확인.
WSADATA wsa;
if(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
{
//AfxMessageBox("WAStartup 에러");
return FALSE;
}
// socket()
m_sock = socket(AF_INET, SOCK_STREAM, 0);
if(m_sock == INVALID_SOCKET)
{
//AfxMessageBox("소켓 생성 에러");
return FALSE;
}
//=============================================
//이부분이 핵심..
int nTime = 10000; //10초 타이머.
if (setsockopt(m_sock,SOL_SOCKET,SO_RCVTIMEO,(char*)&nTime, sizeof( nTime)) == SOCKET_ERROR)
{
shutdown(m_sock,SD_RECEIVE|SD_SEND);
closesocket(m_sock);
return FALSE;
}
//=============================================
SOCKADDR_IN serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(nPort);
char szIP[30];
memset(szIP,'\0',30);
WideCharToMultiByte(CP_ACP, 0, sIP, sIP.GetLength(), szIP, 30, 0, 0);
serveraddr.sin_addr.s_addr = inet_addr(szIP);
retval = connect(m_sock, (SOCKADDR *)&serveraddr,sizeof(serveraddr));
..............
흠..
ioctl로 미리 검사해보신 후에
recv해도될듯
block 모드라서
block 모드라서 그렇습니다. block 모드에서는 데이터가 안들어오면 들어올때까지 기다립니다.
non-block에 대해서 검색해보세요.
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.
-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.
저 같은 경우에는
저 같은 경우에는 타임아웃 시간을 지정하여 select 한 후,
recv할 데이터가 있는지 확인한 다음에 recv합니다.
--------------
save the earth
생산적인 댓글을 달자
recv/send의 마지막 인자 flags에 옵션을 주면 됩니다.
마지막 인자(flags)에 MSG_DONTWAIT를 주면 읽어들일 데이터가 없으면 곧바로 리턴하므로 루프로 돌리면 됩니다.
댓글 달기