[질문] 소켓 프로그램

wag123의 이미지

서버랑 클라이언트에서 통신하다가요
클라이언트에서 close를 안하고 그냥 죽거나 network 선이 끊어져서 통신이 투절됬을 때
서버에서 이것을 어떻게 쳐리해줘야 하나요...
좀 가르쳐 주세요

shean0의 이미지

server.c
ret=send(sok,buf,len,0); ret=write(sok,buf,len);
ret=recv(sok,buf,len,0);  ret=read_len(sok,buf,len);  
if(ret !=len) return error;

대충..이런 개념에서요.. write도 마찬가지지만... read시에 blocking되어서
문제 될 소지가 있으니.. usleep,count룰 사용해두 되구요..
아님 alarm(3); 뭐 이런 방법을 사용해도 되구요..

아래 참조하시고. . 조금만 더 찿아 보시면.. 많은 분이 답해주실거에요..

unsigned int read_len(int sok,char * buf,unsigned int len)
{
  unsigned int r_len;
  unsigned int nleft,nread;
  char *p;
  int count=0;
  nleft=len;
  p=buf;
  while(nleft>0)
  {
     nread=read(sok,p,nleft);
     if(nread<0)
     {
       return ERROR;
     }
     else if(nread==0)
     {
         count++;
         return ERROR;  /*   EOF*/
     }
     else {
               ;
     }
     nleft -=nread;
     p +=nread;
     count++;
#ifdef NoUse
      if(count>10) return -1;/* 길이가 틀리기 때문에 에러로 처리된다 */
      else usleep(1);
#endif
  }
  return (len-nleft);  
}

언제나 즐프를 꿈꾸며~

hwandori의 이미지

select를 써보세요...

alsong의 이미지

SO_KEEPALIVE로 검색 해보시기 바랍니다.

폴링하는 방법과 SO_KEEPALIVE를 사용하는 방법이 있습니다.

폴링은 주기적으로 소켓에 write하는 방법이 있고
SO_KEEPALIVE는 내부적으로 체크합니다.

그나저나 백수 언제 탈출하냐... ㅡㅡ; 배고파라.

dudungsil의 이미지

이거 제대로 동작하나요? 쓰지않는게 속편하다는 결론을 얻은 이후에는 SO_KEEPALIVE를 고려해 본 적이 한번도 없는 것 같네요. 일정 시간마다 소켓에 직접 써보는게 제일 확실하고 속편합니다.

산넘어 산

pynoos의 이미지

dudungsil wrote:
이거 제대로 동작하나요? 쓰지않는게 속편하다는 결론을 얻은 이후에는 SO_KEEPALIVE를 고려해 본 적이 한번도 없는 것 같네요. 일정 시간마다 소켓에 직접 써보는게 제일 확실하고 속편합니다.

왠만한 TCP 스택은 대부분 지원할 것입니다.
저 기능을 도입한 이후로 파이월이 일정 시간 데이터 없는 connection을 끊는 것을 2시간에 한 번 꼴로 확인하여 서버쪽에서도 제거해주는 기능을 구현한 적 있습니다.

댓글 달기

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