是否连接()块,用于TCP套接字? [英] Does connect() block for TCP socket?

查看:157
本文介绍了是否连接()块,用于TCP套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在读TLPI(Linux的编程接口),我有一个问题关于连接()。

Hi I am reading TLPI (The Linux Programming Interface), I have a question about connect().

据我了解,连接()将立即返回,如果听的挂起的连接数()没有达到积压。
它会阻止,否则。 (根据图56-2)

As I understand, connect() will immediately return if the pending connection numbers of listen() doesn't reach "backlog". And it will blocks otherwise. (according to figure 56-2)

但对于TCP套接字,它会一直阻塞,直到被调用(根据图61-5)接受()在服务器端。

But for TCP socket, it will always block until accept() on server side is called (according to figure 61-5).

我是正确的?
因为我看到,在这个例子code(p.1265),它调用听()听一个特定的端口,然后调用connect()到该端口之前调用accept()。

Am I correct? Because I saw that in the example code (p.1265), it calls listen() to listen to a specific port and then calls connect() to that port BEFORE calling accept().

因此​​,在这种情况下,连接()块永远,不是吗?

So connect() blocks forever in this case, doesn't it?

谢谢!

推荐答案

有几乎没有任何立即关于联网,东西可以丢在路上,而且在理论上应立即执行的操作可能会在实践中没有这样做,并且在任何情况下,有以结束传输时间结束

There's hardly any "immediately" regarding networking, stuff can be lost on the way, and an operation that should be performed immediately in theory might not do so in practice, and in any case there's the end to end transmission time.

然而


  • 连接()上的TCP套接字是阻塞操作,除非套接字描述被置于非阻塞模式。

  • connect() on a TCP socket is a blocking operation unless the socket descriptor is put into non-blocking mode.

操作系统取TCP握手的照顾,当握手结束后,连接()返回。 (那是,
连接()不会阻塞,直到另一端调用accept())

The OS takes care of the TCP handshake, when the handshake is finished, connect() returns. (that is, connect() does not block until the other end calls accept())

一个成功的TCP握手将排队到服务器应用程序,并且可以接受()'以后编的任何时间。

A successful TCP handshake will be queued to the server application, and can be accept()'ed any time later.

这篇关于是否连接()块,用于TCP套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆