简体   繁体   English

UDP中的套接字需要文件描述符

[英]Need file descriptor for socket in UDP

In my tcp program i have an int called fd, fd is set = to the file descriptor returned when accept() is called. 在我的tcp程序中,我有一个称为fd的int,fd设置为=调用accept()时返回的文件描述符。 I now want to make this code be udp instead of tcp, but udp has no accept() so how do i get the value accept() would have returned, but in udp? 我现在想使此代码为udp而不是tcp,但是udp没有任何accept(),所以我如何获得值accept()会返回,但是在udp中呢?

UDP is connectionless, so listen() and accept() are not used (and connect() merely assigns a static binding so send() / recv() can be used instead of sendto() / recvfrom() , thus allowing the OS perform some filtering of packets for you). UDP是无连接的,因此不使用listen()accept() (而connect()仅分配静态绑定,因此可以使用send() / recv()代替sendto() / recvfrom() ,从而允许操作系统为您执行一些数据包过滤)。 You get a UDP descriptor from socket() and then send/receive on it as needed, where sendto() (or connect() + send() ) tells it where to send an outbound packet to, and recvfrom() (or connect() + recv() ) tells you where an inbound packet came from. 您可以从socket()获取UDP描述符,然后根据需要在其上发送/接收,其中sendto() (或connect() + send() )告诉它要将出站数据包发送到哪里,然后recvfrom() (或connect() + recv()告诉您入站数据包来自何处。 A single UDP socket can send packets to multiple clients using sendto() , and receive packets from multiple clients using recvfrom() . 单个UDP套接字可以使用sendto()将数据包发送到多个客户端,并使用recvfrom()从多个客户端接收数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM