在服务器和客户端

时间:2016-04-25 07:52:41

标签: java

我正在使用TCPClient类和TCPServer类。我已经在我的TCPClient中创建了一个绘图程序,然后将点(绘图)信息发送到服务器,反之亦然,以使服务器上运行的所有客户端都能在"相同的"画布。

TCPSERVER:

public class TCPServer {

    private static final int serverPort = 9000;

    public static void main(String argv[]) throws Exception {
        ServerSocket welcomeSocket = new ServerSocket(serverPort);

        ArrayList<Point> completeDrawing = new ArrayList<>();
        ArrayList<Point> receivedList = new ArrayList<>();

        while (true) {

            Socket connectionSocket = welcomeSocket.accept();
            ObjectInputStream inFromClient = new ObjectInputStream(connectionSocket.getInputStream());


            ObjectOutputStream outToClient = new ObjectOutputStream(connectionSocket.getOutputStream());

            receivedList.addAll((ArrayList<Point>) inFromClient.readObject());
            completeDrawing.addAll(receivedList);
            receivedList.clear();

            outToClient.writeObject(completeDrawing);

            connectionSocket.close();
        }

    }
}

的TcpClient:

public class TCPClient extends JPanel {

    public static ArrayList<Point> location = new ArrayList<>();

    private JTextArea consoleOutput = new JTextArea(1,20);

    public void addComponentToPane(Container pane) {
        consoleOutput.setEditable(false);
    }

    public TCPClient() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                synchronized (location) {
                location.add(e.getPoint());
                location.notify();
                }
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                synchronized (location) {
                location.add(e.getPoint());
                location.notify();
                repaint();
                }
            }
        });

        setPreferredSize(new Dimension(800, 500));
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        synchronized (location) {
            if(location.isEmpty()){
                return;
            }

            Point p = location.get(0);
            for (int i = 1; i < location.size(); i++) {
                Point q = location.get(i);
                g.drawLine(p.x, p.y, q.x, q.y);
                p = q;
            }
        }
    }

    public static void main(String argv[])  throws Exception {

        InetAddress SERVERIP = InetAddress.getLocalHost();

        JFrame frame = new JFrame("Drawing with friends");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TCPClient(), BorderLayout.CENTER);

        JTextArea IPadress = new JTextArea(1,20);
        IPadress.setEditable(false);
        IPadress.append("DEVICE IP: " + SERVERIP.getHostAddress());
        frame.add(IPadress, BorderLayout.SOUTH);

        frame.setSize(new Dimension(800,600));
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);

        while(true) {
            synchronized (location) {
                Socket clientSocket = new Socket("localhost", 9000);

                ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
                ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

                outToServer.writeObject(location);

                outToServer.flush();
                location.wait();

                location.addAll((ArrayList<Point>) inFromServer.readObject());
            }
        }
    }
}

通过这些课程,我的绘图功能会变得混乱。它开始滞后和口吃很难,而绘图只能从一点开始: enter image description here

但是,如果我将我的服务器注释到客户端流,它可以正常工作(当然只在一个客户端窗口中): enter image description here

我有一个疑问,问题与客户端被数据溢出有关,但除此之外我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

There are too many data on client, because data in 'drawing' arrayLists (on both sides) have a lot duplications. Rewrite code to add only new points to 'location' and 'completeDrawing' lists like:

ArrayList<Point> serverPoints = (ArrayList<Point>)inFromServer.readObject();
for(Point p : serverPoints){
  if(!location.contains(p)) location.add(p);
}