2014-11-06 146 views
0

我試圖通過套接字服務器將BufferedImage發送到另一個客戶端。生病發布我的代碼如下。當我運行服務器並通過發送客戶端連接到服務器並接收客戶端時,所有內容都位於此處。服務器甚至不應該收到任何東西,除非它已經打印出「名稱試圖連接到:」,它不是它只是坐在那裏。我不知道爲什麼它什麼都不做。通過服務器發送BufferedImage到另一個Java客戶端

客戶端發送:http://pastebin.com/X4z55Hdp

客戶端接收:http://pastebin.com/MB9qEyGy發送和臨危

服務器源:

package core; 

import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.PrintWriter; 
import java.net.Socket; 

import utilities.Tools; 

public class Node implements Runnable { 

private String name; 
private Socket socket; 
private boolean isApp; 

public Node(Socket s, String name) { 
    this.setName(name); 
    this.setSocket(s); 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Socket getSocket() { 
    return socket; 
} 

public void setSocket(Socket socket) { 
    this.socket = socket; 
} 

public void run() { 
    while (true) { 
     try { 
      BufferedReader in = new BufferedReader(new InputStreamReader(
        socket.getInputStream())); 
      PrintWriter out = new PrintWriter(socket.getOutputStream(), 
        true); 
      if (in.readLine() != null) { 
       // Tools.log("[INPUT] " + in.readLine()); 
       String i = in.readLine(); 
       if (i.contains("set name ")) { 
        String n = i.replace("set name ", ""); 
        Tools.log("Changing " + name + " to " + n); 
        this.name = n; 
        if (n.contains("_app")) { 
         this.isApp = true; 
        } 
       } else { 
        String toFind = name + "_app"; 
        if (isApp) 
         toFind = name.replace("_app", ""); 
        Tools.log(name + " is attempting to connect to: " 
          + toFind); 
        for (Node n : Server.nodes) { 
         if (n.getName().equals(toFind)) { 
          Tools.log(n.getName() + " found, sending data"); 
          ObjectOutputStream outToNode = new ObjectOutputStream(
            n.getSocket().getOutputStream()); 
          ObjectInputStream inFromClient = new ObjectInputStream(
            socket.getInputStream()); 
          BufferedImage img = (BufferedImage) inFromClient 
            .readObject(); 
          if (img != null) { 
           outToNode.writeObject(img); 
          } 
         } 
        } 
       } 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
} 
+1

你不能在2個不同的包裝器(BufferedInput和ObjectInput)中使用你的流。如果在解決該問題後仍然出現此錯誤,請發佈一個[MVCE](http://stackoverflow.com/help/mcve)來複制問題 – 2014-11-06 00:28:16

回答

0

的BufferedImage必須序列化。您可以將圖像轉換爲字節數組,並在讀取字節數組後將其轉換回圖像。

0

發送者無疑是得到一個NotSerializableException調用writeObject()BufferedImage時,因爲BufferedImage沒有實現Serializable.因此你不能得到一個從readObject()通話要麼。您必須將BufferedImage轉換爲發送字節,並在接收時再次轉回。看看javax.imageio的一種方法來做到這一點。

相關問題