2011-04-09 136 views
0

我使用下面的函数从文件进行读取...读取数据从UDP套接字

int cread(int fd, char *buf, int n){ 

    int nread; 

    if((nread=read(fd, buf, n))<0){ 
    perror("Reading data"); 
    exit(1); 
    } 
    return nread; 
} 

下面是使用上述功能

if(FD_ISSET(tap_fd, &rd_set)){ 
    /* data from tun/tap: just read it and write it to the network */ 

    nread = cread(tap_fd, buffer, BUFSIZE); 

    tap2net++; 
    do_debug("TAP2NET %lu: Read %d bytes from the tap interface\n", tap2net, nread); 

    /* write length + packet */ 
    plength = htons(nread); 
    nwrite = cwrite(net_fd, (char *)&plength, sizeof(plength)); 
    nwrite = cwrite(net_fd, buffer, nread); 

    do_debug("TAP2NET %lu: Written %d bytes to the network\n", tap2net, nwrite); 
} 

他们都很好地工作的功能TCP插座,但不是与udp插座..任何帮助,将不胜感激

+0

在为UDP情况设置的文件描述符中是否有tap_fd? – Jeff 2011-04-09 07:13:11

回答

0

这并不清楚你的问题到底是什么,但如果net_fd是一个UDP套接字,那么两个cwrite()呼叫将创建两个 UDP数据报。

在预先考虑UDP的大小方面没有多大意义 - UDP为您维护消息边界。所以在UDP情况下,请完全删除plength部分。