Socket 和 ServerSocket 有什么区别?
如果Socket
代表客户端,ServerSocket
代表服务器端,为什么Socket.read
从服务器端读取数据?我真的很困惑,你能帮我解释一下吗?
If Socket
represents client side and ServerSocket
represents server side, why Socket.read
reads the data from server side? I'm really confused, Can you please clarify it to me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
(我发布这个答案是因为我总是觉得使逻辑正确很重要。)
我建议您看一下以下示例。
http://docs.oracle.com/javase/tutorial/networking/sockets /clientServer.html
诚然,在进行 TCP/IP 通信时,所有必要的信息都可以由
Socket
类单独提供,以实现通信的唯一目的。无论是在服务器端还是客户端。从上面的链接可以看到,服务器端使用以下代码来获取自己的
Socket
实例。也就是说,在同一服务器本地端口和客户端端口对上创建另一个套接字。然后,服务器使用此
Socket
实例与客户端通信。为了使图片更完整,下面的代码片段显示了 clientt 的
Socket
实例。如果
Socket
已经可以做到这一切,我们为什么还要这样做还需要ServerSocket
吗?这是因为通过 TCP/IP 协议进行通信的工作模式。
当两个程序通过 TCP/IP 进行通信时,通常一个程序会被动监听/等待
,而另一个程序主动监听/等待> 连接到它。所以你可以看到,在沟通的这个
开始阶段
,双方的行为截然不同。因此使用2个不同的类来体现这种差异。Socket
类封装了主动方的行为。 (又名客户端)ServerSocket
类封装被动方(又名服务器)的行为。一旦
ServerSocket
完成其侦听任务并检测到
传入连接时,它将accept()
它并创建一个新的Socket
实例以方便通信。同样,在
java.nio
包中,您将找到ServerSocketChannel
和SocketChannel
类。尽管如此,他们的行为是这样的:所以,在某种程度上,我同意@JohnK,正如他在评论中指出的那样,
这或多或少只是 6 个字母的差异
。(I post this answer because I always feel it's important to make the logic right.)
I suggest you take a look at the following sample.
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Admittedly, when carrying out TCP/IP communication, all the necessary information can be provided by the
Socket
class alone for the sole purpose of communication. No matter it is on server side or client side.As you can see from the above link, server side use the following code to acquire its own
Socket
instance. That is, another socket is created on the same server local port and the client port pair.Then, server use this
Socket
instance to talk to the client.And to make the picture complete, below code snippet shows client's
Socket
instance.So if
Socket
can do it all already, why do we still need theServerSocket
?This is because of the working paradigm of communication over TCP/IP protocol.
When 2 programs talk over TCP/IP, usually one will passively listen/wait on a
<IP:port>
and the other one will actively connect to it.So you can see, at this very
starting phase
of the communication, the 2 sides have very different behaviors. So 2 different classes are used to reflect this difference.Socket
class encapsulates the behavior of the active side. (a.k.a. the client)ServerSocket
class encapsulates the behavior of the passive side (a.k.a. the server)Once the
ServerSocket
accomplished its listening task anddetected
an incoming connection, it willaccept()
it and create a newSocket
instance to facilitate the communication.Similarily, in
java.nio
package, you will findServerSocketChannel
andSocketChannel
classes. And still, they behave like this:So, to some extent, I agree with @JohnK as he pointed out in the comment,
it's more or less just a 6-letter difference
.因为它是通过网络读取服务器发送的数据,并不是直接读取服务器端的数据服务器文件系统或资源(db、ram 或类似的东西)它正在读取已经由 ServerSocket 处理的数据。
将 Socket 视为您的 Web 浏览器,将 ServerSocket 视为远程 Web 服务器。
当您请求图像、页面等时,网络服务器( ServerSocket )将字节写入客户端,反过来客户端必须读取它们(以知道网络服务器发送的内容对吧?)并通过将它们显示到最终来处理它们用户。
ServerSocket/Socket 也发生了同样的情况,但级别较低。套接字从 ServerSocket 中读取信息。
有道理吗?
Because it is reading the data sent by the server through the network, it is not reading directly the server filesystem or resouces ( db , ram or anything like that ) it is reading the data that was already processed by the ServerSocket.
Think about the Socket as your web browser and the ServerSocket as the remote webserver.
When you request an image, page, etc, the webserver ( The ServerSocket ) writes the bytes to the client, in turn the client has to read them ( to know what the webserver sent right? ) and process them by displaying them to the final user.
The same happend with ServerSocket/Socket but at a lower level. The socket reads information from the ServerSocket.
Does it make sense?
首先,让我们澄清一下 IS
Socket
是什么样的:在常见情况下,Socket
是通过:
连接 IP 和端口,例如例如:127.0.0.1:8080
。因此,您决定使用
Socket
制作客户端-服务器应用程序。没有什么太复杂的。以下是关于在客户端
和服务器
之间建立连接的简短说明:客户端
有自己的Socket
并知道服务器
IP 地址和端口。对于服务器
,仅提供ServerSocket
和端口。在这两种情况下,端口都是 0 到 65535 之间的相同数字。因此,我们决定将
客户端
连接到服务器
:client
使用我们服务器
的已知 IP 和端口创建他的Socket clientSocket
对象。server
使用他的ServerSocket.accept()
方法收到传入的连接请求,该方法生成新的Socket newClientSocket
对象(仍在服务器
端(!))。进一步的数据交换通过
clientSocket
和newClientSocket
对象进行(不在clientSocket
和ServerSocket
之间) .这里是了解基本连接过程的近乎完美的图片(请记住,该图片中
Client
上的Socket
对象 - 相同的对象)。完成这个简单的结构后,您需要在
Client.clientSocket
和Server.newClientSocket
端打开两个流来读取和写入信息。First of all, let's clarify what IS
Socket
look like: in a common case,Socket
is a concatenation of IP and port via:
, for example:127.0.0.1:8080
.So, you decided to make client-server application using
Socket
. There's nothing too much complicated. Here's short explanation about making connection betweenclient
andserver
:client
have his ownSocket
and knowsserver
IP address and port. Forserver
there are provided onlyServerSocket
and port. In both cases port are the same number between 0 and 65535.So, we decided to connect our
client
to ourserver
:client
creates hisSocket clientSocket
object with known IP and port of ourserver
.server
got incoming connection request with hisServerSocket.accept()
method, which generates newSocket newClientSocket
object (still on aserver
side (!) ).Further data exchanging goes via
clientSocket
andnewClientSocket
objects (not betweenclientSocket
andServerSocket
).Here is almost perfect picture to understand the basic connection process (keep in mind, that
Socket
object onClient
at this picture - same objects).After you've made this simple structure, you need to open two streams on both
Client.clientSocket
andServer.newClientSocket
sides for reading and writing information.java.net.ServerSocket
java.net.Socket
java.net.ServerSocket
java.net.Socket
ServerSocket 又是一个带有服务器端点附加功能的 Socket。服务器功能包括侦听端口和接受传入连接等......
ServerSocket is again a Socket with additional features of server endpoint. The server features includes listening to the port and accepting an incoming connection etc...
看一下 http://java.sun.com/docs/books /教程/网络/套接字/
Take a look at http://java.sun.com/docs/books/tutorial/networking/sockets/
创建
ServerSocket
是为了绑定
到端口并侦听
来自客户端的连接
。因此,服务器只是等待对话而不启动对话。创建
ClientSocket
是为了连接
到侦听
服务器。客户端发起连接。示例:以入站呼叫中心为例。这些服务就是服务器。他们不会发起呼叫,而是等待客户打来的电话。一旦接到电话,他们就可以进行双向对话。
ServerSocket
is created tobind
to a port andlisten
for aconnect
from a client. So, a server just waits for a conversation and doesn't start one.ClientSocket
is created toconnect
to alisten
ing server. The client initiates the connection.Example: Think of an inbound call center as an example. These services are servers. They don't initiate a call but wait for a call to come in from clients. Once the calls are in, they can engage in a two way conversation.
因为它正在读取服务器发送给您的内容。
Because it's reading what has been sent to you by the server.
Socket
用于客户端,ServerSocket
用于服务器端。Socket
is for the client side andServerSocket
is for the server side.