为什么Socket使用流,而NIO SocketChannel使用读/写? [英] Why does Socket use streams but NIO SocketChannel use read/write?

查看:248
本文介绍了为什么Socket使用流,而NIO SocketChannel使用读/写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个非阻塞的客户端/服务器,我注意到该客户端/服务器的Socket提供了getInputStream()getOutputStream(),但是SocketChannel提供了read()write().

I'm experimenting with writing a non-blocking client/server of which I've noticed that Socket provides getInputStream() and getOutputStream() but SocketChannel provides read() and write().

以下所有问题均与之相关,旨在更好地了解这种情况:

The following questions are all related and is an effort to better understand the situation:

  • 为什么这些对象在提供数据包数据的方式上有所不同?
  • 这是阻塞与非阻塞的副作用,还是只是不同的 实施中?
  • 在决定如何从套接字读取数据时,是否需要考虑这一点?
  • 在任何一种实现中,缓冲区结束时间都比预期的早吗?我可以收到不完整的数据包吗?
  • Why do these objects differ in how the packet data is provided?
  • Is this a side-effect of blocking vs non-blocking or simply a differing in implementation?
  • Is this something I need to consider while deciding how to read from the socket?
  • Can I receive an incomplete packet where the buffer ends earlier than expected in either implementation?

推荐答案

为什么这些对象在提供数据包数据的方式上有所不同?

Why do these objects differ in how the packet data is provided?

因为否则它们将是相同的. NIO的设计师认为他们必须将数据,位置,容量和限制封装到一个类中.

Because otherwise they would be the same. The designers of NIO felt that they had to encapsulate data, position, capacity, and limit into a single class.

这是阻塞与非阻塞的副作用,还是只是实现方式有所不同?

Is this a side-effect of blocking vs non-blocking or simply a differing in implementation?

不,是.

在决定如何从套接字读取数据时,是否需要考虑这一点?

Is this something I need to consider while deciding how to read from the socket?

当然.

在任何一种实现中,缓冲区结束时间都比预期的早吗?我可以收到不完整的数据包吗?

Can I receive an incomplete packet where the buffer ends earlier than expected in either implementation?

是的.这是TCP/IP的属性,而不是Java的属性.

Yes. This is a property of TCP/IP, not of Java.

这篇关于为什么Socket使用流,而NIO SocketChannel使用读/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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