- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试通过 NIO SocketChannel 读取和写入序列化对象。此 SocketChannel 处于非阻塞模式。我似乎无法找到正确的方法来做到这一点而不破坏流,破坏流头等......
这是我当前的写入方法
private void writeData(SelectionKey key) throws IOException {
Packet outPacket = null;
synchronized (pendingPacketQue) {
for (Packet packet : pendingPacketQue) {
if (packet.getChannel().keyFor(selector).equals(key)) {
outPacket = packet;
break;
}
}
}
if (outPacket == null) {
Logger.writeException("Couldn't find out bound packet in list.", LogType.SERVER);
return;
}
SocketChannel connection = (SocketChannel) outPacket.getChannel();
ObjectOutputStream outStream = new ObjectOutputStream(connection.socket().getOutputStream());
outStream.writeObject(outPacket);
outStream.flush();
outStream.close();
connection.keyFor(selector).interestOps(SelectionKey.OP_READ);
}
这是我当前的阅读方法
private void readData(SelectionKey key) throws IOException, ClassNotFoundException {
SocketChannel connection = (SocketChannel) key.channel();
buffer.clear();
int byteCount;
try {
byteCount = connection.read(buffer);
} catch (IOException e) {
Logger.writeException("Connenction terminated.", LogType.SERVER);
connection.close();
key.cancel();
return;
}
if (byteCount == -1) {
Logger.writeException("Connection error. Terminating connection.", LogType.SERVER);
key.channel().close();
key.cancel();
return;
}
Engine.getInstance().getPacketProcessor().processData(connection, buffer.array(), byteCount);
}
public void processData(SocketChannel connection, byte[] data, int count)
throws IOException, ClassNotFoundException {
ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
ObjectInputStream inStream = new ObjectInputStream(byteStream);
addToQue(inStream.readObject());
inStream.close();
}
如果您有任何疑问,请随时提问。谢谢!
最佳答案
您不必在一次读取中读取整个对象。
你也不能这样写作。您不能使用 SocketChannel
的套接字流在非阻塞模式下。你应该有一个IllegalBlockingModeException
在发件人中。
因此,当您一开始就不可能编写任何内容时,很难看出您如何可能达到损坏的流 header 等地步。
无论如何,我强烈建议您不要尝试这样做。太难了。您需要在对象之前发送对象的大小,以便您知道何时读完所有内容,发出多次读取并保存结果,直到拥有所有内容,运行等等。使用 java.net.Socket
和普通对象直接输入和输出流。
关于java - 如何通过SocketChannel以非阻塞方式读写序列化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37040600/
我知道 SocketChannel 通过在调用 read() 后接收到“-1”来“通知”有序关闭的连接。 但是它如何通知我有关无序关闭的连接呢? (作为整个基于 NIO 的服务器的一部分,使用选择器和
我尝试在两个不同的线程(Android API 级别 25)中同时使用 SocketChannel.write 和 SocketChannel.read。 我将 SocketChannel 配置为阻塞
根据文档,我可以这样做: SocketChannel mySocketChannel = new SocketChannel(SelectorProvider.provider()); 根据 JDK,
我正在用Java开发多线程非阻塞tcp服务器和客户端。我在服务器意识到客户端套接字已关闭时遇到问题。对他来说一切都是开放的。这是代码和我的代码的输出: 服务器: /* * To change thi
我正在编写一个聊天室服务器,它从聊天客户端获取消息并将该消息广播给所有用户。这是《Java 网络编程简介:Java 7 兼容》一书中的一个练习,我正在自学 Java 网络基础知识。我按照书中代码的示例
我构建了一个客户端(SocketChannel),它正在获取大消息(每条消息的大小约为 1MB-2MB)。我怎样才能收到消息?我正在使用选择器。当键为Readable时,我想读取接收消息的所有数据包。
我正在尝试通过 SocketChannel 发送数据(400016 字节)。由于某种原因,并未发送所有数据。 (我希望看到所有 400016 字节都将被发送) 代码如下:公共(public) bool
我编写了一个应用程序,它通过 TCP 和 SocketChannel 连接到服务器但我有两个问题: 第一个是次要的 - 有时出于某种未知的原因我会发送串联的消息, 第二个至关重要 - 应用会定期停止发
假设我们有一个打开的 SocketChannel。在终止应用程序之前明确关闭它很重要吗?换句话说,如果我们不这样做,是否存在留下未关闭的系统资源的风险? 最佳答案 假设您的操作系统是一个现代的多用户操
这是我的代码。从测试服务器,我尝试通过输出流发送数据并从测试客户端接收数据。我使用 SocketChannel 是因为我需要客户端同时监听 3 个端口。目前,我只想从一个套接字读取数据。但是它似乎没有
我再次遇到 Android 套接字编程方面的问题。我的问题是 Selector.select() 返回零,表示没有准备好写入的 SocketChannels。同样的代码同样适用于普通的 Java,但不
我正在使用套接字 channel 和 NIO 概念从客户端读取数据。 Socket Channel 如何知道文件读取完成? ByteBuffer byteBuffer = ByteBuffer.all
我创建了一个到远程服务器的 SocketChannel,以便在 Tomcat 上发送和接收消息。为了从远程计算机接收消息,我使用了一个专用于任务的线程(只有这个线程将从套接字读取,没有其他线程)。 当
我有一个正在运行的套接字,使用选择器。我正在尝试检查我的套接字是否已连接到服务器。 Boolean connected = _channel.isConnected(); 它总是返回真。我关闭了计算机
我想使用 SocketChannel 并为其读/写方法设置超时。我试过为拥有我的 SocketChannel 的套接字设置超时,如下所示: channel.socket().setSoTimeout(
我正在努力理解 SocketChannels , 和 NIO一般来说。我知道如何使用常规套接字以及如何制作一个简单的每个客户端线程服务器(使用常规阻塞套接字)。 所以我的问题: 什么是 SocketC
我刚刚写了一些 NIO 代码,想知道如何对我的实现进行压力测试 SocketChannel.write(ByteBuffer) 无法写入整个字节缓冲区 SocketChannel.read(ByteB
我们有一个带有 https 的服务器,它在端口 443 上运行。我想从服务器读取字节。我正在使用以下代码。 private void openAndConfigureChannel(Selector
这个问题已经有答案了: when I use nio, serverSocket.accept() throws IllegalBlockingModeException (3 个回答) 已关闭 4
我的应用程序有一个队列,其中包含“传出网络数据包”(带有 ByteBuffer 和 SocketChannel 的 POJO),由将数据写入的单个线程使用SocketChannel。 我这样做是为了保
我是一名优秀的程序员,十分优秀!