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;
}

익명 사용자의 이미지

signal 과 alarm 을 사용하시면 될것 같군요..

signal 세팅..(SIGALRM에 대한)
while(1)
{
alarm(20);
read(....);
alarm(0);
}

20초 동안 read 가 호출안되면, signal 함수를 부를꺼고, 부른 함수에서,
소켓연결을 끊던지, 그밖의 등등의 처리를 해주면 될것 같습니다.
소켓에서 연결시간, 입력대기 시간을 처리하는 가장간단한 방법입니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.