Domanda

Voglio trasmettere un oggetto serializzato su un canale socket.Voglio creare la stringa "Ciao amico" come oggetto serializzato e quindi scrivere questo oggetto nel canale socket mentre dall'altra parte voglio leggere lo stesso oggetto e recuperare i dati.

Tutte queste cose che voglio fare usando Java SocketChannel.Come fare questo?Ho provato come indicato di seguito, ma non ho ricevuto alcun dato dal lato destinatario.

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;
}
È stato utile?

Soluzione

La gestione di SocketChannel sembra essere incompleta, vedi questo completare esempio per SocketChannels che trasferisce un byte:

/*
 * 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");
        }
    }
}

E il Lettore

/*
 * 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");
    }
}

Quando avvii per la prima volta il Server, quindi il Ricevitore, otterrai il seguente output:

La console del server

Sender Start
Connection ended

Consolle del ricevitore

Receiver Start
String is: 'testtext'
End Receiver

Questa non è la soluzione migliore, ma segue l'utilizzo di Java ServerSocketChannel

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top