如何在套接字通道中发送和接收序列化对象

Sun*_*hoo 27 java sockets nio

我想通过套接字通道传输序列化对象.我想把"好朋友"字符串作为序列化对象,然后在套接字通道中写入此对象,而在另一端我想读取相同的对象并检索数据.

我想用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;
}
Run Code Online (Sandbox Code Playgroud)

tue*_*ist 36

您的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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

和读者

/*
 * 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

当您第一次启动服务器,然后启动接收器时,您将获得以下输出:

服务器的控制台

Sender Start
Connection ended
Run Code Online (Sandbox Code Playgroud)

接收器的控制台

Receiver Start
String is: 'testtext'
End Receiver
Run Code Online (Sandbox Code Playgroud)

这不是最好的解决方案,但是遵循Java的使用 ServerSocketChannel

  • 如果你喜欢这个答案,如果你接受它,我会很感激;) (2认同)