socket에 옵션을 주고 싶은데요...

junteken의 이미지

안녕하세요~~
socket에 option을 주어서 네트워크에 연결되지 않은 host에 접속을 하려고
할때 빨리 socket의 operation을 종료시키고 싶습니다.
이럴때는 어떤 option을 어떻게 주어야 하나요...
제가 조사해본바로는 setsockopt function을 쓰면 되는걸로 아는데..
잘안되네요...
한마디로 제 질문의 요지는 network상에 존재하지 않는 host에 연결시도시
대략1초정도 connection시도를 해보고 connection function을 종료시
키고 싶다는 것입니다. 다음은 제가 테스트해보고 있는 소스코드입니다.

     88     struct timeval period;
     89     period.tv_usec=10;
     90     period.tv_sec= 1;
     91 
     92 
     93     setsockopt(m_nSocket, SOL_SOCKET, SO_RCVTIMEO, &period, sizeof        (period));
     94     
     95     setsockopt(m_nSocket, SOL_SOCKET, SO_SNDTIMEO, &period, sizeof        (period));
     96     m_nSocket= socket(PF_INET, SOCK_STREAM, 0);

감사합니다~~~:)

jinyeong의 이미지

connection timeout을 사용하시는 방법은 두가지가 있습니다.

1. alarm을 사용하시는 방법 (간단한 방법)
2. select를 사용하시는 방법 (복잡한 방법)

thread를 사용하시는 경우가 아닌 간단한 프로그램이라면 alarm을 쓰십시오.

다음을 참조 하십시오.

Stevens의 답변이 1번의 경우고, Gierth 의 답변이 2번의 경우입니다.

Socket FAQ, 3.5

Quote:
3.5. How can I set the timeout for the connect() system call?

From Richard Stevens (rstevens@noao.edu):

Normally you cannot change this. Solaris does let you do this, on a
per-kernel basis with the ndd tcp_ip_abort_cinterval parameter.

The easiest way to shorten the connect time is with an alarm() around
the call to connect(). A harder way is to use select(), after setting
the socket nonblocking. Also notice that you can only shorten the
connect time, there's normally no way to lengthen it.

From Andrew Gierth (andrew@erlenstar.demon.co.uk):

First, create the socket and put it into non-blocking mode, then call
connect(). There are three possibilities:

o connect succeeds: the connection has been successfully made (this
usually only happens when connecting to the same machine)

o connect fails: obvious

o connect returns -1/EINPROGRESS. The connection attempt has begun,
but not yet completed.

If the connection succeeds:

o the socket will select() as writable (and will also select as
readable if data arrives)

If the connection fails:

o the socket will select as readable *and* writable, but either a
read or write will return the error code from the connection
attempt. Also, you can use getsockopt(SO_ERROR) to get the error
status - but be careful; some systems return the error code in the
result parameter of getsockopt, but others (incorrectly) cause the
getsockopt call *itself* to fail with the stored value as the
error.

여담이지만, 인용하신 코드의 순서가 약간..;;

I thought what I'd do was,
I'd pretend I was one of those deaf-mutes.. or should I?

댓글 달기

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 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.