2009-09-21 92 views
24

我想通過套接字通道發送序列化對象。 我想讓「Hi friend」字符串作爲序列化對象,然後在socket通道中寫入這個對象,而在另一端我想讀取同一個對象並檢索數據。如何在套接字通道中發送和接收序列化對象

我想用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; 
} 
+0

的問題是缺少! – tuergeist 2009-09-21 06:50:07

+0

您的SocketChannel是否已經打開並連接? – tuergeist 2009-09-21 06:56:06

+0

是套接字通道是開放和連接 – 2009-09-22 04:31:05

回答

31

你的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"); 
     } 
    } 
} 

與讀者

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

當你第一次啓動服務器,那麼接收器,你會得到以下輸出:

服務器的控制檯

Sender Start 
Connection ended 

接收器的控制檯

Receiver Start 
String is: 'testtext' 
End Receiver 

這是不是最好的解決辦法,但會根據您的使用Java的ServerSocketChannel

+0

感謝您的幫助 – 2009-09-22 04:36:58

+2

如果您喜歡答案,我會感激您接受它;) – tuergeist 2009-09-22 08:36:26

+0

您是什麼意思的「不是最好的解決方案」。你能再詳述一下嗎?另外,如果你圍繞頻道打包流,你是否會失去NIO的性能?謝謝! – Jeach 2009-12-04 03:54:02

相關問題