Socket是否打开另一个线程?它会返回一些东西吗?

发布于 2024-08-25 15:04:39 字数 772 浏览 3 评论 0原文

在客户端应用程序中,我调用new Socket(serverIP,serverPort)。结果,客户端应用程序向服务器应用程序发送打开套接字的请求。它会启动一个新线程吗?我的意思是以下哪项是正确的?

  1. 客户端应用程序发送请求并立即开始执行以下命令(不等待应答)。

  2. 客户端发送请求并等待应答。一旦获得答案,客户端应用程序就会继续执行以下命令。

第二种情况对我来说似乎更现实、更符合逻辑。但是,我不明白如果服务器不打开套接字并且它没有说它不“想要”打开第二个套接字(如果服务器不存在或网络损坏,则可能会发生这种情况)会发生什么。在这种情况下会发生什么?服务器会永远等待吗?

一般来说,客户端最好知道其对套接字的请求的结果是什么。例如,我可以想象以下情况:

  1. 套接字由服务器打开。

  2. 服务器拒绝打开套接字。所以,服务器存在,它收到了客户端的请求,但它说“不”。

  3. 服务器没有响应。

我知道 new Socket(serverIP,serverPort) 不会“返回”此类信息。但它会引发异常。其中之一是“UnkownHostException”。什么时候抛出?当服务器有一段时间没有响应时(多长时间)?

添加:

我刚刚发现抛出UnknownHostException表示无法确定主机的IP地址。因此,它与上述情况无关(服务器没有响应,服务器拒绝打开套接字)。

In the client application I call new Socket(serverIP,serverPort). As a result the client application sends a request to the server application to open a socket. Does it start a new thread? I mean which of the following is true?

  1. Client application sends a request and immediately starts to execute following commands (not waiting for the answer).

  2. Client sends the request and waits for the answer. As soon as the answer is obtained, the client application continues to execute following commands.

The second case seems to be more realistic and logical for me. However, I do not understand what happens if the server does not open a socket and it does not say that it does not "want" to open the second (it can happen if the server does not exist or network is broken). What will happen in this case? Will server wait forever?

In general it would be nice for the client to know what is the result of its request for the socket. For example I can imagine the following situations:

  1. The socket is opened by the server.

  2. The server refuses to open a socket. So, server exists, it got the request from the client but it says "no".

  3. There is no response from the server.

I know that new Socket(serverIP,serverPort) does not "return" this kind of information. But it throws exceptions. One of them is "UnkownHostException". When it is thrown? When the server is not responding for a while (for how long)?

ADDED:

I just found out that UnknownHostException is thrown to indicate that the IP address of a host could not be determined. So, it is unrelated with the above described situations (server is not responding, server refuses to open a socket).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清引 2024-09-01 15:04:40

嗯,我在文档中没有看到任何内容说它创建了一个新线程,所以你的问题的答案是否定的

Well I see nothing in the documentation saying that it creates a new thread, so the answer to your question is NO

荒路情人 2024-09-01 15:04:40

不确定它是否打开新线程,但我知道它会阻塞直到:

  • 成功打开发生
  • 异常
  • 超时

您可以控制超时限制,除此之外,您将不得不等待答案

not sure if it opens new Thread, but i know it blocks until:

  • Successfully opens
  • Exception occurs
  • Timeout

You can control the timeout limit, other than that, you will have to wait for an answer

最近可好 2024-09-01 15:04:40
server is not responding
maybe means before timeout the server dosen't response the request.
server refuses to open a socket
maybe means there hasn't any server process listen listen on this port,the port dosen't open.
server is not responding
maybe means before timeout the server dosen't response the request.
server refuses to open a socket
maybe means there hasn't any server process listen listen on this port,the port dosen't open.
八巷 2024-09-01 15:04:40

该语句

new Socket(ip, port)

不会创建新线程。它只是创建一个客户端套接字对象。

为了发送和接收数据,您必须在客户端套接字上使用 getInputStream/getOutputStream 方法,然后执行读/写操作。 outStream.write() 通常表现为非阻塞调用,而 inStream.read() 则为阻塞调用。

理想情况下,客户端套接字是在单独的线程中创建的,以便主进程可以继续前进,而无需等待与套接字相关的 IO 操作。我建议通过创建一个新线程并将客户端套接字分配给该线程来遵循类似的操作。

The statement

new Socket(ip, port)

doesn't create a new thread. It just creates a client socket object.

For you to send and receive data you have to use getInputStream/getOutputStream methods on the client socket and then do a read/write operation. outStream.write() usually behaves like a non-blocking call whereas inStream.read() is a blocking call.

Ideally, client sockets are created in a separate thread so that the main process would continue to go ahead without waiting for socket related IO operations. I would recommend to follow similar thing by creating a new thread and assigning the client socket to that thread.

贵在坚持 2024-09-01 15:04:40

如果发生什么情况
服务器不打开套接字并且
它并没有说它没有
“想要”打开第二个(它可以
如果服务器不存在或
网络坏了)。会发生什么
在这种情况下?服务器会等待吗
永远?

什么服务器?如果服务器不存在,那么怎么可能永远等待呢?如果网络中断,服务器如何知道连接尝试?

这两种情况都不是“服务器[不想]打开套接字”。

在这两种情况下,服务器都不会永远等待。客户也不会。默认连接超时约为一分钟,取决于平台,但您可以按照其他答案之一中的描述缩短它。

总的来说,这对
客户知道结果是什么
它对套接字的请求。

确实如此。代码要么继续执行,要么抛出 IOException。哪个 IOException 表明出了什么问题。

what happens if
the server does not open a socket and
it does not say that it does not
"want" to open the second (it can
happen if the server does not exist or
network is broken). What will happen
in this case? Will server wait
forever?

What server? If the server does not exist there is no server so how can it wait forever? If the network was broken how would the server ever know about the connection attempt?

Neither of these cases is 'the server [not wanting] to open a socket'.

In neither case will the server wait forever. Neither will the client. There is a default connection timeout of about a minute, platform-dependent, but you can shorten it as described in one of the other answers.

In general it would be nice for the
client to know what is the result of
its request for the socket.

It does. Either the code continues or an IOException is thrown. Which IOException it is indicates what went wrong.

只是偏爱你 2024-09-01 15:04:39

new Socket(host,port) 不会打开新线程。它启动同步 TCP 连接建立。这可能需要几秒钟或在默认超时后超时。

如果服务器拒绝接受连接,或者客户端无法解析主机的 IP 或访问服务器(无法访问),则会抛出适当的异常。

(如果要控制超时,请使用默认构造函数和 connect(SocketAddress endpoint, int timeout) 方法)。

new Socket(host,port) does not open a new thread. it starts a synchronous tcp connection establishment. this may take a few seconds or time out after some default timeout.

if the server refuses to accept the connection, or the client can't resolve the ip of the host or access the server (unreachable) an appropriate exception is thrown.

(if you want to control the timeout use the default constructor and the connect(SocketAddress endpoint, int timeout) method).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文