[thelist] Java Sockets
m.j.milicevic
me at machak.com
Wed Mar 15 08:55:29 CST 2006
Hi Chris,
> This I have read, but I don't understand the concept of where this queue
> is stored, and how I re-initialise sock (in the example below) to
> reference a Socket object connected to the new client. I've added line
> numbers to my example to try and illustrate the source of my lack of
> understanding.
>
> 1 ServerSocket ss = new ServerSocket(9999);
> 2 Socket sock = ss.accept();
> 3 DataInputStream is = new DataInputStream(sock.getInputStream());
> 4 PrintStream os = new PrintStream(sock.getOutputStream());
> 5 String cmd = "";
> 6 while (true)
> 7 {
> 8 cmd = is.readLine();
> 9 if (cmd.equals("QUIT")
> 10 break;
> 11 // Respond appropriately.
> 12 }
> 13 // ?
>
> On line 2: as soon as the first client connects, sock references a
> Socket that is connected to the client. On line 6, we enter a loop that
> deals with all communication between the client and server. This loop
> continues to deal with communication until the server receives the
> message QUIT from the client. Line 10 exits the while loop, and
> processing is transferred to line 13. It is at this point that my
> understanding disappears.
>
> * How do I then ensure that the server continues to listen for client
> connections if none are in the queue, or process the next connection if
> connections are queued up?
well, once serversocket is binded to an address, it'll go and process
incomming connections
till it is shut down. ServerSocket insures that for you (till it's shut down
of course).
Just take it for granted. Also, read below..
> * Should I be closing the input/output streams?
yes, always do cleanup, also when sending data(e.g. through PrintWriter), be
sure to flush data, otherwise client/server needs to wait
till buffer is full to get any data. You can use "true" paramater for
autoflushing data.
> * Should I close the Socket object referenced by sock?
yes.
try {
ServerSocket ss = new ServerSocket(9999);
while (true) {
Socket incoming = ss.accept();
doSomethingWithSocket(incoming);// close socket, and move to process next
one
incoming.close();
}
} catch(IOException e) {
// deal with exceptions
}
> * Should I be thinking in terms of a recursive method thus:
>
> 1 private ServerSocket ss = new ServerSocket(9999);
> 2 private void listen()
> 3 {
> 4 Socket sock = this.ss.accept();
> 5 DataInputStream is = new DataInputStream(sock.getInputStream());
> 6 PrintStream os = new PrintStream(sock.getOutputStream());
> 7 String cmd = "";
> 8 while (true)
> 9 {
> 10 cmd = is.readLine();
> 11 if (cmd.equals("QUIT")
> 12 break;
> 13 // Respond appropriately.
> 14 }
> 15 // Close streams and socket
> 16 this.listen();
> 17 }
>
> Any further information will be gratefully received.
well, what you need to understand is that accept() method is blocking till
the connection is established. Once this is done, accept()method will return
a socket instance. Once you get socket instance, you can process it e.g.
read some data or write to it. During this process, no other connections
will be accepted, so this whole process is blocking.
So, what you really need is a separate process(thread) that handles this
reading/writing operation
simple example:
public static void main(String[] args){// create serversocket ss, surround
with try catch or throw exceptions:// main server lop would look like:while
(true){ new SocketHandlerThread(ss.accept()).start();
}}and your socket handler would be something like:public class
SocketHandlerThread extends Thread {private Socket socket = null; public
SocketHandlerThread(Socket socket) { super("SocketHandlerThread");
this.socket = socket;} public void run() { try { PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);//autoflush
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream())); String input, output;
out.println("hello");// just echo data: while ((input = in.readLine())
!= null) { out.println("you said:"); out.println(input); if
(input.equals("QUIT")) { break; } }//close out.close();
in.close(); socket.close(); } catch (IOException e) { //deal with
exception } }}hthkind regards,-m.j.milicevichttp://www.machak.com
More information about the thelist
mailing list