...

View Full Version : Help: multi client server applet



zwebie
01-28-2010, 07:54 PM
Hi guys,

I'm trying to make a little twitter applet and I have 2 problems:

1. for some reason I am unable to delete the user when he closes the connection

2. I am using the TasksExecutor to refer the client to a connection handler now my question is when one client updates his status how do I write the new states to all users?

any advice will be much appreciated.

here is the code:

server:


import java.net.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.concurrent.*;
import java.io.*;

public class TwitServer {
private int port;
public Hashtable<String, String> users = null;
public ExecutorService tasks;
public ArrayList <Socket> sockets;

public TwitServer(int port) {
this.port = port;
users = new Hashtable<String, String>();
sockets = new ArrayList<Socket>();
}
public int getPort() {
return(port);
}
public void listen() {
int poolSize = 10 * Runtime.getRuntime().availableProcessors();
tasks = Executors.newFixedThreadPool(poolSize);
try {
ServerSocket listener = new ServerSocket(port);
Socket socket;
while(true) { // Run until killed
socket = listener.accept();
tasks.execute(new ConnectionHandler(socket,this));
}
} catch (IOException ioe) {
System.err.println("IOException: " + ioe);
//ioe.printStackTrace();
}
}


}

connection handler:


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public class ConnectionHandler implements Runnable {
private Socket socket = null;
private TwitServer server = null;
boolean firstConnect = true;
String userName;
ObjectInputStream reader = null;
ObjectOutputStream write = null;

public ConnectionHandler(Socket socket, TwitServer server) {
this.socket = socket;
this.server = server;
firstConnect = true;
userName = null;
}

public void run() {

while (!socket.isClosed()) {
try {
handleConnection(socket);
} catch (Exception ioe) {
System.err.println("Exception: " + ioe);
}
}
stop();
}

public synchronized void handleConnection(Socket socket)
throws IOException, ClassNotFoundException, InterruptedException{
System.out.println("handling connection " + socket.getInetAddress());
write = new ObjectOutputStream(socket.getOutputStream());
reader = new ObjectInputStream(socket.getInputStream());
Form user = (Form)reader.readObject();
if(firstConnect){
if(server.users != null && server.users.containsKey(user.userName)){
write.writeObject(new Exception());
socket.close();
return;
}
else{
firstConnect = false;
userName = user.userName;
server.users.put(user.userName,user.status);
server.sockets.add(socket);
write.writeObject(server.users);
notifyAll();
//updateSockets();
}
}
else {
server.users.put(user.userName,user.status);
write.writeObject(server.users);
//updateSockets();
}
wait();

}
public void stop(){
if (server.users != null){
server.users.remove(userName);
server.sockets.remove(socket);
} else{
System.err.println("users = NULL");
}


}



Thank you,

Noam.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum