udp로 broadcast하는 방법

송지석의 이미지

UDP 프로토콜로 broadcast를 하는 방법을 잘 모르겠네요...
인터넷에 나오는 예제를 따라해봐도
sendto: Network is unreachable
로 나오거나
패킷 캡쳐해봤을 때 ARP만 잔뜩 뿌리거든요.
UDP로 broadcast 하는 간단한 코드를 좀 구할 수 있을까요?

송지석의 이미지

아 되긴 되는군요.
문제는 윈도우에서 udp 패킷을 거부하기 때문이었습니다.
테스트를 윈도우/cygwin와 리눅스에서 하고 있었는데
리눅스에서 윈도우로 udp 패킷을 보내면 받아지질 않더군요.
윈도우에서 리눅스로 보내면 됩니다.
정확한 메커니즘은 모르겠습니다. recvfrom하고 있으면 패킷 캡쳐해봤을 때는 제대로 가는데 어플리케이션은 아무것도 못받고, 받는 쪽 프로그램을 끄면 ICMP로 port unreacheable이 리턴되네요.

송지석의 이미지

다시 해보니까 리눅스에서는 보내는 것이 안되고 있습니다.
받는 것만 됩니다.
리눅스에서 sendto시에 Network is unreachable 에러가 납니다.

int tosd;
struct sockaddr_in toaddr;
char buffer[1024];
port = DEFAULT_PORT;
strcpy(buffer,"hello");
tosd = socket(AF_INET, SOCK_DGRAM, 0);
if ( tosd < 0 ) perror("socket failed");
bzero(&toaddr, sizeof(toaddr));
toaddr.sin_family = PF_INET;
toaddr.sin_port = htons(port);
toaddr.sin_addr.s_addr = INADDR_BROADCAST;
if ( setsockopt(tosd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) != 0 )
    perror("set2 SO_BROADCAST failed");
if ( setsockopt(tosd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0 )
    perror("set SO_REUSEADDR failed");
while(1) {
    if (sendto(tosd, buffer, strlen(buffer), 0, (struct sockaddr*)&toaddr,
                sizeof(toaddr)) < 0)
        perror("sendto");
    sleep(1);
}

코드는 이렇습니다.
간단한 루틴인데 윈도에선 보내지고, 리눅스에선
sendto: Network is unreachable 라면서 보내지지 않습니다.
어떻게 하면 될까요?
pynoos의 이미지

ifconfig 가 제대로 안잡히면 간혹 permission denied로 에러메시지를 보내오는 경우가 있습니다.

ifconfig의 broadcast나 network 설정을 살펴보세요.

송지석의 이미지

$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:02:2A:C7:39:39
          inet addr:10.11.11.12  Bcast:10.11.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:143005 errors:6 dropped:0 overruns:0 frame:0
          TX packets:89225 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:74799630 (71.3 Mb)  TX bytes:31730357 (30.2 Mb)
          Interrupt:10 Base address:0xe000

제대로 되어있는 것 같습니다만....
그런데 뭔가 문제가 있긴 한 것 같습니다.
다른 시스템(학교의 서버에서 잠시 돌려봤음 데비안 3.0)에서는 에러가 안나더군요.
송지석의 이미지

찾았습니다.
/etc/sysconfig/network-scripts/ifcfg-eth0 에 GATEWAY=10.11.11.1 를 추가해주니 되는군요
GATEWAY=10.11.11.1 는 실제로는 없는 주소인데 그래도 괜찮은가보네요..

pynoos의 이미지

윈도우에서 ipconfig /all 을 해서 확인해보면 Subnet Mask가
255.255.0.0
으로 같은데도 안되었었단 말이죠?
Gateway 설정때문에 된다니.. 8)

댓글 달기

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