views:

59

answers:

2

Essentially I have built a socket server that multiple clients can connect to and when they send a message to the server it's sent to every other client. The problem is it's not working yet. I need to sort out "SingletonClients" and "getClients" so they return all of the connected users ready for me to send messages to them. It's only a basic problem but I have no idea at all how to do it and it's the very last part of a student project that I am doing (The main part is an Obj-C client and not a Java Server). If anyone could do it for me then I would be eternally grateful.

Heres the code so far:

import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedList;
import java.io.*;
import java.net.*;

class ClientWorker implements Runnable {
    private Socket client;
    private JTextArea textArea;
    BufferedReader in = null;
    PrintWriter out;

    ClientWorker(Socket client, JTextArea textArea) {
        this.client = client;
        this.textArea = textArea;  

        String line = in.readLine();
        LinkedList<ClientWorker> clients = SingletonClients.getClients();
        for(int i = 0; i < clients.size(); i++) {
            ClientWorker c = clients.get(i);
            //The client doesn't need to get it's own data back.
            if(c == this){
                continue;
            }
            c.writeString(line);
        }

    }

    public void writeString(String s) {
        try {
            out.println(s);
        } catch(IOException ex) {
        }
    }

    public void run(){
        String line;
        out = null;
        try{
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            System.out.println("in or out failed");
            System.exit(-1);
        }

        while(true){
            try{
                line = in.readLine();
                //Send data back to client
                out.println(line);
                textArea.append(line);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
            }
        }
    }
}

class SocketThrdServer extends JFrame{

    JLabel label = new JLabel("Text received over socket:");
    JPanel panel;
    JTextArea textArea = new JTextArea();
    ServerSocket server = null;

    SocketThrdServer(){ //Begin Constructor
        panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(Color.white);
        getContentPane().add(panel);
        panel.add("North", label);
        panel.add("Center", textArea);
    } //End Constructor

    public void listenSocket(){
        try{
            server = new ServerSocket(4444); 
        } catch (IOException e) {
            System.out.println("Could not listen on port 4444");
            System.exit(-1);
        }
        while(true){
            ClientWorker w;
            try{
                w = new ClientWorker(server.accept(), textArea);
                Thread t = new Thread(w);
                t.start();
            } catch (IOException e) {
                System.out.println("Accept failed: 4444");
                System.exit(-1);
            }
        }
    }

    protected void finalize(){
        //Objects created in run method are finalized when 
        //program terminates and thread exits
        try{
            server.close();
        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }

    public static void main(String[] args){
        SocketThrdServer frame = new SocketThrdServer();
        frame.setTitle("Server Program");
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        frame.addWindowListener(l);
        frame.pack();
        frame.setVisible(true);
        frame.listenSocket();
    }
}

Thanks in advance!

+1  A: 
LinkedList<ClientWorker> clients = SingletonClients.getClients();

Since it seems like you need something that returns a collection of ClientWorker I suggest you look through your code for a place where you create ClientWorkers and try and put them in a collection.

Does that make sense?

willcodejavaforfood
A: 

You will want to pay attention to the beginning of your ClientWorker class. In your fields declaration section, you declare some instance members like so:

BufferedReader in = null;
PrintWriter out;

but then in your constructor, you try to use them without initializing them first:

    String line = in.readLine(); //will throw a NullPointerException
    LinkedList<ClientWorker> clients = SingletonClients.getClients();
    for(int i = 0; i < clients.size(); i++) {
        ClientWorker c = clients.get(i);
        //The client doesn't need to get it's own data back.
        if(c == this){
            continue;
        }
        c.writeString(line); //calls out.println, will throw a NullPointerException
    }

With regards to your SingletonWorker, if this is part of the project, you are probably being asked to make a singleton in Java. This is not a big deal, but there is obviously one thing that you need to ensure - only allowing one instance of your Singleton to be created during the life of your process. Take a look here for some ideas. If, on the other hand, the SingletonWorker is a helper that you have devised, you might be better off following @willcodejavaforfood's lead and making a type-safe LinkedList.

akf