Server Client发送对象

时间:2012-09-06 18:58:34

标签: java swing

我想在客户端和服务器之间来回发送一个类。想要从客户端运行Panel类。请任何人都可以告诉我该怎么办?

客户等级

import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Client
{
    private static Socket socket = null;
    public static void main (String args[]) throws IOException, ClassNotFoundException, EOFException
    {
        try {
            socket = new Socket("localhost", 4444);
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        socket.close();
    }
}

当我想从客户端读取面板对象时,会出现一个错误,指出找不到主类。如何从客户端访问面板类???

服务器类

import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Server {
    public static void main(String[] args) throws IOException, ClassNotFoundException{
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(4444);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 4444.");
        System.exit(-1);
    }
    Socket socketOut = serverSocket.accept();
    ObjectOutputStream oos = new ObjectOutputStream(socketOut.getOutputStream()) ;
    socketOut.close();
    serverSocket.close();
 }
}

小组类

import javax.swing.*;
import java.awt.*;

public class Panel extends JPanel {
    public Panel()
    {
        JTextField n = new JTextField(10);
        n.setText("Hello");
        JButton q = new JButton("Who are you?");
        setSize(300,300);
        add(n);
        add(q);
        setLayout (new FlowLayout());
   }
}

2 个答案:

答案 0 :(得分:0)

要通过套接字在不同进程之间发送对象,需要序列化对象。因此Panel类需要实现Serializable接口:http://java.sun.com/developer/technicalArticles/Programming/serialization/

至于主要类没找到的东西。我假设这两个类必须是两个单独的进程。在这种情况下,您可能会发现如果您在IDE中构建它,主类定义是两个项目的服务器。您可能需要更正此问题。确保有两个单独的构建结果和两个单独的主类。

答案 1 :(得分:0)

服务器类:

import java.net.*;
import java.io.*;

class ServerThread extends Thread {
    private Socket socket = null;

    public ServerThread(Socket socket) {
    super("ServerThread");
    this.socket = socket;
    }

    public void run() {

    try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
            EagleEye y = new EagleEye();
            Object object = y;
            oos.writeObject(object);
            oos.flush();
            oos.close();
            socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        while (listening)
        new ServerThread(serverSocket.accept()).start();

        serverSocket.close();
    }
}

客户类:

public class Client {
    private static Socket socket = null;
    public static void main (String args[]) throws IOException, ClassNotFoundException, EOFException{
    try {
            socket = new Socket("localhost", 4444);
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: localhost");
            System.exit(1);
        }
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        EagleEye y = (EagleEye)ois.readObject();
        socket.close();
    }
}

EagleEye(普通Jframe类):

public class EagleEye extends JFrame implements Serializable {
    private JLabel label1;
    private Icon photo;

    public EagleEye(){
        super("EAGLE EYE");
        photo= new ImageIcon( getClass().getResource( "Untitled.jpg" ) );
        label1= new JLabel();
        label1.setIcon(photo);
        label1.setBounds(0, 0, 800, 600);
        add(label1);

        label1.addMouseListener(new MouseAdapter(){  
            public void mousePressed(MouseEvent e)  
            {
                int x= e.getX();
                int y=e.getY();
                System.out.println();
                if(x>120 && x<150 && y>80 && y<110){
                    drawCircle(667,280);
                }
                if(x>174 && x<214 && y>125 && y<170){
                    drawCircle(667,280);
                }
                if(x>250 && x<270 && y>150 && y<240){
                    drawCircle(667,320);
                }
                if(x>375 && x<402 && y>75 && y<112){
                    drawCircle(670,359);
                }
                if(x>440 && x<490 && y>180 && y<215){
                    drawCircle(675,397);
                }
                if(x>535 && x<615 && y>110 && y<140){
                    drawCircle(670,437);
                }
                removeMouseListener(this);
            }  
        });

        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }

    public void drawCircle(int x, int y) {
        Graphics g = getGraphics();
        g.setColor(Color.RED);
        g.drawOval(x,y,70,30);
    }
    public static void main(String[] args){
        EagleEye e= new EagleEye();
    }
}