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

}

}

}

[12319 byte] By [toada] at [2007-10-3 9:44:15]
# 1
For one thing your server is much less multi-threaded than you think.
cotton.ma at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 2

What do you think is happening here? Specifically if a socket is a connected and a new one comes in. What then?

private void listen() {

try {

clientSocket = serverSocket.accept();

} catch (IOException e) {

e.printStackTrace();

}

}

cotton.ma at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 3
I think the server is waiting for something to be sent from a client, any client.
toada at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 4
> I think the server is waiting for something to be> sent from a client, any client.You seem to want a multi-threaded server. But your server is not multithreaded. Do you have any experience with multithreading?
cotton.ma at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 5

Yes, I have experience with multi threading, what I am unclear on is sockets. What I don't understand (with respect to my code) is why it works if I remove the for loop in the client. When the loop in the client is executed the second time the connection to the server is still open, why can't it be reused?

toada at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 6
Earlier responses suggest that threading is required if the server is to handle multiple requests, however nothing I've found in my research indicates this is a requirement, Is threading a requirement in order to reuse a socket?
toada at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 7
Problem solved. >> clientSocket = serverSocket.accept(); I was under the mistaken impression that the above line also retrieved data from the input stream. I was wrong it only accepts the connection. Thanks all.
toada at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...
# 8
If it retrieved data where would it put it?
ejpa at 2007-7-15 5:00:42 > top of Java-index,Archived Forums,Socket Programming...