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 !! ");

  //  Initialising the ServerSocket
  sersock =  new ServerSocket(PORT);

  // Gives the Server Details Machine name, Port number

  System.out.println("Server Started  :"+sersock);


   // 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");

   // Close the Socket connection 


    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 :  " + 

 }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");

 // to get the ip address of the  server by the name

 InetAddress ip =InetAddress.getByName

 // 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()));

 catch(SocketException e)
 catch(IOException e)
  System.out.println("IOException " + e);

  // Finally closing the socket from the client side

  catch(IOException ie)
   System.out.println(" Close Error   :" + 
 }  // finally 

} // main 
}   // Class Client

Server ouput:

C:\WorkStation\Testcserver\src>java Server
Wait !!
Server Started :ServerSocket[addr=,port=0,localport=1025]
Client Connected :Socket[addr=/,port=56143,localport=1025]
Connection from : /


Client output:

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)

How can I fix this?


I realised I never printed out the response.

I have another question, though.
How would I go about making it like chat?

This article has been dead for over six months. Start a new discussion instead.