线程“main”中的异常java.net.SocketException:java.net.SocketInputStream.read中的连接重置(未知源)

时间:2016-04-20 12:31:37

标签: java sockets

我在绘图程序中遇到了这个恼人的错误。我通过绘制线在ArrayList中创建点,然后将其发送到我的服务器。但是,我收到此错误。这不是很好的,但是当我不画画时,我会暂停,所以用户不会向服务器发送任何积分。

我的课程:

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) {
                    location.add(e.getPoint());
                }
            });

            addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    location.add(e.getPoint());
                    repaint();
                }
            });
            setPreferredSize(new Dimension(800, 500));
            setBackground(Color.WHITE);
        }

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

            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);

                try {
                    while(true) {
                        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
                        Socket clientSocket = new Socket("localhost", 9000);

                        ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());

                        ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

                        outToServer.writeObject(location);

                        outToServer.flush();
                        clientSocket.close();

                        Thread.sleep(1000);
                    } 
                } catch (SocketException e) {
            }
        }
    }

控制台:

Received: []
Received: [java.awt.Point[x=54,y=127], java.awt.Point[x=55,y=127], java.awt.Point[x=56,y=127], java.awt.Point[x=57,y=127], java.awt.Point[x=58,y=127], java.awt.Point[x=60,y=127], java.awt.Point[x=61,y=127], java.awt.Point[x=64,y=127], java.awt.Point[x=66,y=127], java.awt.Point[x=69,y=127], java.awt.Point[x=73,y=127], java.awt.Point[x=76,y=126], java.awt.Point[x=80,y=126], java.awt.Point[x=84,y=125], java.awt.Point[x=89,y=125], java.awt.Point[x=94,y=124], java.awt.Point[x=99,y=124], java.awt.Point[x=105,y=124], java.awt.Point[x=110,y=124], java.awt.Point[x=115,y=125], java.awt.Point[x=121,y=125], java.awt.Point[x=126,y=126], java.awt.Point[x=131,y=127], java.awt.Point[x=136,y=127], java.awt.Point[x=140,y=128], java.awt.Point[x=144,y=129], java.awt.Point[x=147,y=130], java.awt.Point[x=150,y=130], java.awt.Point[x=153,y=131], java.awt.Point[x=156,y=131], java.awt.Point[x=159,y=132], java.awt.Point[x=162,y=132], java.awt.Point[x=164,y=132]]
Received: [java.awt.Point[x=54,y=127], java.awt.Point[x=55,y=127], java.awt.Point[x=56,y=127], java.awt.Point[x=57,y=127], java.awt.Point[x=58,y=127], java.awt.Point[x=60,y=127], java.awt.Point[x=61,y=127], java.awt.Point[x=64,y=127], java.awt.Point[x=66,y=127], java.awt.Point[x=69,y=127], java.awt.Point[x=73,y=127], java.awt.Point[x=76,y=126], java.awt.Point[x=80,y=126], java.awt.Point[x=84,y=125], java.awt.Point[x=89,y=125], java.awt.Point[x=94,y=124], java.awt.Point[x=99,y=124], java.awt.Point[x=105,y=124], java.awt.Point[x=110,y=124], java.awt.Point[x=115,y=125], java.awt.Point[x=121,y=125], java.awt.Point[x=126,y=126], java.awt.Point[x=131,y=127], java.awt.Point[x=136,y=127], java.awt.Point[x=140,y=128], java.awt.Point[x=144,y=129], java.awt.Point[x=147,y=130], java.awt.Point[x=150,y=130], java.awt.Point[x=153,y=131], java.awt.Point[x=156,y=131], java.awt.Point[x=159,y=132], java.awt.Point[x=162,y=132], java.awt.Point[x=164,y=132], java.awt.Point[x=166,y=133], java.awt.Point[x=168,y=133], java.awt.Point[x=169,y=133], java.awt.Point[x=170,y=133], java.awt.Point[x=172,y=134], java.awt.Point[x=173,y=134], java.awt.Point[x=174,y=134], java.awt.Point[x=175,y=134], java.awt.Point[x=177,y=134], java.awt.Point[x=178,y=134], java.awt.Point[x=179,y=134], java.awt.Point[x=181,y=134], java.awt.Point[x=182,y=134], java.awt.Point[x=184,y=134], java.awt.Point[x=185,y=134], java.awt.Point[x=186,y=134], java.awt.Point[x=188,y=134], java.awt.Point[x=190,y=134], java.awt.Point[x=191,y=134], java.awt.Point[x=193,y=134], java.awt.Point[x=195,y=135], java.awt.Point[x=197,y=135], java.awt.Point[x=199,y=135], java.awt.Point[x=201,y=136], java.awt.Point[x=203,y=136], java.awt.Point[x=204,y=137], java.awt.Point[x=206,y=137], java.awt.Point[x=208,y=138], java.awt.Point[x=210,y=139], java.awt.Point[x=211,y=139], java.awt.Point[x=212,y=139], java.awt.Point[x=212,y=140], java.awt.Point[x=213,y=140], java.awt.Point[x=214,y=141], java.awt.Point[x=215,y=141], java.awt.Point[x=215,y=142], java.awt.Point[x=216,y=142], java.awt.Point[x=216,y=143], java.awt.Point[x=217,y=144], java.awt.Point[x=218,y=145], java.awt.Point[x=220,y=145], java.awt.Point[x=221,y=146], java.awt.Point[x=222,y=147], java.awt.Point[x=223,y=148], java.awt.Point[x=224,y=149], java.awt.Point[x=226,y=150], java.awt.Point[x=227,y=151], java.awt.Point[x=228,y=151], java.awt.Point[x=229,y=152], java.awt.Point[x=231,y=153], java.awt.Point[x=232,y=153], java.awt.Point[x=233,y=154], java.awt.Point[x=234,y=155], java.awt.Point[x=235,y=155], java.awt.Point[x=236,y=155], java.awt.Point[x=237,y=155], java.awt.Point[x=237,y=156], java.awt.Point[x=238,y=156], java.awt.Point[x=239,y=156], java.awt.Point[x=240,y=157], java.awt.Point[x=241,y=157], java.awt.Point[x=242,y=157], java.awt.Point[x=243,y=158], java.awt.Point[x=245,y=158], java.awt.Point[x=246,y=159], java.awt.Point[x=248,y=159], java.awt.Point[x=250,y=160], java.awt.Point[x=252,y=160], java.awt.Point[x=253,y=161], java.awt.Point[x=255,y=161], java.awt.Point[x=256,y=162], java.awt.Point[x=257,y=162], java.awt.Point[x=259,y=162], java.awt.Point[x=261,y=163], java.awt.Point[x=262,y=163], java.awt.Point[x=263,y=163], java.awt.Point[x=264,y=163]]
Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.skipCustomData(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at drawingwf.TCPServer.main(TCPServer.java:26)

我是否需要创建某种形式的例外,或者您会建议什么?

1 个答案:

答案 0 :(得分:0)

每次循环时都不要继续重新创建套接字和对象流。如果套接字在两端,则为它们创建一次。