Java:serversocket 和 datagramsocket 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27730330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:18:14  来源:igfitidea点击:

Java : what is the difference between serversocket and datagramsocket?

javatcpudp

提问by neferpitou

Basically I am new to server and client programming in java , I google all the necessary resources to learn from this particular topic however I did not understand the difference between them.

基本上我是 Java 服务器和客户端编程的新手,我在谷歌上搜索了所有必要的资源以从这个特定主题中学习,但是我不明白它们之间的区别。

What I Understand so far for these two is that Both of them can Handle Client Request, but I need to further know the benefits of each Class and what particular scenario or specific case where when can I used it efficiently.

到目前为止,我对这两个的理解是它们都可以处理客户端请求,但是我需要进一步了解每个类的好处以及什么特定场景或特定情况下我何时可以有效地使用它。

Like for instance , I have a Server Client Program which is a subset of team-viewer in which The client program must send Screenshot to the server in every millisecond while the server is going to publish it from another connected client. The code is working but I found out ServerSocket consumes so much Heap although it delivers successfully to the servers and client as well. I also read a blog (The link is missing) that is related to my problem suggested that DatagramSocket is the solution because it does not execute Handshakes.

例如,我有一个服务器客户端程序,它是团队查看器的一个子集,其中客户端程序必须每毫秒将屏幕截图发送到服务器,而服务器将从另一个连接的客户端发布它。该代码正在运行,但我发现 ServerSocket 消耗了如此多的堆,尽管它也成功地传送到服务器和客户端。我还阅读了与我的问题相关的博客(链接丢失),建议 DatagramSocket 是解决方案,因为它不执行握手。

I am really concern of the Benefits and Disadvantage of these classes.

我真的很关心这些课程的优缺点。

回答by Stephen C

A ServerSocketis for accepting incoming network connections on some stream protocol; e.g. TCP/IP.

AServerSocket用于接受某些流协议上的传入网络连接;例如 TCP/IP。

A DatagramSocketis for sending and receiving datagrams on some connectionless datagram / message protocol; e.g. UDP/IP

ADatagramSocket用于在一些无连接数据报/消息协议上发送和接收数据报;例如 UDP/IP



Supplementary questions:

补充问题:

Basically what is a datagram

基本上什么是数据报

A datagram is bunch of information sent in a single logical packet. For example, a UDP packet.

数据报是在单个逻辑数据包中发送的一堆信息。例如,UDP 数据包。

and does this mean datagram = lightweight packets ?

这是否意味着数据报 = 轻量级数据包?

It depends on your definition of lightweight!

这取决于你对轻量级的定义!

UDP datagrams are sent as IP packets. If a UDP datagram is too big for an IP packet, it is broken into multiple IP packets by the sender and reassembled by the receiver.

UDP 数据报作为 IP 数据包发送。如果 UDP 数据报对于 IP 数据包来说太大,它会被发送方分解为多个 IP 数据包,并由接收方重新组装。

and what does connectionless [mean],

什么是无连接 [意味着],

It means that no logical connection exists between the 2 parties. If a component IP packet of a UDP datagram is lost, the UDP datagram is lost. The receiver never knows (at the application level). There is no reporting of data loss and no retrying in UDP. This is typical "connectionless" behavior.

这意味着 2 方之间不存在逻辑联系。如果 UDP 数据报的一个组件 IP 包丢失,则 UDP 数据报丢失。接收者永远不知道(在应用程序级别)。UDP 中没有数据丢失报告和重试。这是典型的“无连接”行为。

does it mean Data might get lost during transmission?

这是否意味着数据在传输过程中可能会丢失?

Basically, yes. If you want reliable / lossless data transmissin the event that a datagram or on you should use ServerSocket and Socket; e.g. TCP/IP streams.

基本上,是的。如果你想要可靠/无损的数据传输,你应该使用 ServerSocket 和 Socket;例如 TCP/IP 流。

However, be aware that even with a (bare) TCP/IP stream, data delivery is not guaranteed:

但是,请注意,即使使用(裸)TCP/IP 流,也无法保证数据传输:

  • If there is a network failure, or if either the sender or receiver has a failure, then a connection can be broken while data is in transit. That will result in data loss ... for that connection. (Sockets do not support reconnecting.) If the sender and/or receiver are still alive they will typically be informed that the connection has been broken, but they won't know why, or how much data was lost in transit.

  • It is possible for data to be corrupted in transit in ways that TCP/IP's error detection cannot spot. The receiver won't know this has happened.

  • 如果出现网络故障,或者发送方或接收方出现故障,则在数据传输过程中连接可能会中断。这将导致数据丢失......对于该连接。(套接字不支持重新连接。)如果发送方和/或接收方还活着,他们通常会被告知连接已断开,但他们不知道为什么,或者在传输过程中丢失了多少数据。

  • 数据在传输过程中可能会以 TCP/IP 错误检测无法发现的方式损坏。接收者不会知道这已经发生了。

Both of these issues can be addressed at the application protocol level; e.g. using message queues for the first and strong encryption and strong checksumming for the second.

这两个问题都可以在应用程序协议级别解决;例如,第一个使用消息队列,第二个使用强加密和强校验和。



Concerning your attempt to use ServerSocket.

关于您尝试使用ServerSocket.

The code is working but I found out ServerSocketconsumes so much Heap although it delivers successfully to the servers and client as well.

该代码正在运行,但我发现ServerSocket虽然它也成功地交付给服务器和客户端,但它消耗了如此多的堆。

You are doing something wrong. If you use the API appropriately the memory overheads should be insignificant.

你做错了什么。如果您适当地使用 API,内存开销应该是微不足道的。

My guess is that you are doing one or more of the following:

我的猜测是您正在执行以下一项或多项操作:

  1. Opening a new connection for each client / server interaction
  2. On the server side, creating a new thread for each connection
  3. Not closing the connections.
  1. 为每个客户端/服务器交互打开一个新连接
  2. 在服务器端,为每个连接创建一个新线程
  3. 不关闭连接。

I also read a blog (The link is missing) that is related to my problem suggested that DatagramSocket is the solution because it does not execute Handshakes.

我还阅读了与我的问题相关的博客(链接丢失),建议 DatagramSocket 是解决方案,因为它不执行握手。

  1. Handshakes won't cause significant memory consumption.
  2. TCP/IP stacks don't typically do handshakes by default anyway.
  1. 握手不会导致大量内存消耗。
  2. 默认情况下,TCP/IP 堆栈通常不会进行握手。

回答by u4370109

You say you have looked on google, but there are several pages on google that address your question directly. There are several that have the same title as your question. You have even indicated that you understand some of the difference between them by using the [tcp] and [udp] tags on your question.

你说你在谷歌上看过,但谷歌上有几个页面可以直接解决你的问题。有几个标题与您的问题相同。您甚至通过在问题中使用 [tcp] 和 [udp] 标签表示您了解它们之间的一些区别。

The difference is one uses TCP communication protocol and one uses the UDP communication protocol. Perhaps your question is not one about Java but about how the internet, computer networking, and the communication protocols work?

区别是一种使用TCP通讯协议,一种使用UDP通讯协议。也许您的问题不是关于 Java,而是关于互联网、计算机网络和通信协议的工作原理?

  • TCP is a connection oriented reliable delivery protocol.
  • UDP is a connectionless unreliable delivery protocol.
  • TCP 是一种面向连接的可靠传输协议。
  • UDP 是一种无连接的不可靠传输协议。

What this means is you have to decide which is important, speed or reliability.

这意味着您必须决定哪个重要,速度还是可靠性。

Is the data such that it must not be corrupted in transit? If it is then you must use TCP or a serversocket.

数据是否在传输过程中不会被破坏?如果是,那么您必须使用 TCP 或服务器套接字。

If the data must arrive by the fastest method, even at risk of getting lost, then you must use UDP or a datagramsocket.

如果数据必须以最快的方式到达,即使有丢失的风险,那么您必须使用 UDP 或数据报套接字。

If you need more explanation to understand you should take a course on computer networking.

如果您需要更多解释才能理解,您应该参加计算机网络课程。