Java “原因”;软件导致的连接中止:recv失败“;
我发现了一个客户机/服务器代码,出现以下错误: java.net.SocketException:软件导致的连接中止:recv失败 服务器代码:Java “原因”;软件导致的连接中止:recv失败“;,java,sockets,Java,Sockets,我发现了一个客户机/服务器代码,出现以下错误: java.net.SocketException:软件导致的连接中止:recv失败 服务器代码: 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
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 Server
Wait !!
Server Started :ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=1025]
Client Connected :Socket[addr=/127.0.0.1,port=56143,localport=1025]
Connection from : /127.0.0.1
C:\WorkStation\Testcserver\src>
C:\WorkStation\testClient\src>java client
Trying to connect
SocketException java.net.SocketException: Software caused connection abort: recv
failed
服务器没有等待来自客户端的任何数据,当服务器退出时,连接将关闭 向服务器代码中添加
ins.readLine()
,如下所示:
// Receive message from client i.e Request from client
BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(ins.readLine());
在我的例子中,它与客户端身份验证有关。 当我试图从Java代码中访问SOAP API而不设置keystore/keystorePassword时,我遇到了这种情况。从stacktrace中,我们无法发现身份验证存在问题 在调用SOAPAPI之前,我添加了以下几行代码,它按预期工作
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");
存在一些与防火墙相关的问题。请关闭防火墙,或者将您的互联网连接到wifi/个人wifi。这些解决方案对我很有效。更改客户端代码以打印异常的完整堆栈跟踪,而不仅仅是异常消息。您好,很抱歉花了这么长时间。这是堆栈跟踪:编辑:嗯,输出似乎对我不起作用。服务器在客户端连接后才退出,这是问题吗?你在Windows下运行吗?我见过当Windows防火墙干扰网络通信时发生此错误。请参阅@Emil Styrke:Yes和no。它应该在服务器输出上说Hi from client,在客户端输出上说Hi from server。当我打开两个控制台窗口并运行客户端和服务器代码时,我会收到上面发布的错误(堆栈跟踪错误)。(此帖子似乎没有提供问题的答案。请编辑您的答案,或者将其作为问题的注释发布)。
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");