Java 如何在套接字通道中发送和接收序列化对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1453028/
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
How to send and receive serialized object in socket channel
提问by Sunil Kumar Sahoo
I want to transmit a serialized object over a socket channel. I want make "Hi friend" string as serialized object and then write this object in socket channel while in the other end i want to read the same object and retrieve the data.
我想通过套接字通道传输序列化对象。我想让“嗨朋友”字符串作为序列化对象,然后在套接字通道中写入这个对象,而在另一端我想读取同一个对象并检索数据。
All these things I want to do using Java SocketChannel
. How to do this?
I have tried like below, but did not get any data in the recipient side.
我想用 Java 做所有这些事情SocketChannel
。这该怎么做?我尝试过如下所示,但在接收方没有得到任何数据。
private static void writeObject(Object obj, SelectionKey selectionKey) {
ObjectOutputStream oos;
try {
SocketChannel channel = (SocketChannel) selectionKey.channel();
oos = new ObjectOutputStream(Channels.newOutputStream(channel));
oos.writeObject(obj);
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static Object readObject(SelectionKey selectionKey) {
ObjectInputStream ois;
Object obj = new Object();
SocketChannel channel = (SocketChannel) selectionKey.channel();
try {
ois = new ObjectInputStream(Channels.newInputStream(channel));
obj = ois.readObject();
} catch (Exception ex) {
ex.printStackTrace();
}
return obj;
}
采纳答案by tuergeist
Your SocketChannel handling seems to be incomplete, see this completeexample for SocketChannels transferring a byte:
您的 SocketChannel 处理似乎不完整,请参阅此SocketChannels 传输字节的完整示例:
/*
* Writer
*/
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class Sender {
public static void main(String[] args) throws IOException {
System.out.println("Sender Start");
ServerSocketChannel ssChannel = ServerSocketChannel.open();
ssChannel.configureBlocking(true);
int port = 12345;
ssChannel.socket().bind(new InetSocketAddress(port));
String obj ="testtext";
while (true) {
SocketChannel sChannel = ssChannel.accept();
ObjectOutputStream oos = new
ObjectOutputStream(sChannel.socket().getOutputStream());
oos.writeObject(obj);
oos.close();
System.out.println("Connection ended");
}
}
}
And the Reader
和读者
/*
* Reader
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
public class Receiver {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
System.out.println("Receiver Start");
SocketChannel sChannel = SocketChannel.open();
sChannel.configureBlocking(true);
if (sChannel.connect(new InetSocketAddress("localhost", 12345))) {
ObjectInputStream ois =
new ObjectInputStream(sChannel.socket().getInputStream());
String s = (String)ois.readObject();
System.out.println("String is: '" + s + "'");
}
System.out.println("End Receiver");
}
}
When you first start the Server, then the Receiver, you'll get the following output:
当您首先启动服务器,然后是接收器时,您将获得以下输出:
Server's console
服务器的控制台
Sender Start
Connection ended
Receiver's console
接收器控制台
Receiver Start
String is: 'testtext'
End Receiver
This is not the best solution, but follows your use of Java's ServerSocketChannel
这不是最好的解决方案,而是遵循您对 Java 的使用 ServerSocketChannel