简体   繁体   English

“软件导致连接中止:接收失败”的原因

[英]Cause of “Software caused connection abort: recv failed”

I found a client/server code and I am getting this error: 我找到了客户端/服务器代码,但出现此错误:

java.net.SocketException: Software caused connection abort: recv failed java.net.SocketException:软件导致连接中止:recv失败

Server code: 服务器代码:

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

Client code: 客户代码:

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

The Server code gives the following output: 服务器代码提供以下输出:

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>

The client code gives the following output: 客户端代码给出以下输出:

C:\WorkStation\testClient\src>java client
 Trying to connect
SocketException java.net.SocketException: Software caused connection abort: recv
 failed
    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)

The server isn't waiting for any data from the client, and when the server exits the connection will be closed. 服务器不等待来自客户端的任何数据,并且当服务器退出时,连接将关闭。

Add a ins.readLine() to the server code like this: ins.readLine()添加到服务器代码中,如下所示:

// Receive message from client i.e Request from client

BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(ins.readLine());

In my case it was related to Client authentication. 就我而言,它与客户端身份验证有关。 It happened to me when I was trying to access SOAP API from Java code without setting keystore/keystorePassword and from the stacktrace there is no way we can figure out that there's a problem with Authentication. 当我尝试从Java代码访问SOAP API而未设置keystore / keystorePassword时,这发生在我身上,并且从stacktrace中无法确定身份验证是否存在问题。

After I added following lines before making call to SOAP API and it worked as expected. 在调用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");

There are some issues related to Firewall. 有一些与防火墙有关的问题。 Please turn off Firewall or else, connect your internet to wifi/personal wifi. 请关闭防火墙,否则,将您的Internet连接到wifi /个人wifi。 These solution worked for me. 这些解决方案为我工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 软件导致连接中止:recv失败 - Software caused connection abort: recv failed Java错误 - 软件导致连接中止:recv失败 - Java error - Software caused connection abort: recv failed 客户端异常:软件导致连接中止:recv 失败 - Client Exception: Software caused connection abort: recv failed java.net.SocketException:软件导致连接中止:recv失败; 原因和治疗方法? - java.net.SocketException: Software caused connection abort: recv failed; Causes and cures? JnrpeClient:java.net.SocketException:软件导致连接中止:recv失败 - JnrpeClient : java.net.SocketException: Software caused connection abort: recv failed java.net.SocketException:软件导致连接中止:Java 8 recv失败 - java.net.SocketException: Software caused connection abort: recv failed with Java 8 FTP文件上传异常“软件导致连接中止:接收失败” -JAVA - FTP File upload Exception 'Software caused connection abort: recv failed' -JAVA java.net.SocketException:软件导致连接中止:使用其他jre安全性时recv失败 - java.net.SocketException: Software caused connection abort: recv failed when using different jre security BufferedReader.readLine()给出错误java.net.SocketException:软件导致连接中止:recv失败 - BufferedReader.readLine() gives error java.net.SocketException: Software caused connection abort: recv failed Java SocketException:软件导致连接中止 - Java SocketException: Software caused connection abort
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM