“软件导致连接中止:recv failed”的原因
Posted
技术标签:
【中文标题】“软件导致连接中止:recv failed”的原因【英文标题】:Cause of "Software caused connection abort: recv failed" 【发布时间】:2012-02-05 12:44:00 【问题描述】:我找到了一个客户端/服务器代码,我收到了这个错误:
java.net.SocketException:软件导致连接中止:recv failed
服务器代码:
import java.net.*;
import java.lang.*;
import java.io.*;
public class Server
//port number should be more than 1024
public static final int PORT = 1025;
public static void main( String args[]) throws IOException
ServerSocket sersock = null;
Socket sock = null;
System.out.println(" Wait !! ");
try
// Initialising the ServerSocket
sersock = new ServerSocket(PORT);
// Gives the Server Details Machine name, Port number
System.out.println("Server Started :"+sersock);
try
// makes a socket connection to particular client after
// which two way communication take place
sock = sersock.accept();
System.out.println("Client Connected :"+ sock);
// Receive message from client i.e Request from client
BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
// Send message to the client i.e Response
PrintStream ios = new PrintStream(sock.getOutputStream());
ios.println("Hello from server");
ios.close();
// Close the Socket connection
sock.close();
catch(SocketException se)
System.out.println("Server Socket problem "+se.getMessage());
catch(Exception e)
System.out.println("Couldn't start "
+ e.getMessage()) ;
// Usage of some methods in Socket class
System.out.println(" Connection from : " +
sock.getInetAddress());
finally // main
// Server class
客户端代码:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;
public class client
public static void main(String args[])
Socket sock=null;
DataInputStream dis=null;
PrintStream ps=null;
System.out.println(" Trying to connect");
try
// to get the ip address of the server by the name
InetAddress ip =InetAddress.getByName
("localhost");
// Connecting to the port 1025 declared in the Serverclass
// Creates a socket with the server bind to it.
sock= new Socket(ip,1025);
ps= new PrintStream(sock.getOutputStream());
ps.println(" Hi from client");
BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(is.readLine());
catch(SocketException e)
System.out.println("SocketException " + e);
catch(IOException e)
System.out.println("IOException " + e);
// Finally closing the socket from the client side
finally
try
sock.close();
catch(IOException ie)
System.out.println(" Close Error :" +
ie.getMessage());
// finally
// main
// Class Client
服务器代码给出以下输出:
C:\WorkStation\Testcserver\src>java 服务器 等待 !! 服务器已启动:ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=1025] 客户端连接:Socket[addr=/127.0.0.1,port=56143,localport=1025] 连接来自:/127.0.0.1 C:\WorkStation\Testcserver\src>客户端代码给出以下输出:
C:\WorkStation\testClient\src>java 客户端 尝试连接 SocketException java.net.SocketException:软件导致连接中止:recv 失败的 C:\WorkStation\testClient\src>java client
Trying to connect
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at client.main(client.java:30)
【问题讨论】:
更改您的客户端代码以打印异常的完整堆栈跟踪,而不仅仅是异常消息。 您好,抱歉拖了这么久。这是堆栈跟踪: 编辑:嗯,输出似乎对我不起作用。 客户端连接后服务器就退出了,是这个问题吗? 您是在 Windows 下运行吗?我已经看到当 Windows 防火墙干扰网络通信时会发生此错误。见***.com/questions/6990663/… @Emil Styrke:是的,不是的。应该在服务器输出上说来自客户端的 Hi,在客户端输出上说来自服务器的 Hi。当我打开 2 个控制台窗口并运行客户端和服务器代码时,我收到上面发布的错误(堆栈跟踪之一)。 【参考方案1】:
服务器不等待来自客户端的任何数据,当服务器退出时,连接将被关闭。
在服务器代码中添加ins.readLine()
,如下所示:
// Receive message from client i.e Request from client
BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(ins.readLine());
【讨论】:
【参考方案2】:就我而言,它与客户端身份验证有关。 当我尝试从 Java 代码访问 SOAP API 而不设置 keystore/keystorePassword 并且从堆栈跟踪中我们无法确定身份验证存在问题时,它发生在我身上。
在调用 SOAP API 之前添加了以下几行之后,它按预期工作。
System.setProperty("javax.net.ssl.keyStoreType", "Your Cert Type");
System.setProperty("javax.net.ssl.keyStore", "Your cert path");
System.setProperty("javax.net.ssl.keyStorePassword", "Password");
【讨论】:
【参考方案3】:有一些与防火墙相关的问题。请关闭防火墙,否则,将您的互联网连接到 wifi/个人 wifi。这些解决方案对我有用。
【讨论】:
(这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对问题的评论发布)。以上是关于“软件导致连接中止:recv failed”的原因的主要内容,如果未能解决你的问题,请参考以下文章
“'NSInternalInconsistencyException' 的替代原因,原因:'此 NSPersistentStoreCoordinator 没有持久存储。'”