Java StackOverflowerError then SocketException:软件导致的连接中止:套接字写入错误
我正在尝试制作一个程序,它可以向服务器发送文件和消息,还可以检测服务器何时断开连接(可能是因为关闭应用程序等),所以我所做的就是让客户端读取,当我第一次单击按钮Java StackOverflowerError then SocketException:软件导致的连接中止:套接字写入错误,java,sockets,network-programming,Java,Sockets,Network Programming,我正在尝试制作一个程序,它可以向服务器发送文件和消息,还可以检测服务器何时断开连接(可能是因为关闭应用程序等),所以我所做的就是让客户端读取,当我第一次单击按钮btnItem时,它工作正常,但当我再次单击它时,它在healthChecker 问题:如何修复堆栈溢出错误 这是我的密码: 服务器 public class Server extends JFrame { JPanel server=new JPanel(); JPanel main=new JPanel();
btnItem
时,它工作正常,但当我再次单击它时,它在healthChecker
问题:如何修复堆栈溢出错误
这是我的密码:
服务器
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:软件导致的连接中止:套接字写入错误
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();
}
}
在方法
healthchecker()
中,您再次调用healthchecker()
。这将导致非终止递归。每个递归级别都会在堆栈上放置一些内容,最后堆栈就满了。以下方法调用导致溢出。如错误消息所示,问题在客户端代码的healthChecker中
从第二行到最后一行的呼叫是这里的问题。
不管怎样,healthChecker总是递归地调用自己,没有任何退出条件可以阻止它这样做。
因此,在运行时,它会在调用堆栈上堆积越来越多的指针,最终导致堆栈溢出
执行递归调用时,请确保始终具有退出条件。
否则,递归调用将永远进行下去,这将产生StackOverflow错误。您有无限递归。@EJP抱歉,我理解不清楚。。。你能给我解释一下我的问题的解决方案是什么或如何解决的吗?@askManiac这都在对这个问题的回答中解释过了。解释是+1。或者是
connect2Server()
参见更新:D我如何持续检查服务器是否在线?
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();
}