Re: Java
From: andreas (newsgroup_at_kinell.ch)
Date: 09/22/04
- Next message: Ken Philips: "Is an extension of ".xslt2" a must for xslt stylesheets of spec 2.0 ?"
- Previous message: EjP: "Re: Obtaining derived classes"
- In reply to: Ted: "Re: Java"
- Next in thread: Stefan Schulz: "Re: Java"
- Reply: Stefan Schulz: "Re: Java"
- Reply: Ted: "Re: Java"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 22 Sep 2004 20:28:12 +0200
> I haven't got any threads running yet, am I correct in thinking that
> I'll need threads to run multiple clients?
yes: if you want the server to handle multiple clients, you need threads.
> I'm also a little bit unsure about how I should layout the classes.
> Ie: should I have a client class, server class, and connection class??
a client class and a server class makes a lot of sense.
later both the client and the server class will have their own main()
method.
so you start a server on a computer as a standalone, and the client on
another.
the connection class:
it is not needed. i do use a ConnectionManager class which offers
openConnection(), closeConnection(), send and receive.
but note, that a server might handle a connection differently than a client.
you could start without. your client would then look more or less like this:
public class Client{
static final int SUCCESS = 1;
static final int ERROR = 0;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
public Client(//arguments){
//create socket
//bind streams
}
public int send(String message){
//maybe convert the string to byte[]
out.write(message);
return SUCCESS;
}
public int receive(byte[] byteArray){
//in.read returns the number of bytes read and writes the data into the
byteArray
return in.read(byteArray);
}
public static void main(//arguments){
Client client = new Client(//arguments);
//read something from input
client.send(//input);
client.receive(//answer);
}
}
note that this is not a complete client but a framework.
this is how it could look like. also, it is very convenient
to be able to receive messages unblocked (note that receive() here
will block until data is available). you would then start a new thread
with a run method looking more or less like this:
run(){
while(true){
client.receive();
//print the received data to the screen
}
>> you should now design a chat-protocol, which both server and client
>
> I'm not too sure what you mean by this. Do you mean a class to handle
> messages to and from the server?
>
no, the general design. maybe you want to interpret some comands instead
of just printing them on the screen.
have a look at RFC2812 for the specifications of the IRC protocol
andreas
- Next message: Ken Philips: "Is an extension of ".xslt2" a must for xslt stylesheets of spec 2.0 ?"
- Previous message: EjP: "Re: Obtaining derived classes"
- In reply to: Ted: "Re: Java"
- Next in thread: Stefan Schulz: "Re: Java"
- Reply: Stefan Schulz: "Re: Java"
- Reply: Ted: "Re: Java"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]