[thelist] Java Sockets

Chris at globet.com Chris at globet.com
Wed Mar 15 04:54:21 CST 2006


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

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