TCP/IP 관련 질문이여
HTTP프로토콜을 이용하여 데이터를 송수신하는 프로그램을 작성하여 테스
트하는데 문제가 발생해서 질문 드립니다.
다름이 아니라 서버에서 데이터를 읽을때 시간을 체크하여 그 시간이 초과
되면 연결을 끊을려구 하는데 이상하게 Read()함수에서 걸려서 넘어갈생각
을 안하네여..
그래서 select()함수을 적용해서 타임아웃을 20초 걸었는데 끊어지기는 하
는데 정확히 20초에 끊기는 경우도 있구 그 이상 넘어서 끊어지는 경우가
있더라구여. 그리구 아예 끊기지 않는 경우두 있구여.
방법좀 알려주세여...제가 작성할 프로그램은 타임아웃을 20초를 걸어서
시간 초과시 TcpRead()함수를 벗어나게 하구 그리구 20초 타임아웃에 끊어
졌는지 정상 처리 됐는지 판단하게 하고 싶은데 아시는분 도움좀 주세여
소스는 아래와 같습니다.
//TCP/IP Connection
int SockClientInit(const char *HostIpAddr, const int HostPortNo){
struct sockaddr_in ServerAddress;
int sockfd, sleepcnt=0, status;
struct hostent *host;
memset((void*)&ServerAddress, 0, sizeof(ServerAddress));
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = ntohs(HostPortNo);
if((host=gethostbyname(HostIpAddr))==NULL){
printf("no such HostAddress![%s][%s]\n", HostIpAddr, strerror
(errno));
return FAIL;
}
memcpy((char *)&ServerAddress.sin_addr.s_addr, (char *)host-
>h_addr, host->h_length);
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("Socket Open error![%s][%s]\n", HostIpAddr, strerror
(errno));
}
while((status=connect(sockfd, (SA *)&ServerAddress, sizeof
(ServerAddress))) < 0){
printf("again connecting %s%d%s\n", HostIpAddr,
HostPortNo, strerror(errno));
sleepcnt++;
if(sleepcnt > 5){
printf("fail connecting %s\n", strerror(errno));
return FAIL;
}
sleep(1);
}
printf("Connect Success!\n");
return sockfd;
}
char *TcpRead(int SockFd, char *str, ){
char Buff[SIZE];
int nbytes;
int val;
int maxfd;
struct t;
fd_set rset;
maxfd = SockFd + 1;
val = fcntl(SockFd, F_GETFL, 0);
fcntl(SockFd, F_SETFL, val | O_NONBLOCK);
t.tv_sec = 20L;
t.tv_usec = 0L;
while(1){
FD_ZERO(&rset);
FD_SET(SockFd, &rset);
memset(Buff, 0x00, SIZE);
errcnt = 0;
if(select(maxfd, &rset, NULL, NULL, &t) < 0){
errcnt++;
if((errcnt++) => 5){
printf("select Check! ErrCnt=%d\n", errcnt);
return NULL;
}
}
if (FD_ISSET(SockFd, &rset)) {
if( (nbytes = read(SockFd, Buff,SIZE)) <= 0){
if(nbytes == 0){
FD_CLR(SockFd, &rset);
break;
}else
continue;
}
str = ReMallStr(str, Buff, LOGName);
memset(Buff, 0x00, SIZE);
}
}
return str;
}
Re: TCP/IP 관련 질문이여
signal 과 alarm 을 사용하시면 될것 같군요..
signal 세팅..(SIGALRM에 대한)
while(1)
{
alarm(20);
read(....);
alarm(0);
}
20초 동안 read 가 호출안되면, signal 함수를 부를꺼고, 부른 함수에서,
소켓연결을 끊던지, 그밖의 등등의 처리를 해주면 될것 같습니다.
소켓에서 연결시간, 입력대기 시간을 처리하는 가장간단한 방법입니다.
댓글 달기