Client hangs on second request to server
Hi All, I'm doing something wrong and would appreciate some help.
When the client calls the server more than one time the client hangs.
Everything works fine if the client opens the connection to the socket, reads and
writes to the socket and then closes the socket and ends, but if I put the client in
a loop so it communicates with socket more than once the client hangs. (I think because
it's waiting for the sever.) Execution hangs in the getResponse() method in the client.
Any thoughts or suggestions will be appreciated? Source for both client and server are below.
Here is the source for the server:
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
publicclass StatusServer{
private ServerSocket serverSocket;
private Socket clientSocket =null;
private ObjectOutputStream out;
private ObjectInputStream in;
privatefinalint port;
private String message;
privatestaticfinal String SERVER_IS_UP ="001";
privatestaticfinal String SHUTDOWN_IN_PROCESS ="999";
privatestaticfinal String SHUTDOWN ="shutdown";
public StatusServer(int port){
this.port = port;
this.message ="";
}
privatevoid run()throws IOException, ClassNotFoundException{
open();
while (true){
listen();
message = getMessage();
if (shutdownRequested()){
sendResponse(SHUTDOWN_IN_PROCESS);
break;
}
sendResponse(SERVER_IS_UP);
}
close();
}
privatevoid close(){
try{
in.close();
out.close();
serverSocket.close();
}catch (IOException e){
e.printStackTrace();
}
}
privateboolean shutdownRequested(){
return message.equalsIgnoreCase(SHUTDOWN) ?true :false;
}
privatevoid sendResponse(String code)throws IOException,
ClassNotFoundException{
out =new ObjectOutputStream(clientSocket.getOutputStream());
out.writeObject(code);
out.flush();
}
private String getMessage()throws IOException, ClassNotFoundException{
in =new ObjectInputStream(clientSocket.getInputStream());
return (String) in.readObject();
}
privatevoid listen(){
try{
clientSocket = serverSocket.accept();
}catch (IOException e){
e.printStackTrace();
}
}
privatevoid open(){
try{
serverSocket =new ServerSocket(port, 10);
}catch (IOException e){
e.printStackTrace();
}
}
publicstaticvoid checkParms(String[] args){
if (args.length != 1){
thrownew IllegalArgumentException("Please specify a port.");
}
}
publicstaticvoid main(String args[])throws IOException,
ClassNotFoundException{
checkParms(args);
int port = Integer.parseInt(args[0]);
new StatusServer(port).run();
}
}
And here is the source for the client
import java.net.*;
import java.io.*;
publicclass StatusServerClient{
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
privatefinalint port;
privatefinal String address;
public StatusServerClient(int port, String address){
this.port = port;
this.address = address;
}
void run(String msg){
open();
// Loop placed here for testing
for (int i = 0; i < 2; ++i){
try{
out =new ObjectOutputStream(socket.getOutputStream());
out.flush();
}catch (IOException e){
e.printStackTrace();
}
sendRequest(msg);
System.out.println("Message from Host is: " + getResponse());
}
close();
}
privatevoid sendRequest(String request){
try{
out.writeObject(request);
out.flush();
}catch (IOException ioException){
ioException.printStackTrace();
}
}
private String getResponse(){
String response =null;
try{
in =new ObjectInputStream(socket.getInputStream());// This is where executions hangs
response = (String) in.readObject();
}catch (IOException e){
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
return response;
}
privatevoid close(){
try{
in.close();
out.close();
socket.close();
}catch (IOException ioException){
ioException.printStackTrace();
}
}
privatevoid open(){
try{
socket =new Socket(address, port);
}catch (UnknownHostException e){
System.out.println("Server name is invalid, specify a valid name");
}catch (IOException e){
e.printStackTrace();
}
// System.out.println("Connected to localhost in port 2004");
}
publicstaticvoid main(String args[]){
if (args.length == 3){
int port = Integer.parseInt(args[0]);
String address = args[1];
String command = args[2];
if (command !=null && command.length() > 0){
new StatusServerClient(port, address).run(command);
}
}else{
System.out.println("Three arguments are required.");
System.out
.println("Usage: Administrator <port> <address> <message>");
System.out.println("Example: Administrator 4000 ANDY Hello");
}
}
}

