How to make a server accept more clients after terminating the first

Hi, I'm having a problem with my program. It is a server program that can handle one user at a time, but after closing the client, the server just hangs.. It still takes connections, but it doesn't respond to telnet commands after the first client has disconnected.

How do I go about changing this, so I can connect to it as much as I want?

Here is the code:

package main;

import java.net.*;

import java.io.*;

/**

* Network class that opens a server socket and contains the related methods

* @author Frederik

*

*/

publicclass Network{

BufferedReader in;

PrintWriter out;

ServerSocket serverSocket;

Socket clientSocket;

/**

* Constructor for the Network class

* @param port

*/

public Network(int port){

serverSocket =null;

try{

//Opens a new server socket

serverSocket =new ServerSocket(port);

}catch (IOException e){

System.err.println("Could not listen on port: " + port);

System.exit(1);

}

clientSocket =null;

try{

//opens the clientSocket

clientSocket = serverSocket.accept();

}catch (IOException e){

System.err.println("Accept failed.");

System.exit(1);

}

try{

out =new PrintWriter(clientSocket.getOutputStream(),true);

in =new BufferedReader(

new InputStreamReader(

clientSocket.getInputStream()));

}catch(IOException e){

System.out.println(e);

}

}

/**

* Method that returns the input from the buffered reader, if it is ready

* @return input

* @throws IOException

*/

public String inputLine()throws IOException{

String input =null;

if(in.ready()){

input = in.readLine();

}

return input;

}

/**

* Method that sends data through the socket

* @param output the string that is supposed to be sent

*/

publicvoid send(String output){

//Only send if there is data in the outputString

if(output !=null){

out.println(output);

}

}

}

[4023 byte] By [hogla] at [2007-11-27 8:19:21]
# 1
But the accept call inside a while loop. When new sockets are returned spin them off in a new thread (or another thread at least).
cotton.ma at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...
# 2
But it's not supposed to have multiple users logged on at the same time. It just need to be able to recieve data from a client after the first one has disconnected
hogla at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...
# 3
How do I check if the client is still connected? clientSocket.isConnected() returns true when it has been connected to once
hogla at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...
# 4
Then still put it in a while loop, and just simply don't start a new thread.Handle that connection in the while loop, then simply close that client socket, and let the while loop continue and then you are automatically waiting on the next connection.
masijade.a at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...
# 5

> How do I check if the client is still connected?

InputStream.read() won't return -1. readLine() won't return null. readXXX() for any other XXX won't throw EOFException. A write to the socket won't throw an IOException.

If any of those things happens, the client is not still connected and you should close the socket.

This has nothing to do with the question of whether or not you should start a new thread per accepted socket, which is simply a question of whether or not you want to handle multiple clients at the same time.

> clientSocket.isConnected() returns true when it has

> been connected to once

That's what it's for. It's not supposed to tell you about the other end, it tells you about what you've done at this end.

ejpa at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...
# 6

> > How do I check if the client is still connected?

>

> InputStream.read() won't return -1. readLine() won't

> return null. readXXX() for any other XXX won't throw

> EOFException. A write to the socket won't throw an

> IOException.

>

> If any of those things happens, the client is not

> still connected and you should close the socket.

>

> This has nothing to do with the question of whether

> or not you should start a new thread per accepted

> socket, which is simply a question of whether or not

> you want to handle multiple clients at the same

> time.

>

> > clientSocket.isConnected() returns true when it

> has

> > been connected to once

>

> That's what it's for. It's not supposed to tell you

> about the other end, it tells you about what you've

> done at this end.

Thanks, this looks like what I was looking for. Back to work :)

hogla at 2007-7-12 20:07:29 > top of Java-index,Core,Core APIs...