Chat with multiple customers using Threads and Sockets

Asked

Viewed 385 times

0

The code here posted represents a small chat between client and server. As 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. You could 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()); 
                } 
            }
        }
    }
}
  • How difficult?

  • The difficulty is that I don’t know how to get multiple clients to communicate and I also have no sense of threads.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.