select에 대해...

살자의 이미지

select 사용시

struct timeval tim;
tim.tv_sec = 0;
tim.tv_usec = 0;
while(1)
{
      select(MAXfd, &rset, NULL , NULL,&tim);
      if(FD_ISSET(sockfd))
      {
             if((readbyte = read(sockfd, recvBuff, MAXSIZE)) > 0)
             {
                      
              }
       }
}

위의 코드 처럼 구현을 했을때..
클라이언트에서 Close를 하면 어떻게 체크를 해야 하는지여?
고수님들의 조언 부탁드립니다.

pynoos의 이미지

살자 wrote:
select 사용시
struct timeval tim;
tim.tv_sec = 0;
tim.tv_usec = 0;
while(1)
{
      select(MAXfd, &rset, NULL , NULL,&tim);
      if(FD_ISSET(sockfd))
      {
             if((readbyte = read(sockfd, recvBuff, MAXSIZE)) > 0)
             {
                      
              }
       }
}

위의 코드 처럼 구현을 했을때..
클라이언트에서 Close를 하면 어떻게 체크를 해야 하는지여?
고수님들의 조언 부탁드립니다.

close 해도 select 되어 내려오게 되며, read, recv 에서 0 을 return 합니다.

최피디의 이미지

일반적으로 소켓 옵션을 보거나 클라이언트로부터 read한 값이 0일 경우
클라이언트에서 종료한 것으로 보는 것 같습니다.

int isDisconnected = 0;

if( FD_ISSET( sockfd, &rset ) )
{
 if( getsockopt( sockfd, SOL_SOCKET, SO_ERROR, &errorval, &errorlen ) == -1 )
 {
  fprintf(stderr, "getsocketopt error\n");  
 }

 IsDisconnected = 0;

 if( errorval == ECONNRESET || errorval == ETIMEDOUT || errorval == EHOSTUNREACH )
 {
  IsDisconnected = 1;
 }
 result = read( sockfd, recvbuff, MAXLINE );

 if( result <= 0 || IsDisconnected == 1 )
 {
  //클라이언트 소켓 끊어졌군요... 처리해주시고
  close(sockfd);
 } else {
  // 정상적으로 읽었군요. 읽은 값 처리해주시고...
 }
}

KT하이텔, 앱스 개발자

june8th의 이미지

select에서 readfd가 선택되는 경우 중의 하나가 FIN을 받았을때입니다.
(즉, 저쪽에서 먼저 close했을때)

뭔가 있다고 해서 읽을려고 했는데 아무것도 없을뿐더러 에러도 아니라면,
저쪽에서 끊자는 것이니까 이쪽에서 close하면 됩니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.