Java : what is the difference between serversocket and datagramsocket?(Java:serversocket和datagramsocket有什么区别?)
问题描述
基本上我是 java 服务器和客户端编程的新手,我在谷歌上搜索了所有必要的资源来学习这个特定的主题,但是我不明白它们之间的区别.
到目前为止,我对这两者的理解是它们都可以处理客户端请求,但我需要进一步了解每个类的好处以及我何时可以有效地使用它的特定场景或特定情况.
例如,我有一个服务器客户端程序,它是团队查看器的一个子集,其中客户端程序必须每毫秒将屏幕截图发送到服务器,而服务器将从另一个连接的客户端发布它.该代码正在运行,但我发现 ServerSocket 消耗了很多堆,尽管它也成功地交付给服务器和客户端.我还阅读了与我的问题相关的博客(链接丢失),建议 DatagramSocket 是解决方案,因为它不执行握手.
我真的很关心这些课程的优缺点.
A ServerSocket
用于在某些流协议上接受传入的网络连接;例如TCP/IP.
一个DatagramSocket
用于在一些无连接的数据报/消息协议上发送和接收数据报;例如UDP/IP
补充问题:
<块引用>基本上什么是数据报
数据报是在单个逻辑数据包中发送的一堆信息.例如,UDP 数据包.
<块引用>这是否意味着数据报 = 轻量级数据包?
这取决于你对轻量级的定义!
UDP 数据报作为 IP 数据包发送.如果一个 UDP 数据报对于一个 IP 包来说太大了,它会被发送方分解成多个 IP 包,然后由接收方重新组合.
<块引用>什么是无连接[意思],
表示两方之间不存在逻辑联系.如果 UDP 数据报的组成 IP 包丢失,则该 UDP 数据报丢失.接收者永远不知道(在应用程序级别).在 UDP 中没有数据丢失报告和重试.这是典型的无连接"行为.
<块引用>这是否意味着数据在传输过程中可能会丢失?
基本上,是的.如果您希望在数据报或事件上传输可靠/无损的数据,您应该使用 ServerSocket 和 Socket;例如TCP/IP 流.
但是,请注意,即使使用(裸)TCP/IP 流,也不能保证数据传输:
如果出现网络故障,或者发送方或接收方出现故障,则连接可能会在数据传输过程中中断.这将导致数据丢失......对于该连接.(套接字不支持重新连接.)如果发送者和/或接收者还活着,他们通常会被告知连接已断开,但他们不知道为什么,或者在传输过程中丢失了多少数据.
数据在传输过程中可能以 TCP/IP 错误检测无法发现的方式损坏.接收者不会知道发生了这种情况.
这两个问题都可以在应用协议级别解决;例如第一次使用消息队列,第二次使用强加密和强校验和.
<小时>关于您尝试使用 ServerSocket
.
代码正在运行,但我发现 ServerSocket
消耗了很多堆,尽管它也成功地传递到服务器和客户端.
你做错了什么.如果您适当地使用 API,内存开销应该是微不足道的.
我的猜测是您正在执行以下一项或多项操作:
- 为每个客户端/服务器交互打开一个新连接
- 在服务器端,为每个连接创建一个新线程
- 不关闭连接.
<块引用>
我还阅读了与我的问题相关的博客(链接丢失)建议 DatagramSocket 是解决方案,因为它不执行握手.
- 握手不会导致大量内存消耗.
- 默认情况下,TCP/IP 堆栈通常不会进行握手.
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.
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.
I am really concern of the Benefits and Disadvantage of these classes.
A ServerSocket
is for accepting incoming network connections on some stream protocol; e.g. TCP/IP.
A DatagramSocket
is for sending and receiving datagrams on some connectionless datagram / message protocol; e.g. 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.
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.
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.
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.
However, be aware that even with a (bare) TCP/IP stream, data delivery is not guaranteed:
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.
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
.
The code is working but I found out
ServerSocket
consumes so much Heap although it delivers successfully to the servers and client as well.
You are doing something wrong. If you use the API appropriately the memory overheads should be insignificant.
My guess is that you are doing one or more of the following:
- Opening a new connection for each client / server interaction
- On the server side, creating a new thread for each connection
- Not closing the connections.
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.
- Handshakes won't cause significant memory consumption.
- TCP/IP stacks don't typically do handshakes by default anyway.
这篇关于Java:serversocket和datagramsocket有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java:serversocket和datagramsocket有什么区别?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01