简体   繁体   English

应该何时连接数据报套接字?

[英]When should a datagram socket be connected?

I am working in C, using the POSIX socket API. 我在C中工作,使用POSIX套接字API。

I am not sure when it is appropriate to connect a datagram socket. 我不确定何时连接数据报套接字是合适的。 As I understand it, UDP is connectionless, and SOCK_DGRAM sockets use UDP. 据我了解,UDP是无连接的,SOCK_DGRAM套接字使用UDP。 So what happens when connect() and accept() are used on datagram sockets? 那么当在数据报套接字上使用connect()和accept()时会发生什么?

It seems to me that connecting them and using send()/recv() is easier than not connecting them and using sendto()/recvfrom(). 在我看来,连接它们并使用send()/ recv()比不连接它们和使用sendto()/ recvfrom()更容易。

Is there a difference in the functionality of the sockets when connected, or is this just an abstraction? 连接时套接字的功能是否有区别,或者这仅仅是一种抽象?

connect() on a datagram socket is appropriate when you want the convenience of a default destination peer for use by send() , rather than explicitly specifying a destination with sendto() . 当您希望send()使用默认目标对等体的便利性而不是使用sendto()显式指定目标时,数据报套接字上的connect()是合适的。 There is no functional difference. 没有功能差异。

connect() ing to an AF_UNSPEC address will clear the datagram socket's default peer. connect()到AF_UNSPEC地址将清除数据报套接字的默认对等体。 (This last behavior is widely supported, I think, but only recently formalized .) (我认为,这最后一种行为得到了广泛支持,但最近才正式化 。)

connect() on a datagram socket will limits recv() s to data originating from the peer. 数据报套接字上的connect()会将recv() s限制为源自对等体的数据。

accept() on a datagram socket is an error (EOPNOTSUPP), regardless of "connectedness". 无论“连通性”如何,数据报套接字上的accept()都是错误(EOPNOTSUPP)。

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

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