a simple program
a simple program
- Subject: a simple program
- From: Candide Kemmler <email@hidden>
- Date: Thu, 16 Aug 2001 20:41:39 +0200
Hi,
I'm relatively new to Cocoa (though I've already written a small
graphics app in learning mode), and I'd like to play a bit with it for
server-side programming. In fact, I'd need some help with
networking/multi-threading.
The following program (written in java) is a simple chat server I wrote
some time ago for a client. See how small it is (it took me about an
hour to write, test, deploy) !
To me, writing something like this in Obj-C still appears like a
challenge...
Prove me wrong, please ;-) In particular, I'd like to know how to deal
with the multi-threading part.
Regards,
Aurilien
PS: here's how the program works: a main thread listens for connections.
Everytime it accepts a new client, a new Client object is created and
added to a Vector. The client spawns a new threads where it listens for
incoming messages. When a new message arrives, it tells each client in
the Vector to write the message to their output stream. When the
connection's closed, the client removes itself from the Vector.
import java.net.*;
import java.io.*;
import java.util.*;
public class messageCenter
implements Runnable {
/** Creates new messageCenter */
/**
* @param args the command line arguments
*/
public static void main (String args[]) {
messageCenter mc = new messageCenter ();
Thread t = new Thread ( mc, "messageCenter" );
try {
// System.setOut ( new PrintStream ( new FileOutputStream
( "messageCenter_out.log" ) ) );
// System.setErr ( new PrintStream ( new FileOutputStream
( "messageCenter_err.log" ) ) );
} catch ( Exception e ) {
System.out.println ( "Couldn't set out- and err- streams." );
e.printStackTrace ();
}
t.start ();
}
Vector clients = new Vector ();
public void run() {
try {
ServerSocket ss = new ServerSocket ( 6401 );
while ( true ) {
System.out.println ( "accepting connections on port
6401" );
Socket socket = ss.accept ();
clients.add ( new Client ( socket ) );
System.out.println ( "new client, we have " +
clients.size () + " clients" );
}
} catch ( Exception e) {
e.printStackTrace ();
}
}
int lastMessageIndex = 0;
String [] lastMessages = new String [5];
public class Client extends Thread {
Thread t1, t2;
boolean somethingToWrite, running = true;
String line;
PrintStream printoutput;
public Client ( final Socket socket ) {
final Client self = this;
try {
printoutput = new PrintStream
( socket.getOutputStream () );
} catch ( IOException e ) {
System.out.println ( "Couldn't get OutputStream !" );
e.printStackTrace ();
}
t1 = new Thread () {
public void run () {
try {
InputStream in = socket.getInputStream ();
DataInputStream datainput = new DataInputStream
( in );
while ( running ) {
String line = datainput.readLine ();
if ( line == null ) break;
for ( Iterator it=clients.iterator();
it.hasNext(); ) {
Client client = ( Client ) it.next ();
client.write ( line );
}
}
} catch ( Exception e ) {
System.out.println ( "client probably died..." );
running = false;
} finally {
clients.remove ( self );
running = false;
System.out.println ( "removed a client, we
have " + clients.size () + " clients" );
}
}
};
t1.start ();
}
public void write ( String line ) {
printoutput.println ( line );
printoutput.flush ();
}
}
}