[thelist] Java Sockets

Chris at globet.com Chris at globet.com
Wed Mar 15 05:49:47 CST 2006


MJM

Many thanks for responding.

> you don't have to do much: incoming connections wil be stored 
> in a queue until your server can accept them.

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?

* Should I be closing the input/output streams?

* Should I close the Socket object referenced by sock?

* 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.

> I believe queue accepts 50 clients by default, if it is full 
> it will refuse connections till queue has place.
> You can override it by using:
> 
> ServerSocket server_socket = new ServerSocket(88, 150);
> 
> where second parameter is number of clients in the queue.
> Java 1.4 has support for non-blocking sockets, be sure to 
> have a look at those (it's part of java NIO).

Thanks for the info, I shall be sure to investigate non-blocking sockets
further.

Chris Marsh
Web Developer
http://www.globet.com/
Tel: +44 20 8246 4804 Ext 828
Fax: +44 20 8246 4808

Any opinions expressed in this email are those of the individual and not
necessarily the Company. This message is intended for the use of the
individual or entity to which it is addressed and may contain
information that is confidential and privileged and exempt from
disclosure under applicable law. If the reader of this message is not
the intended recipient, you are hereby notified that any dissemination,
distribution, or copying of this communication is strictly prohibited.
If you have received this communication in error, please contact the
sender immediately and delete it from your system. 



More information about the thelist mailing list