2012-02-10 8 views
0

UDPパケットがsendto()を使用してホストに送信された場合はどうなりますか。 私は送信しているすべてのビットが送信されます(戻り値からわかります)。私はrecvfrom()を使用しますが、何も出力せずにプログラムを終了しません(戻り値なし)。UDPによるネットワーク処理

返信がなければ、プログラムは終了しなければならないと思います。

ポートからのUDPパケットの返答は?

このパケットはファイアウォールでブロックされていますか?もしそうなら、なぜsendtoの戻り値が負でないのでしょうか?

+0

あなたは何をしようとしているのですか? UDPが使用したいプロトコルであることを確認しましたか? – bitops

答えて

0

recvfrom()は、ソケットを非ブロックに設定しない限り、メッセージが受信されるまでブロックされます。

あなたが見てみたいインタフェースである(お使いのプラットフォームに依存)FIONBIOまたはO_NONBLOCK

  • ioctl()
  • select()データが到着するのを待つ、またはwhile
後のタイムアウト

sendto()のアドレスとポート番号は通常ネットワークバイトオーダーであることを覚えておいてください。ntohlntohs

0

クライアントまたはサーバーに何らかのエラーがある必要があります。 localhostを先に試してみてください。

これは私のテストで使っていたnonblocking udp client/serverの例ですが、ioctl()を使ってソケットに読み込むデータがあるかどうかを確認します。また、あなたがマイクロ秒で待機するタイムアウトを指定することができ、より効率的であるのepollを使っていくつかの深刻なアプリケーションをしたい:

[[email protected] tests]$ cat udpserv.c 
#include <stdlib.h> 
#include <fcntl.h> 
#include <sys/ioctl.h> 
#include <string.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <unistd.h> 
#include <errno.h> 

#define BUFLEN 512 
#define NPACK 15 
#define PORT 9930 

void diep(char *s) 
{ 
    printf("erno=%d errstr=%s\n",errno,strerror(errno)); 
    perror(s); 
    exit(1); 
} 

int main(void) 
{ 
    struct sockaddr_in si_me, si_other; 
    int s,ret,nbytes, i, slen=sizeof(si_other); 
    char buf[BUFLEN]; 

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) 
    diep("socket"); 

    memset((char *) &si_me, 0, sizeof(si_me)); 
    si_me.sin_family = AF_INET; 
    si_me.sin_port = htons(PORT); 
    si_me.sin_addr.s_addr = htonl(INADDR_ANY); 
    if (bind(s, (struct sockaddr*) &si_me, sizeof(si_me))==-1) 
     diep("bind"); 

    fcntl(s, F_SETFL, fcntl(s, F_GETFL, 0) | O_NONBLOCK); 
    sleep(10); 
    for (i=0; i<NPACK; i++) { 
    ret=ioctl(s,FIONREAD,&nbytes); 
    if (ret==-1) { 
     printf("error on FIONREAD\n"); 
    } else { 
     printf("nbytes=%d\n",nbytes); 
    } 
    if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr*) &si_other, &slen)==-1) 
     diep("recvfrom()"); 
    printf("Received first half of packet from %s:%d\nData: %s\n\n", 
      inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); 
    } 

    close(s); 
    return 0; 

} 



[[email protected] tests]$ cat udpclient.c 
#include <stdlib.h> 
#include <string.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <unistd.h> 

#define SRV_IP "127.0.0.1" 
#define BUFLEN 200 
#define NPACK 10 
#define PORT 9930 

/* diep(), #includes and #defines like in the server */ 
void diep(char *s) 
{ 
    perror(s); 
    exit(1); 
} 

int main(void) 
{ 
    struct sockaddr_in si_other; 
    int s, i, slen=sizeof(si_other); 
    char buf[BUFLEN]; 

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) 
    diep("socket"); 

    memset((char *) &si_other, 0, sizeof(si_other)); 
    si_other.sin_family = AF_INET; 
    si_other.sin_port = htons(PORT); 
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { 
    fprintf(stderr, "inet_aton() failed\n"); 
    exit(1); 
} 

for (i=0; i<NPACK; i++) { 
    printf("Sending packet %d\n", i); 
    sprintf(buf, "This is packet %d\n", i); 
    if (sendto(s, buf, BUFLEN, 0, (struct sockaddr*) &si_other, slen)==-1) 
     diep("sendto()"); 
} 

close(s); 
return 0; 
} 

のsendto()を、それが送信したバイト数を返すので、非負です。 sendtoのマニュアルページを確認してください

関連する問題