Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Networking 从UDP套接字读取数据_Networking_Udp_Tcp - Fatal编程技术网

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);
}