简体   繁体   English

Socket 和 ServerSocket 有什么区别?

[英]What is the difference between Socket and ServerSocket?

If Socket represents client side and ServerSocket represents server side, why Socket.read reads the data from server side?如果Socket代表客户端, ServerSocket代表服务端,那为什么Socket.read从服务端读取数据呢? I'm really confused, Can you please clarify it to me?我真的很困惑,你能帮我澄清一下吗?

(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 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.诚然,在进行 TCP/IP 通信时,所有必要的信息都可以由Socket类单独提供,仅用于通信目的。 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.从上面的链接可以看出,服务器端使用以下代码来获取自己的Socket实例。 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.然后,服务器使用此Socket实例与客户端对话。

And to make the picture complete, below code snippet shows clien t's Socket instance.为了使图片完整,下面的代码片段显示了客户端Socket实例。

在此处输入图片说明

So if Socket can do it all already, why do we still need the ServerSocket ?那么如果Socket已经可以做到这一切,为什么我们还需要ServerSocket呢?

This is because of the working paradigm of communication over TCP/IP protocol.这是因为通过 TCP/IP 协议进行通信的工作范式。

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.当两个程序通过 TCP/IP 通信时,通常一个程序会被动地监听/等待<IP:port> ,而另一个程序会主动连接到它。

So you can see, at this very starting phase of the communication, the 2 sides have very different behaviors.所以你可以看到,在这个沟通的最starting phase ,双方的行为非常不同。 So 2 different classes are used to reflect this difference.因此使用 2 个不同的类来反映这种差异。

  • Socket class encapsulates the behavior of the active side. Socket类封装了主动端的行为。 (aka the client) (又名客户)
  • ServerSocket class encapsulates the behavior of the passive side (aka the server) ServerSocket类封装了被动端(又名服务器)的行为

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.一旦ServerSocket完成其侦听任务并detected传入连接,它将accept()它并创建一个新的Socket实例以促进通信。

Similarily, in java.nio package, you will find ServerSocketChannel and SocketChannel classes.同样,在java.nio包中,您会发现ServerSocketChannelSocketChannel类。 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 .所以,在某种程度上,我同意 @JohnK 在评论中指出的, it's more or less just a 6-letter difference

why socket.read reads the data from serverside为什么 socket.read 从服务器端读取数据

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.因为它正在读取服务器通过网络发送的数据,它不是直接读取服务器文件系统或资源(db、ram 或类似的东西),而是读取已经由 ServerSocket 处理的数据。

Think about the Socket as your web browser and the ServerSocket as the remote webserver.将 Socket 视为您的 Web 浏览器,将 ServerSocket 视为远程 Web 服务器。

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 ServerSocket )将字节写入客户端,反过来客户端必须读取它们(以了解网络服务器正确发送了什么?)并通过将它们显示到最后来处理它们用户。

The same happend with ServerSocket/Socket but at a lower level. ServerSocket/Socket 也发生了同样的情况,但发生在较低级别。 The socket reads information from the ServerSocket.套接字从 ServerSocket读取信息。

Does it make sense?有意义吗?

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 .首先,让我们澄清一下 IS Socket什么样的:在常见情况下, Socket是 IP 和端口的串联,通过: ,例如: 127.0.0.1:8080

So, you decided to make client-server application using Socket .因此,您决定使用Socket制作客户端-服务器应用程序。 There's nothing too much complicated.没有什么太复杂的。 Here's short explanation about making connection between client and server :以下是有关在clientserver之间建立连接的简短说明:

  1. First of all, let's clarify that fact, that our client have his own Socket and knows server IP address and port.首先,让我们澄清一个事实,我们的client有自己的Socket并且知道server IP 地址和端口。 For server there are provided only ServerSocket and port.对于server ,只提供了ServerSocket和端口。 In both cases port are the same number between 0 and 65535.在这两种情况下,端口都是 0 到 65535 之间的相同数字。
  2. So, we decided to connect our client to our server :所以,我们决定将我们的client连接到我们的server

    • client creates his Socket clientSocket object with known IP and port of our server . client使用我们server已知 IP 和端口创建他的Socket clientSocket对象。

    • server got incoming connection request with his ServerSocket.accept() method, which generates new Socket newClientSocket object (still on a server side (!) ). server使用他的ServerSocket.accept()方法收到传入的连接请求,该方法生成新的Socket newClientSocket对象(仍在server端 (!) )。

    • Further data exchanging goes via clientSocket and newClientSocket objects (not between clientSocket and ServerSocket ).进一步的数据交换通过clientSocketnewClientSocket对象进行(不在clientSocketServerSocket之间)。

Here is almost perfect picture to understand the basic connection process (keep in mind, that Socket object on Client at this picture - same objects).是理解基本连接过程的几乎完美的图片(请记住,这张图片中Client上的Socket对象 - 相同的对象)。

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.完成这个简单的结构后,您需要在Client.clientSocketServer.newClientSocket打开两个流来读取和写入信息。

java.net.ServerSocket 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.套接字是两台机器之间通信的端点。

ServerSocket is again a Socket with additional features of server endpoint. ServerSocket 又是一个具有服务器端点附加功能的 Socket。 The server features includes listening to the port and accepting an incoming connection etc...服务器功能包括侦听端口和接受传入连接等...

ServerSocket is created to bind to a port and listen for a connect from a client.创建ServerSocketbind到端口并listen来自客户端的connect So, a server just waits for a conversation and doesn't start one.因此,服务器只等待对话而不启动对话。

ClientSocket is created to connect to a listen ing server.创建ClientSocketconnectlisten服务器。 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.一旦呼叫进入,他们就可以进行双向对话。

因为它是阅读的内容已经服务器发送给您。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM