2009-09-21 11 views
24

Desidero trasmettere un oggetto serializzato su un canale socket. Voglio rendere la stringa "Ciao amico" come oggetto serializzato e quindi scrivere questo oggetto nel canale socket mentre nell'altra estremità voglio leggere lo stesso oggetto e recuperare i dati.Come inviare e ricevere oggetto serializzato nel canale socket

Tutte queste cose che voglio fare utilizzando Java SocketChannel. Come fare questo? Ho provato come sotto, ma non ho ricevuto alcun dato nel lato del 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; 
} 
+0

La domanda di Java manca! – tuergeist

+0

Il tuo SocketChannel è già aperto e connesso? – tuergeist

+0

sì il canale socket è aperto e connesso –

risposta

31

tuo movimentazione SocketChannel sembra essere incompleta, vedere questo esempio completo per SocketChannels trasferimento di 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"); 
    } 
} 

Al primo avvio del server , quindi il ricevitore, riceverai il seguente risultato:

console del server

Sender Start 
Connection ended 

console del ricevitore

Receiver Start 
String is: 'testtext' 
End Receiver 

questa non è la soluzione migliore, ma segue l'utilizzo del ServerSocketChannel

+0

Grazie per il vostro aiuto –

+2

Se vi piace la risposta, sarei grato se lo accettaste;) – tuergeist

+0

Cosa intendete per "non la soluzione migliore". Potresti elaborarne ancora? Inoltre, se avvolgi un flusso attorno a un canale, perdi le prestazioni NIO? Grazie! – Jeach

Problemi correlati