3

I found a client/server code and I am getting this error:

java.net.SocketException: Software caused connection abort: recv failed

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)
user1021085
  • 729
  • 3
  • 10
  • 28
  • Change your client code to print the full stack trace of the exception, not just the exception message. – rfeak Jan 08 '12 at 20:47
  • Hi, sorry for taking so long. Here's the stack trace: EDIT: Uh, the
     output 
    doesn't seem to work for me.
    – user1021085 Jan 08 '12 at 20:55
  • The server just exits after the client connects, is that the problem? – Emil Styrke Jan 08 '12 at 22:04
  • Are you running under Windows? I have seen this error occur when the Windows firewall interferes with network communication. See http://stackoverflow.com/questions/6990663/java-7-prevents-ftp-transfers-on-windows-vista-and-7-if-firewall-is-on-any-idea – prunge Jan 08 '12 at 22:06
  • @Emil Styrke: Yes and no. It is supposed to say Hi from client on the server output and Hi from server on the client output. when I have 2 console windows open and run the client and server code, I get the error I posted above (the stack trace one). – user1021085 Jan 08 '12 at 22:15
  • @prunge: I tried disabling the firewall, didn't work. The weird thing is, It worked at first.. but now it doesn't : / – user1021085 Jan 08 '12 at 22:16
  • @user1021085: But the server isn't waiting for any data from the client, and when the server exits the connection will be closed. I think you have a race condition between the server exiting and the client receiving its data. – Emil Styrke Jan 08 '12 at 22:25
  • @Emil Styrke: How can I make it wait for data? By the way, I added System.out.println(ins.readLine()); to the server code and changed part of the client code to BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream())); System.out.println(is.readLine());. How embaressing.. – user1021085 Jan 08 '12 at 22:38
  • @user1021085: You're not reading anything from the client in the server. If you add a `ins.readLine()` to the server code the server will wait for data from the client before proceeding. – Emil Styrke Jan 08 '12 at 22:53
  • @Emil Styke: Yes, they say hi to eachother now. But how would I go about making a chat? How can I make it keep waiting for input from the server/the client? – user1021085 Jan 08 '12 at 22:56
  • I think that is out of scope for this comment field, but basically you have to put a loop around the readLine/println calls. – Emil Styrke Jan 08 '12 at 23:10

3 Answers3

1

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:

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

BufferedReader ins = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(ins.readLine());
Emil Styrke
  • 1,073
  • 11
  • 19
0

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.

After I added following lines before making call to SOAP API and it worked as expected.

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");
Rohit
  • 396
  • 6
  • 9
0

There are some issues related to Firewall. Please turn off Firewall or else, connect your internet to wifi/personal wifi. These solution worked for me.

  • (This post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer, or just post it as a comment to the question). – sɐunıɔןɐqɐp Jul 26 '18 at 08:08