teckid1991 0 Newbie Poster

Im working on this project for my class and what to make a chat thing for the network of computers in the room... i have everything set up correctly im just having trouble figureing out how to brodcast to all users. I assumed using a Vector and Threads would work but im still having problems with my code....

any ideas on what im doing wrong?

Code (Server):

/*
 * Main.java
 *
 * Created on April 21, 2007, 9:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jimserver;

import java.net.*;
import java.io.*;

/**
 *
 * @author Robert Mayo
 */
public class JIMServer {
    
    public static void main(String[] args)throws IOException{
        ServerSocket ssock = null;
                
        try{
            ssock = new ServerSocket(6789);      
        }catch(IOException e){
            System.err.println("Couldnt listen to port");
            System.exit(1);
        }  
        
        while(true){
            new JIMServerThread(ssock.accept()).start();
           }
    
        //ssock.close();
  
  }
    
}

Code(Thread):

/*
 * JIMServerThread.java
 *
 * Created on April 25, 2007, 9:43 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jimserver;

import java.net.*;
import java.io.*;
import java.util.Vector;

/**
 *
 * @author Robert
 */
public class JIMServerThread extends Thread {
    // vector will store the clients
    static Vector clients = new Vector(25);
    
    // io objects and connection socket
    private Socket connectionSocket = null;
    private   DataOutputStream out = null;
    private   BufferedReader bufIn = null;
    
    
    /** Creates a new instance of JIMServerThread */
    public JIMServerThread(Socket client) throws IOException {
        super("new client");
        connectionSocket = client;
        out  = new DataOutputStream(connectionSocket.getOutputStream());
        bufIn  = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

    }
   
    public void run(){
            
            synchronized(clients){clients.addElement(connectionSocket);}
            
            String message;
            try{
                    
                    do{
                        message = bufIn.readLine();
                        System.out.println(message);
                        if(!(message).equals("/exit"))
                            break;
                        for(int i = 0; i < clients.size(); i++){
                            synchronized(clients){
                                JIMServerThread jimClient = (JIMServerThread)clients.elementAt(i);
                                jimClient.out.writeBytes(message + "\n");
                            }
                        }                        
                    }while(true);

                    connectionSocket.close();
                    
                
            }catch(IOException e){
                System.err.println("IO Error: "  + e);
                System.exit(1);
            }
    }
   
}

Code(Client):

/*
 * Main.java
 *
 * Created on April 13, 2007, 11:09 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Main;

import java.net.* ;
import java.io.*;

/**
 *
 * @author Robert Mayo
 */
public class JIMClient {
   
   static String message, fromServer;

    public static void main(String args[]){
       
        String ClientName = null;

        try{
            // Input for the System input
            BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in ));

            //client sever
            Socket ctSocket = new Socket(InetAddress.getLocalHost().getHostName(), 6789);

            //Input and Output stream reader objects
            DataOutputStream out = new DataOutputStream( ctSocket.getOutputStream());
            BufferedReader innet = new BufferedReader(new InputStreamReader(ctSocket.getInputStream()));
            
            System.out.println("What is your UserName?");
            ClientName = bufIn.readLine();
            
            System.out.println("Message: ");
            message = bufIn.readLine();
            
            do{
                if(message.equals("/bye"))
                    break;
                out.writeBytes(ClientName + " says " + message + "\n");
                fromServer = innet.readLine();
                System.out.println(fromServer);
                System.out.println("Message: ");
            }while(!(message = bufIn.readLine()).equals(null));
            
            System.out.println("Good bye.");
            
            ctSocket.close();
            out.close ();
            innet.close();
            bufIn.close();
           
        }catch(UnknownHostException e){
            System.err.println("Unknown Host");
            System.exit(1);
        }catch(IOException e){
            System.err.println(e.getMessage());
            System.exit(1);
        }      
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.