通过服务器将BufferedImage发送到另一个Java客户端

时间:2014-11-06 00:21:34

标签: java sockets server

我试图通过套接字服务器将BufferedImage发送到另一个客户端。我发布下面的代码。当我运行服务器并使用发送客户端连接到服务器并接收客户端时,一切都就在那里。服务器甚至不应该接收任何东西,除非它已经打印出“名称正在尝试连接到:”,它不会只是坐在那里。我不知道为什么它什么都不做。

发送: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();
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

必须序列化BufferedImage。您可以将图像转换为字节数组,一旦读取字节数组,就将其转换回图像。

答案 1 :(得分:1)

发件人在使用NotSerializableException致电writeObject()时无疑会获得BufferedImage,因为BufferedImage并未实施Serializable.因此您可以&# 39;从readObject()电话中获得一个。您必须将BufferedImage转换为发送字节,并在接收时再转回。如果有一种方法,请查看javax.imageio