Java Socket和ServerSocket在使用端口上的区别 [英] Java the difference of Socket and ServerSocket in using port

查看:84
本文介绍了Java Socket和ServerSocket在使用端口上的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器端,我们使用

Socket server = serverSocket.accept();

创建一个套接字.创建套接字后,我们可以创建一个新线程来处理该套接字的输入/输出流.因此,如果有进一步的连接请求进来,我们可以返回在同一端口上侦听并创建新套接字.由于我们已经在特定端口创建了 ServerSocket,当然我们不能再次在该端口创建另一个 ServerSocket.

to create a socket. After the socket is created, we can create a new thread to handle the input/output stream of that socket. So we can go back to listening at the same port and create new socket if there are further connection requests come in. Since we already created ServerSocket at a specific port, of course we could not create another ServerSocket at that port again.

所以根据我的理解,我可以得出结论,在服务器端,我们可以在一个端口下创建多个套接字吗?(类似于网络服务器所做的)

So from my understanding, can I conclude that, at server side, we can create multiple sockets under one port? (similar to what web server does)

实际上我的问题是,在客户端,当我们创建套接字时,我们可以指定要使用的本地端口.在该本地端口成功创建客户端套接字后,我们可以将该端口重用于其他客户端套接字吗?该端口是否永久绑定到套接字直到套接字关闭(或端口关闭)?既然客户端没有侦听"的概念,那我们能不能做和ServerSocket一样的事情(参考ServerSocket可以在一个端口下创建多个socket)?

Actually my question is, at client side, when we are creating a socket, we can specify the local port that we want to use. After we have successful created a client socket at that local port, can we reuse that port for other client socket? Does that port bind to the socket permenantly until the socket is closed (or port close)? Since there is no "Listening" concept at client side, are we able to do the same thing as ServerSocket does (refer to ServerSocket can create multiple socket under one port)?

我很困惑客户端如何处理端口和套接字,因为我将 ServerSocket 与客户端套接字进行比较.

I am seriously confused how client side handle the port and socket, because I am comparing ServerSocket with the client socket.

请指出正确的方向,我知道我的想法在某种程度上是错误的.非常感谢.

Please point me to the correct direction, I know that my thinking somehow is wrong. Thanks very much.

推荐答案

所以根据我的理解,我可以得出结论,在服务器端,我们可以在一个端口下创建多个套接字吗?(类似于网络服务器所做的)

So from my understanding, can I conclude that, at server side, we can create multiple sockets under one port? (similar to what web server does)

您对自己的术语感到困惑.ServerSocket.accept() 接受一个连接并将端点包装在一个Socket中.根据 ServerSocket 相同的本地端口号>RFC 793,因此包装 Socket 也是如此.

You're confusing yourself with your terminology. ServerSocket.accept() accepts a connection, and wraps the endpoint in a Socket. The endpoint has the same local port number as the ServerSocket, by definition as per RFC 793, and therefore so does the wrapping Socket.

实际上我的问题是,在客户端,当我们创建套接字时,我们可以指定要使用的本地端口.

Actually my question is, at client side, when we are creating a socket, we can specify the local port that we want to use.

我们可以,但我们很少这样做.

We can, but we rarely if ever do so.

在本地端口成功创建客户端套接字后,我们可以将该端口重用于其他客户端套接字吗?

After we have successful created a client socket at that local port, can we reuse that port for other client socket?

没有

该端口是否永久绑定到套接字直到套接字关闭(或端口关闭)?

Does that port bind to the socket permenantly until the socket is closed (or port close)?

是的,或者相反:套接字绑定到端口.

Yes, or rather the other way round: the socket is bound to the port.

既然客户端没有Listening"的概念,那么我们能不能做和ServerSocket一样的事情(参考ServerSocket可以在一个端口下创建多个socket)?

Since there is no "Listening" concept at client side, are we able to do the same thing as ServerSocket does (refer to ServerSocket can create multiple socket under one port)?

没有

这篇关于Java Socket和ServerSocket在使用端口上的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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