for a school project i had to do a chat with multiple users in java. I did it, it works but i don't think is perfect. In the future i want to add a GUI, but before doing that i want to correct it. So my question is how can i improve it? What can i do better?. This is my code. On the Client side i used the main thread to wait the user input and another thread that listen to the server that wait message from another clients. On the server side i used the main thread to accept new connection request from clients and N thread that manage N clients. Please help me.
Client side:
import java.net.*;
import java.io.*;
class listener implements Runnable{
private Socket connection;
private static boolean running;
listener(Socket connection){
this.connection=connection;
running=true;
}
public void run(){
InputStreamReader in;
BufferedReader sIN;
String msgToRecv;
try{
in=new InputStreamReader(connection.getInputStream());
sIN=new BufferedReader(in);
}
catch(IOException e){
System.out.println(e);
return;
}
while(running){
try{
msgToRecv=sIN.readLine();
}
catch(IOException e){
System.out.println("Connection closed: "+e);
return;
}
System.out.println(msgToRecv);
}
return;
}
public static void stopListening(){
running=false;
return;
}
}
class Client
{
public static void main(String[] args)
{
String ipAddServer="127.0.0.1";
int port=8080;
Socket connection=null;
String msgToSend;
String username;
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader tastiera=new BufferedReader(input);
OutputStream out;
PrintWriter sOUT;
System.out.println("Enter a username");
try{
connection=new Socket(ipAddServer, port);
out=connection.getOutputStream();
sOUT=new PrintWriter(out);
username=tastiera.readLine();
}
catch(IOException e){
System.out.println(e);
return;
}
System.out.println("User: "+username);
System.out.println("Chat Joined");
Thread t=new Thread(new listener(connection));
t.start();
do{
try{
msgToSend=tastiera.readLine();
}
catch(IOException e){
System.out.println(e);
return;
}
sOUT.println(username+": "+msgToSend);
sOUT.flush();
}while(!msgToSend.equals("EXIT"));
listener.stopListening();
try
{
connection.close();
}
catch(IOException e)
{
System.out.println(e);
}
return;
}
}
Server side:
import java.net.*;
import java.io.*;
import java.util.ArrayList;
class clientHandler implements Runnable{
private ServerSocket sSocket;
private Socket connection;
private ArrayList<Socket> sockets;
public clientHandler(ServerSocket sSocket, Socket connection, ArrayList<Socket> sockets) {
this.sSocket=sSocket;
this.connection=connection;
this.sockets=sockets;
}
public void run(){
InputStreamReader in, input;
BufferedReader sIN, tastiera;
OutputStream out;
PrintWriter sOUT;
String msgToRecv;
try{
out=connection.getOutputStream();
sOUT=new PrintWriter(out);
in=new InputStreamReader(connection.getInputStream());
sIN=new BufferedReader(in);
}
catch(Exception e){
System.out.println(e);
return;
}
sockets.add(connection);
do{
try{
msgToRecv=sIN.readLine();
}
catch(IOException e){
System.out.println(e);
return;
}
for(int i=0;i<sockets.size();++i){
Socket temp=sockets.get(i);
if(!connection.equals(temp)&&!msgToRecv.equals("null")){
OutputStream tmpOut;
PrintWriter tmpSOUT;
try{
tmpOut=temp.getOutputStream();
tmpSOUT=new PrintWriter(tmpOut);
}
catch(IOException e){
System.out.println(e);
return;
}
tmpSOUT.println(msgToRecv);
tmpSOUT.flush();
}
}
}while(msgToRecv!=null);
sockets.remove(sOUT);
return;
}
}
class Server
{
public static void main(String[] args)
{
int port=8080;
ServerSocket sSocket;
ArrayList<Socket> sockets=new ArrayList<Socket>();
Thread t;
try{
sSocket=new ServerSocket(port);
}
catch(IOException e){
System.out.println(e);
return;
}
while(true){
try{
t=new Thread(new clientHandler(sSocket, sSocket.accept(), sockets));
}
catch(IOException e){
System.out.println(e);
return;
}
t.start();
}
}
}