user7196175
user7196175

Reputation:

Clients can't communicate with each other

I wrote a chat program that lets multiple users chat. The problem is clients cannot see what the other clients typed. The server is functioning well it accepts multiple clients and their inputs. I created an ArrayList to store the list of clients in my server for them to see what the other client typed but it doesn't seem to be working.

Here's my code

Server

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
    public class myServerSocket {

        int portNumber = 4434;
        ServerSocket serverSocket = null;
        Socket clientSocket;
        ArrayList<myServerSocketRunnable> clients = new ArrayList<myServerSocketRunnable>();

        public void runServer() {
            try {
                while(true) {
                    serverSocket = new ServerSocket(portNumber);
                }
            } catch(IOException aww) {
                System.out.println(aww.getMessage());
                aww.printStackTrace();
            }

            try {
                while(true) {
                    clientSocket = serverSocket.accept();
                    myServerSocketRunnable client = new myServerSocketRunnable(clientSocket);
                    clients.add(client);
                    new Thread(new myServerSocketRunnable(clientSocket)).start();
                }
            } catch(IOException aww) {
                System.out.println(aww.getMessage()+"\n");
            } finally {
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class myServerSocketRunnable extends Thread {

    protected Socket clientSocket = null;
    BufferedReader in;
    PrintWriter out;
    String username; 
    String message;

    public myServerSocketRunnable(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            while(true) {
                in =  new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                out = new PrintWriter(clientSocket.getOutputStream(), true);

                username = in.readLine();
                message = in.readLine();

                System.out.println(" "+username+": "+message);
                out.println(" "+username+": "+message);
                out.flush();
            }
        } catch(IOException aww) {
            System.out.println(" "+username+" has disconnected!");
            out.println(" "+username+" has disconnected!"); 
        } finally {
            try {
                in.close();
                out.close();
            } catch(IOException aww) {
                aww.printStackTrace();
            }
        }
    }

}




public class myServer {

    public static void main(String[] args) {

        myServerSocket call = new myServerSocket();

        System.out.println("=========================================");
        System.out.println("=             Messenger Server          =");
        System.out.println("=========================================");

        call.runServer();
    }
}

Client

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class Messenger {
    private static String hostName = "127.0.0.1";
    private static int portNumber = 4434;
    private static Socket clientSocket;
    private static PrintWriter out;
    private static BufferedReader in;

    private static String username, content;
    private static JFrame firstFrame, secondFrame;
    private static JTextField userName, userField, clientTextField;
    private static JTextArea clientTexts;
    private static JButton talkButton, sendButton;


    public static void firstGUI(Container pane) {
        pane.setLayout(null);

        userName = new JTextField("Username :");
        userName.setBounds(10, 35, 70, 20);
        userName.setEditable(false);

        userField = new JTextField();
        userField.setBounds(10, 55, 225, 20);

        talkButton = new JButton("Talk!");
        talkButton.setBounds(85, 100, 70, 35);
        talkButton.setFont(new Font("Verdana", Font.PLAIN, 15));

        pane.add(userName);
        pane.add(userField);
        pane.add(talkButton);
        pane.setBackground(Color.DARK_GRAY);

        talkButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                username = userField.getText();
                showFrame();
                talkButton.setEnabled(false);
                firstFrame.dispose();
            }
        });

    }

    public static void showGUI() {

        firstFrame = new JFrame("Messenger");
        firstFrame.setVisible(true);
        firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        firstFrame.setBounds(500, 100, 250, 200);
        firstFrame.setResizable(false);

        firstGUI(firstFrame.getContentPane());
    }

    public static void contentGUI(Container pane) {

        pane.setLayout(null);

        clientTexts = new JTextArea();
        clientTexts.setEditable(false);
        clientTexts.setBounds(15, 10, 465, 490);
        clientTexts.setBackground(Color.WHITE);
        clientTexts.setFont(new Font("Verdana", Font.PLAIN, 13));

        clientTextField = new JTextField();
        clientTextField.setText("");
        clientTextField.setBounds(15, 525, 360, 25);
        clientTextField.setBackground(Color.WHITE);
        clientTextField.setFont(new Font("Verdana", Font.PLAIN, 15));

        sendButton = new JButton("Send");
        sendButton.setBounds(405, 519, 75, 35);
        sendButton.setFont(new Font("Verdana", Font.PLAIN, 15));

        pane.add(clientTexts);
        pane.add(clientTextField);
        pane.add(sendButton);
        pane.setBackground(Color.DARK_GRAY);

        sendButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                content = clientTextField.getText();
                clientTextField.setText("");
                new Thread(new Runnable() {
                    public void run() {
                        try {
                            out.println(username);
                            out.println(content);
                            out.flush();
                            clientTexts.append(in.readLine()+"\n");
                        } catch (IOException aww) {
                            aww.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }

    public static void showFrame() {

        secondFrame = new JFrame("Messenger - Client");
        secondFrame.setVisible(true);
        secondFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentGUI(secondFrame.getContentPane());

        secondFrame.setBounds(550, 100, 500, 600);
        secondFrame.setResizable(false);


    }

    public static void main(String[] args) {

        try {
            showGUI();
            clientSocket = new Socket(hostName, portNumber);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch(IOException aww) {
            userField.setForeground(Color.RED);
            userField.setFont(new Font("Courier new", Font.PLAIN, 13));
            userField.setText("Server is not Online");
            userField.setEditable(false);
            talkButton.setEnabled(false);

            aww.printStackTrace();
        }

    }

}

I want to know what part of the code is wrong. Maybe a solution to resolve the problem.

Upvotes: 0

Views: 54

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

One issue is here:

clientSocket = serverSocket.accept();
myServerSocketRunnable client = new myServerSocketRunnable(clientSocket);
clients.add(client);
new Thread(new myServerSocketRunnable(clientSocket)).start();

You're creating two myServerSocketRunnable objects, one you add to the clients List and one you place in a thread and run, and that may be a serious problem. Instead create one for both:

clientSocket = serverSocket.accept();
myServerSocketRunnable client = new myServerSocketRunnable(clientSocket);
clients.add(client);

// new Thread(new myServerSocketRunnable(clientSocket)).start(); // NO
new Thread(client).start();     // YES

I don't know if this is the cause of your problems, but I do know that it's just not right and needs to be fixed. Also, as I state in comments, this while (true) loop doesn't belong:

while(true) {
    serverSocket = new ServerSocket(portNumber);
}

Just create your server socket once and move on.


Also, where does your Server broadcast data received from one Client to all the other Clients? I would assume that you would use your clients ArrayList for this, but I don't see this being used. The clients cannot magically connect to each other, and the server must provide the glue.

Upvotes: 1

Related Questions