This is for a school project. So far, this code works for one server and one client. However, I'm still learning exactly how everything works. I'm not sure if the "server" would be considered as the current user while everyone else connected is considered as the client? With that being said, my thinking is that all the juicy stuff (usernames and broadcast messages) would be in the server class... but honestly, I have no idea and I've been looking things up all day.

Client Class:

package chatroom_secure;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class Chat_Client extends javax.swing.JFrame {
static Socket s;
static DataInputStream din;
static DataOutputStream dout;

public Chat_Client() {
    initComponents();
}

private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         

    try {
        String msgout ="";
        msgout = msg_text.getText().trim();
        msg_area.setText(msg_area.getText().trim()+"\nClient:  "+msgout);
        dout.writeUTF(msgout);
    }catch(Exception e){
     
    }
}

public static void main(String args[]) throws IOException {

    InetAddress ca = InetAddress.getLocalHost();  
    String str = ca.getHostAddress(); 
    s = new Socket(str,1201);
    
    din = new DataInputStream(s.getInputStream());
    dout = new DataOutputStream(s.getOutputStream());
   
    new Chat_Client().setVisible(true);
    
    try{
        while(true){
            String msgin="";
            if(!msgin.equals("exit")){
                msgin= din.readUTF();
                msg_area.setText(msg_area.getText().trim()+"\n"+Chat_Server.name+":  "+msgin);
            }
            else break;
        }
    }catch(Exception e){
        
    }finally{
        din.close();
        dout.close(); 
    }
}
}

Server Class:

package chatroom_secure;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import static java.lang.System.in;
import static java.lang.System.out;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Chat_Server extends javax.swing.JFrame {

private static final Set<String> names = new HashSet<>();

static ServerSocket ss;
static Socket s;
static DataInputStream din;
static DataOutputStream dout;

public Chat_Server() {
    initComponents();
}

private void msg_sendActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {
        String msgout = "";
        msgout = msg_text.getText().trim();
        msg_area.setText(msg_area.getText().trim() + "\n" + name + ":  " + msgout); 
        dout.writeUTF(msgout); 
    } catch (Exception e) {
        
    }
}         
                           
static String name;

public static void main(String args[]) throws IOException {

    String msgin = "";
    try {

        while (true) {
            Scanner input = new Scanner(System.in);
            out.println("SUBMITNAME");
            name = input.next();
            if (name == null) {
                return;
            }
            synchronized (names) {
                if (name != null || !name.trim().isEmpty()) {
                    names.add(name);
                    break;
                } else {
                    System.out.println("Sorry this name isn't unique to the chat. Please Try again.");
                }
            }
        }

        new Chat_Server().setVisible(true);
        ss = new ServerSocket(1201); //Server starts at 1201 port number
        s = ss.accept(); //now server will accept the connections

        din = new DataInputStream(s.getInputStream());
        dout = new DataOutputStream(s.getOutputStream());

        while (!msgin.equals("exit")) {
            msgin = din.readUTF();
            msg_area.setText(msg_area.getText().trim() + "\nClient:  " + msgin); 
        }

    } catch (Exception e) {

    } finally {
        din.close();
        dout.close();
    }
}              
}
0

There are 0 best solutions below