java.net.SocketException Socket is closed when i use socket.getInputStream(
843790Nov 1 2006 — edited Nov 3 2006I have a client and server test application. The client opens a socket on the server and send a msg "Hello server", this ms is read well by BufferedReader and the reader send back a msg "Fine Client" using a PrintWriter...
The problem is, when i try to read this inputStream, is give "Socket is Closed" error, here is the code for client
/*
* ClientSide.java
*
* Created on November 1, 2006, 12:08 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.sikped.client;
import java.net.*;
import java.io.*;
/**
*
* @author Simon
*/
public class ClientSide {
String hostName = "192.168.0.8";
Integer portNumber = 20000;
/** Creates a new instance of ClientSide */
public ClientSide() {
Socket clientSocket = null;
InputStream is;
try{
clientSocket = new Socket(hostName,portNumber) ;
PrintStream ps = new PrintStream(clientSocket.getOutputStream());
ps.print("Hello Server");
//ps.flush();
ps.close();
}catch(IOException ioe){
System.out.println("Error "+ioe.getMessage());
ioe.printStackTrace();
}
try{
System.out.println("Reading now");
System.out.println(clientSocket.isConnected());//this returns true
//BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//String line = reader.readLine();
System.out.println(clientSocket.getInputStream());//this gives the error Socket Closed
}catch(Exception ioe){
System.out.println("Reader error "+ioe.getMessage());
ioe.printStackTrace();
}
}
public static void main(String[] arg){
new ClientSide();
}
}