Java:serversocket 和 datagramsocket 有什么区别? [英] Java : what is the difference between serversocket and datagramsocket?

查看:57
本文介绍了Java:serversocket 和 datagramsocket 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我非常关心这些课程的优缺点.

解决方案

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

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

<小时>

补充问题:

<块引用>

基本上什么是数据报

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

<块引用>

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

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

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

<块引用>

无连接是什么意思,

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

<块引用>

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

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

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

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

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

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

<小时>

关于您尝试使用 ServerSocket.

<块引用>

代码正在运行,但我发现 ServerSocket 消耗了太多堆,尽管它也成功地传送到服务器和客户端.

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

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

  1. 为每个客户端/服务器交互打开一个新连接
  2. 在服务器端,为每个连接创建一个新线程
  3. 未关闭连接.

<块引用>

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

  1. 握手不会导致大量内存消耗.
  2. 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:

  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.

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.

  1. Handshakes won't cause significant memory consumption.
  2. TCP/IP stacks don't typically do handshakes by default anyway.

这篇关于Java:serversocket 和 datagramsocket 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆