Networking 从UDP套接字读取数据
我使用以下函数读取文件描述符Networking 从UDP套接字读取数据,networking,udp,tcp,Networking,Udp,Tcp,我使用以下函数读取文件描述符 int cread(int fd, char *buf, int n){ int nread; if((nread=read(fd, buf, n))<0){ perror("Reading data"); exit(1); } return nread; } 它们都可以很好地使用TCP siocket,但不能使用udp套接字。。如果您能提供任何帮助,我们将不胜感激。目前还不清楚您的问题是什么,但是如果net\u fd是一
int cread(int fd, char *buf, int n){
int nread;
if((nread=read(fd, buf, n))<0){
perror("Reading data");
exit(1);
}
return nread;
}
它们都可以很好地使用TCP siocket,但不能使用udp套接字。。如果您能提供任何帮助,我们将不胜感激。目前还不清楚您的问题是什么,但是如果
net\u fd
是一个UDP套接字,那么两个cwrite()
调用将创建两个UDP数据报
使用UDP预先设置大小没有多大意义-UDP为您维护消息边界。因此,在UDP情况下,只需完全删除
plungth
部分。是否在为UDP情况设置的文件描述符中tap_fd
?
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);
}