简体   繁体   English

gen_tcp:send / 2是否阻塞?

[英]Is gen_tcp:send/2 blocking?

Is gen_tcp:send() asynchronous? gen_tcp:send()是异步的吗? Assume I'll send some byte array using gen_tcp:send/2. 假设我将使用gen_tcp:send / 2发送一些字节数组。 Will process continue to work: 流程将继续工作:

a) Immediately a)立即
b) At the time data will arrive in target's inner buffer b)那时数据将到达目标的内部缓冲区
c) When the target gets the data from buffer c)当目标从缓冲区获取数据时

Thank You in advance. 先感谢您。

gen_tcp:send/2 is synchronous. gen_tcp:send/2是同步的。 It means that the call returns only after the given packet is really sent. 这意味着只有在实际发送给定的数据包后,呼叫才会返回。 Usually it happens immediately, however if TCP window is full gen_tcp:send/2 blocks until the data is sent. 通常它会立即发生,但是如果TCP窗口已满gen_tcp:send/2块,直到发送数据为止。 So it means that the call can theoretically block infinitely (for example when receiver does not read data from socket on its side). 因此,这意味着该调用理论上可以无限阻塞(例如,当接收方未从其一侧的套接字读取数据时)。 Fortunately there are some options to avoid such situation. 幸运的是,有一些选择可以避免这种情况。 There are two options {send_timeout, Integer} and {send_timeout_close, Boolean} for sockets which can be specified by the call inet:setopts/2 . 套接字有两个选项{send_timeout, Integer}{send_timeout_close, Boolean} ,可以通过调用inet:setopts/2来指定。 The first one allows to specify a longest time to wait for a send operation. 第一个允许指定等待发送操作的最长时间。

When the limit is exceeded, the send operation will return {error, timeout} . 当超过限制时,发送操作将返回{error, timeout} Default value of that option is infinity (and it is the reason of infinite block). 该选项的默认值是infinity (这是无限块的原因)。 Also unfortunately it is unknown how much of data was sent if {error, timeout} was returned. 同样不幸的是,如果返回了{error, timeout}则发送了多少数据是未知的。 In that case it is better to close the socket. 在这种情况下,最好关闭插座。 If the second option {send_timeout_close, Boolean} is set to true then the socket will be close automatically if {error, timeout} occurs. 如果第二个选项{send_timeout_close, Boolean}设置为true则如果发生{error, timeout}套接字将自动关闭。

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

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