Multiple client chat using Threads and Sockets

0

The code posted here represents a small chat between client and server. Like I said, I'm not familiar with threads.

The teacher told us from this code to create a multi-client chat, where clients send and receive messages and the only function of the server is to receive the message from a client and pass it on to others. Could you help me?

Server Class

import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Servidor {
    public static void main (String args[]) {
        try{
            int serverPort = 7896; 
            ServerSocket listenSocket = new ServerSocket(serverPort);
            while(true) {
                Socket clientSocket = listenSocket.accept();
                Connection c = new Connection(clientSocket);
            }
        } 
        catch(IOException e) {
            System.out.println("Listen :"+e.getMessage());
        }
    }
}

class Connection extends Thread {
    DataInputStream in;
    DataOutputStream out;
    Socket clientSocket;
    public Connection (Socket aClientSocket) {
        try {
            clientSocket = aClientSocket;
            in = new DataInputStream( clientSocket.getInputStream());
            out =new DataOutputStream( clientSocket.getOutputStream());
            this.start();
        } 
        catch(IOException e)  {
            System.out.println("Connection:"+e.getMessage());
        }
    }
    public void run(){
        try {
            while(true) {
                String data = in.readUTF();                  
                System.out.println("Mensagem recebida do cliente: "+data);
                Scanner sc = new Scanner(System.in);
                System.out.print("Enviar mensagem do servidor: ");
                String msg = sc.nextLine();
                out.writeUTF(msg);
            }
        } 
        catch(EOFException e) {
            System.out.println("EOF:"+e.getMessage());
        } 
        catch(IOException e) {
            System.out.println("IO:"+e.getMessage());
        } 
        finally{ 
            try {clientSocket.close();
            }
            catch (IOException e)
            {/*close failed*/}
        }
    }

}

Client Class

package chat;

import java.net.*;
import java.util.Scanner;
import java.io.*;
public class Cliente {
    public static void main (String args[]) {
        // arguments supply message and hostname of destination
        Socket s = null;
        try{
            int serverPort = 7896;
            s = new Socket("localhost", serverPort);
            DataInputStream in = new DataInputStream( s.getInputStream());
            DataOutputStream out = new DataOutputStream( s.getOutputStream());
            Scanner sc = new Scanner(System.in);

            while(true) {
                System.out.print("Enviar mensagem do cliente: ");
                String msg = sc.nextLine(); 
                out.writeUTF(msg);        
                String data = in.readUTF();
                System.out.println("Mensagem recebida do servidor: "+ data);                
            }

        } 
        catch (
                UnknownHostException e){
            System.out.println("Sock:"+e.getMessage()); 
        } 
        catch (EOFException e){ 
            System.out.println("EOF:"+e.getMessage());
        } 
        catch (IOException e){ 
            System.out.println("IO:"+e.getMessage());
        } 
        finally { 
            if(s!=null) { 
                try { s.close(); 
                } 
                catch (IOException e){
                    System.out.println("close:"+e.getMessage()); 
                } 
            }
        }
    }
}
    
asked by anonymous 24.08.2018 / 14:02

0 answers