***Error 然后 SocketException:软件导致连接中止:套接字写入错误 [重复]
Posted
技术标签:
【中文标题】***Error 然后 SocketException:软件导致连接中止:套接字写入错误 [重复]【英文标题】:***Error then SocketException: Software caused connection abort: socket write error [duplicate] 【发布时间】:2014-10-12 04:32:22 【问题描述】:我正在尝试制作一个程序,它将向服务器发送文件和消息,还检测服务器何时断开连接(可能是因为关闭应用程序等)所以我所做的就是让客户端读取,当我第一次单击按钮btnItem
时,它可以正常工作,但是当我再次单击它时,它在healthChecker
上给了我这个错误***Error
问题:如何修复 ***Error
?
这是我的代码:
服务器
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();
【问题讨论】:
你有无限递归。 @EJP 抱歉,我的理解很差...您能向我解释一下我的问题的解决方案是什么或如何解决吗? @askManiac 这一切都在这个重复的问题的答案中得到了解释。 【参考方案1】:在您的方法healthchecker()
中,您再次调用healthChecker()
。这导致非终止递归。每个递归级别都会在堆栈上放一些东西,最后堆栈是满的。以下方法调用导致溢出。
【讨论】:
+1 解释。 还是connect2Server()
的非终止递归
查看更新 :D 我将如何持续检查服务器是否在线?【参考方案2】:
如错误消息所示,问题出在客户端代码中的 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 总是递归调用自己,没有退出条件会阻止它这样做。 因此,在运行时,它会在调用堆栈上堆积越来越多的指针,最终导致 ***。
进行递归调用时,请确保始终有退出条件。 否则,递归调用将永远持续下去,这将产生 *** 错误。
【讨论】:
以上是关于***Error 然后 SocketException:软件导致连接中止:套接字写入错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
尝试在 React Native 中使用 GoogleSignIn,然后可能出现未处理的 Promise Rejection(id:错误:DEVELOPER_ERROR
500(Internal Server)Error怎么处理?
Error:fatal error C1010: unexpected end of file while looking for precompiled head