[thelist] Java Sockets

m.j.milicevic me at machak.com
Wed Mar 15 05:25:53 CST 2006


Hi Chris,
you don't have to do much: incoming connections wil be stored in a queue
until your server can accept them.
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).

kind regards,
-m.j.milicevic
http://www.machak.com
----- Original Message ----- 
From: <Chris at globet.com>
To: <thelist at lists.evolt.org>
Sent: Wednesday, March 15, 2006 11:54 AM
Subject: [thelist] Java Sockets


> All
>
> If this is off topic, please let me know and I shallnot proceed with any
> further enquiries.
>
> I am learning Java, and am currently attempting to complete an exercise
> to produce a very simple client and server that will operate across the
> internet. The client will have several simple commands to which the
> server will respond with specified text. The client will have the
> command QUIT, which will disconnect the client, but NOT close down the
> server.
>
> As I understand it, I should create a ServerSocket instance thus:
>
> ServerSocket ss = new ServerSocket(9999);
>
> I can then use the accept() method thus:
>
> Socket sock = ss.accept();
>
> This will suspend processing until a client connects on the specified
> port, after which sock will reference a Socket instance that is
> connected to the Socket instance on the client. Once a client has
> connected to the server on the specified port, I can get the input and
> output streams and process them thus:
>
> DataInputStream is = new DataInputStream(sock.getInputStream());
> PrintStream os = new PrintStream(sock.getOutputStream());
>
> String cmd = "";
> while (true)
> {
> cmd = is.readLine();
> if (cmd.equals("QUIT")
> break;
> // Respond appropriately.
> }
>
> My question is as follows. My understanding is that once a client has
> connected to the server, if any other clients try to connect to the
> server they will be put into a queue to be dealt with when the connected
> client disconnects. Given my approach above, how do I ensure that this
> happens? I need to understand more clearly how the Socket/ServerSocket
> mechanism works, rather than simply be presented with a solution. Also
> for the purposes of this exercise I do not wish to use threads.
>
> Many thanks in advance!
>
> 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.
> -- 
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !




More information about the thelist mailing list