StackOverflowError然后SocketException:软件导致连接中止:套接字写入错误

时间:2014-08-19 06:51:43

标签: java sockets network-programming

我正在尝试创建一个程序,它将文件和消息发送到服务器,也会检测服务器何时断开连接(可能是因为关闭了应用程序等)所以我所做的是让客户端读取,当我第一次单击按钮btnItem时它完美无缺,但当我再次点击它时,StackOverflowError

上的healthChecker错误

问题:如何修复StackOverflowError

这是我的代码:

服务器

public class Server extends JFrame
{
    JPanel server=new JPanel();
    JPanel main=new JPanel();
    JPanel top=new JPanel();
    JPanel bot=new JPanel();
    JTextArea console=new JTextArea();

    public Server()
    {
        super("Server");
        server.setLayout(new BorderLayout());
        main.setLayout(new BorderLayout());
        top.setLayout(new BorderLayout());
        bot.setLayout(new BorderLayout());
        console.setEditable(false);
        console.setFont(new Font("Courier New",Font.PLAIN,14));
        console.setBackground(Color.BLACK);
        console.setForeground(Color.WHITE);
        bot.add(console);
        main.add(top,BorderLayout.NORTH);
        main.add(bot,BorderLayout.CENTER);
        add(main);
    }

    public static void main(String[] args) 
    {
        Server f=new Server();
        f.setVisible(true);
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Thread s1=new Thread(new Socket1(f.console));
        s1.start();
        Thread s2=new Thread(new Socket2());
        s2.start();

    }
}

SOCKET1

public class Socket1 implements Runnable
{
    JTextArea console;

    public Socket1(JTextArea console)
    {
        this.console=console;
    }

    public void run()
    {
        try
        {
            int port=4000;
            ServerSocket checker=new ServerSocket(port);
            console.append("Server is up and listening to port:"+port+"\r\n");
            while(true)
            {
                Socket socket=checker.accept();
                InputStreamReader streamReader=new InputStreamReader(socket.getInputStream());
                BufferedReader reader=new BufferedReader(streamReader);
                String[] received=reader.readLine().split("~");
                console.append(received[1]+": requesting for "+received[0]+"\r\n");
                Thread createfile=new Thread(new Creator(received[0],received[1],console));
                createfile.start();
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

客户端

public class Branch extends JFrame
{
    private Socket socket;
    private DataOutputStream dos;
    private DataInputStream dis;
    private InetAddress address;
    private String IP="localhost";
    private int port=4000;
    private static String OS= System.getProperty("os.name").toLowerCase();
    String filename;


    JPanel main=new JPanel();
    JPanel top=new JPanel();
    JPanel bot=new JPanel();
    JButton btnItem=new JButton("item");
    JButton btnGlstock=new JButton("glstock");
    JTextArea console=new JTextArea();
    JScrollPane scrollv=new JScrollPane(console);
    DefaultCaret caret=(DefaultCaret) console.getCaret();

    ActionListener item=new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String myIP="127.0.0.1";
            String send="";
            try
            {
                //myIP=InetAddress.getLocalHost().getHostAddress();
                send="item~"+myIP;
                request(send);
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }
        }
    };

    private void request(String send)
    {
        try
        {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(send+"\n");
            bw.flush();
            String[] obj=send.split("~");
            console.append("requesting for "+obj[0]+"\n");
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public Branch()
    {
        super("Branch");
        scrollv.setAutoscrolls(true);
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        main.setLayout(new BorderLayout());
        console.setFont(new Font("Courier New",Font.PLAIN,14));
        top.setLayout(new FlowLayout());
        top.add(btnItem);
        top.add(btnGlstock);
        btnItem.addActionListener(item);
        btnGlstock.addActionListener(glstock);
        bot.setLayout(new BorderLayout());
        console.setEditable(false);
        console.setForeground(Color.white);
        console.setBackground(Color.black);
        bot.add(scrollv,BorderLayout.CENTER);
        main.add(top,BorderLayout.NORTH);
        main.add(bot,BorderLayout.CENTER);
        add(main);
    }

    private void healthChecker()
    {
        try
        {
            socket.setSoTimeout(5000);
            InputStreamReader isr=new InputStreamReader(socket.getInputStream());
            isr.read();

        }catch(SocketException ex)
        {
            ex.printStackTrace();
            try
            {
                connect2Server();
            }catch(Exception exc)
            {

            }
        }
        catch(SocketTimeoutException ex)
        {

        }
        catch(IOException ex)
        {

        }
        healthChecker();
    }

    private void connect2Server() throws IOException
    {
        try
        {
            socket = new Socket(IP,port);
            console.append("You are now Connected to the Server\r\n");
            healthChecker();
        }
        catch(IOException ex)
        {
            console.append("Server is offline\r\n");
            ex.printStackTrace();
            connect2Server();
        }
    }

    public static void main(String args[])
    {
        Branch frame=new Branch();
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try
        {
            frame.connect2Server();
        }catch(IOException ex)
        {

        }
    }
}

更新

我尝试编辑healthChecker()现在它给了我SocketException: Software caused connection abort: socket write error

private void healthChecker()
{
    try
    {
           socket.setSoTimeout(5000);
           InputStreamReader isr=new InputStreamReader(socket.getInputStream());
           isr.read();

    }
    catch(SocketException ex)
    {
         ex.printStackTrace();
         try
         {
             connect2Server();
         }catch(Exception exc)
         {
             ex.printStackTrace();
         }
    }
    catch(SocketTimeoutException ex)
    {
         ex.printStackTrace();  
    }
    catch(IOException ex)
    {
         ex.printStackTrace();       
    }
 }

2 个答案:

答案 0 :(得分:2)

在方法healthchecker()中,您再次呼叫healthChecker()。这导致非终止递归。每个递归级别都会在堆栈上放置一些内容,最后堆栈已满。以下方法调用会导致溢出。

答案 1 :(得分:0)

如错误消息所示,问题出在客户端代码中的healthChecker中。

private void healthChecker(){
    try
    {
        socket.setSoTimeout(5000);
        InputStreamReader isr=new InputStreamReader(socket.getInputStream());
        isr.read();

    }catch(SocketException ex)
    {
        ex.printStackTrace();
        try
        {
            connect2Server();
        }catch(Exception exc)
        {

        }
    }
    catch(SocketTimeoutException ex)
    {

    }
    catch(IOException ex)
    {

    }
  healthChecker();
}

第二行到最后一行的电话是这里的问题。 无论如何,healthChecker总是以递归方式调用自身,没有退出条件会阻止它这样做。 因此,在运行时,它会将越来越多的指针堆积到调用堆栈上,最终会导致StackOverflow。

进行递归调用时,请务必始终具有退出条件。 否则,递归调用将永远进行,这将产生StackOverflow错误。