Socket 和 ServerSocket 有什么区别?

发布于 2024-08-17 04:23:43 字数 120 浏览 5 评论 0原文

如果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 技术交流群。

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

发布评论

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

评论(9

寂寞笑我太脆弱 2024-08-24 04:23:43

(我发布这个答案是因为我总是觉得使逻辑正确很重要。)

我建议您看一下以下示例。

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包中,您将找到ServerSocketChannelSocketChannel类。尽管如此,他们的行为是这样的:

ServerSocketChannel -------------> SocketChannel
                      accept()

所以,在某种程度上,我同意@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.

enter image description here

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.

enter image description here

So if Socket can do it all already, why do we still need the ServerSocket?

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 and detected an incoming connection, it will accept() it and create a new Socket instance to facilitate the communication.

Similarily, in java.nio package, you will find ServerSocketChannel and SocketChannel classes. And still, they behave like this:

ServerSocketChannel -------------> SocketChannel
                      accept()

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.

_蜘蛛 2024-08-24 04:23:43

为什么socket.read要从服务器端读取数据

因为它是通过网络读取服务器发送的数据,并不是直接读取服务器端的数据服务器文件系统或资源(db、ram 或类似的东西)它正在读取已经由 ServerSocket 处理的数据。

将 Socket 视为您的 Web 浏览器,将 ServerSocket 视为远程 Web 服务器。

当您请求图像、页面等时,网络服务器( ServerSocket )将字节写入客户端,反过来客户端必须读取它们(以知道网络服务器发送的内容对吧?)并通过将它们显示到最终来处理它们用户。

ServerSocket/Socket 也发生了同样的情况,但级别较低。套接字从 ServerSocket 中读取信息。

有道理吗?

why socket.read reads the data from serverside

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?

嗳卜坏 2024-08-24 04:23:43

首先,让我们澄清一下 IS Socket 是什么样的:在常见情况下,Socket 是通过 : 连接 IP 和端口,例如例如:127.0.0.1:8080

因此,您决定使用Socket 制作客户端-服务器应用程序。没有什么太复杂的。以下是关于在客户端服务器之间建立连接的简短说明:

  1. 首先,让我们澄清一个事实,我们的客户端有自己的Socket 并知道服务器 IP 地址和端口。对于服务器,仅提供ServerSocket和端口。在这两种情况下,端口都是 0 到 65535 之间的相同数字。
  2. 因此,我们决定将客户端连接到服务器

    • client 使用我们服务器的已知 IP 和端口创建他的 Socket clientSocket 对象。

    • server 使用他的 ServerSocket.accept() 方法收到传入的连接请求,该方法生成新的 Socket newClientSocket 对象(仍在服务器端(!))。

    • 进一步的数据交换通过 clientSocketnewClientSocket 对象进行(不在 clientSocketServerSocket 之间) .

这里是了解基本连接过程的近乎完美的图片(请记住,该图片中 Client 上的 Socket 对象 - 相同的对象)。

完成这个简单的结构后,您需要在 Client.clientSocketServer.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 between client and server:

  1. First of all, let's clarify that fact, that our client have his own Socket and knows server IP address and port. For server there are provided only ServerSocket and port. In both cases port are the same number between 0 and 65535.
  2. So, we decided to connect our client to our server:

    • client creates his Socket clientSocket object with known IP and port of our server.

    • server got incoming connection request with his ServerSocket.accept() method, which generates new Socket newClientSocket object (still on a server side (!) ).

    • Further data exchanging goes via clientSocket and newClientSocket objects (not between clientSocket and ServerSocket).

Here is almost perfect picture to understand the basic connection process (keep in mind, that Socket object on Client at this picture - same objects).

After you've made this simple structure, you need to open two streams on both Client.clientSocket and Server.newClientSocket sides for reading and writing information.

恍梦境° 2024-08-24 04:23:43

java.net.ServerSocket

此类实现服务器套接字。服务器套接字等待
通过网络传入的请求。它执行一些基于操作
响应该请求,然后可能将结果返回给请求者。

java.net.Socket

此类实现客户端套接字(也称为“套接字”)。一个
套接字是两台机器之间通信的端点。

java.net.ServerSocket

This class implements server sockets. A server socket waits for
requests to come in over the network. It performs some operation based
on that request, and then possibly returns a result to the requester.

java.net.Socket

This class implements client sockets (also called just "sockets"). A
socket is an endpoint for communication between two machines.

〗斷ホ乔殘χμё〖 2024-08-24 04:23:43

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...

你爱我像她 2024-08-24 04:23:43

创建 ServerSocket 是为了绑定到端口并侦听来自客户端的连接。因此,服务器只是等待对话而不启动对话。

创建ClientSocket是为了连接侦听服务器。客户端发起连接。

示例:以入站呼叫中心为例。这些服务就是服务器。他们不会发起呼叫,而是等待客户打来的电话。一旦接到电话,他们就可以进行双向对话。

ServerSocket is created to bind to a port and listen for a connect from a client. So, a server just waits for a conversation and doesn't start one.

ClientSocket is created to connect to a listening 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.

萌无敌 2024-08-24 04:23:43

因为它正在读取服务器发送给您的内容。

Because it's reading what has been sent to you by the server.

最偏执的依靠 2024-08-24 04:23:43

Socket 用于客户端,ServerSocket 用于服务器端。

Socket is for the client side and ServerSocket is for the server side.

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