Chat Server

I am attempting to make a multi user chat server with this code. I'm having a problem with sending the message from one user to each other user. I think I'm going in the right direction but I'm getting runtime errors on line 24, NoSuchMethodError. This is most likely due to the attempt of getting the clientArray into the CClient class. If there's an alternative route someone can point me to, I'm open to it. Any other suggestions/comments would be fantastic.

import java.io.IOException;

import java.net.*;

import java.io.*;

import java.util.*;

publicclass ChatServer{

publicstaticint SHCCIS_PORT = 11337;

publicstaticvoid main(String[] args){

int port = SHCCIS_PORT;

ServerSocket serverSocket =null;

Socket sock =null;

CClient[] clientArray =new CClient[20];

try{

serverSocket=new ServerSocket(port);

int i=0;

while(true){

sock = serverSocket.accept();

clientArray[i] =new CClient(sock, clientArray);

i++;

}

}catch(IOException ioe){System.out.println("You screwed up...big time\n"+ioe);}

finally{

try{

serverSocket.close();

}catch(IOException ioe){

System.out.println("something broke\n" +ioe);

}

}

}

}

class CClientimplements Runnable{

private Socket sock;

private BufferedReader in;

private PrintWriter out;

Thread t;

CClient[] clientArray;

public CClient(Socket socket, CClient[] cl){

clientArray = cl;

try{

this.sock = socket;

in =new BufferedReader(new InputStreamReader(socket.getInputStream()));

out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

}catch(IOException ioe){System.out.println("You screwed up...big time\n"+ioe);}

t =new Thread(this);

t.start();

}

publicvoid run(){

String message;

try{

while(!(message = in.readLine()).contains("/quit")){

for(int i=0;i<19;i++){

clientArray[i].out.println(message);

clientArray[i].out.flush();

System.out.println(message);

}

}

}catch(IOException ioe){System.out.println("error\n"+ioe);}

finally{

try{

in.close();

out.close();

sock.close();

}catch(IOException ioe){System.out.println("error problem\n"+ioe);

}finally{

}

}

}

}

[5375 byte] By [WillDoesNotCarea] at [2007-11-27 3:31:26]
# 1
looks like you're really close.... you just need to get rid of a few lines
friendofbawlsa at 2007-7-12 8:34:27 > top of Java-index,Java Essentials,Java Programming...
# 2

Design and coding would be simpler if your CClient is an inner class of the server. Try and study this:

/* beware: exception handling is far from perfect */

import java.net.*;

import java.io.*;

import java.util.*;

public class ChatServer2 {

public static final int SHCCIS_PORT = 11337;

ServerSocket serverSocket;

Vector<CClient2> clientList; // synchronized list

int cCount;

boolean go;

ChatServer2 server;

public ChatServer2() throws Exception{

serverSocket = new ServerSocket(SHCCIS_PORT);

clientList = new Vector<CClient2>();

cCount = 0;

go = true;

server = this;

}

void acceptClients() throws Exception{

while (go){

Socket sock = serverSocket.accept();

CClient2 client = new CClient2(sock);

clientList.add(client);

++cCount;

new Thread(client).start();

}

serverSocket.close();

}

public void stopServer(){

go = false;

}

synchronized void broadCastMessage(String msg){

for (CClient2 c : clientList){

c.pw.println(msg);

}

}

synchronized void decrCount(){

--cCount;

}

public static void main(String[] args) {

try{

ChatServer2 cs = new ChatServer2();

cs.acceptClients();

}

catch (Exception e){

e.printStackTrace();

}

}

class CClient2 implements Runnable{

Socket csock;

BufferedReader br;

PrintWriter pw;

public CClient2(Socket socket){

csock = socket;

try{

br = new BufferedReader(new InputStreamReader(csock.getInputStream()));

pw = new PrintWriter(csock.getOutputStream(), true); // auto-flush

}

catch(IOException ioe){

ioe.printStackTrace();

}

}

public void run(){

String message;

try{

while ((message = br.readLine()) != null){ // must check EOF

if (message.contains("/quit")){

break;

}

else {

server.broadCastMessage(message);

}

}

clientList.remove(this);

server.decrCount();

br.close();

pw.close();

}

catch(IOException ioe){

ioe.printStackTrace();

}

}

}

}

hiwaa at 2007-7-12 8:34:27 > top of Java-index,Java Essentials,Java Programming...
# 3
That's a practical use of inner classes. I would have done the same, but using a server reference passed to the constructor.
Peetzorea at 2007-7-12 8:34:27 > top of Java-index,Java Essentials,Java Programming...
# 4
> server reference passed to the constructorIf it is a stand-alone class, not an inner, that's a good idea.
hiwaa at 2007-7-12 8:34:27 > top of Java-index,Java Essentials,Java Programming...