multiples Thread client, server using socket

Asked

Viewed 184 times

0

Hello I’m doing a job for college and I’m doubtful how to add a new thread to add another client...

basically what I want to do is after started the server is possible to send message from more than one client, and distinct

the system is a proof of concept of an auction system

follows what was developed up to the moment


import java.net.*;
import java.io.*;
import java.util.Scanner;

public class CliThread extends Thread {

    static DataOutputStream outStream = null;
    DataInputStream inStream = null;

    static String host = "";
    static int port = 9090;
    Socket socket = null;
    String MRcv= "";
    static String MSnd = "";


    CliThread(){
        try {
            socket = new Socket("localhost", port);//conecta com o servidor.
            System.out.println("Conectado ao servidor...");
            this.start();//comeca uma nova thread. O metodo run é executado.
            outStream = new DataOutputStream(socket.getOutputStream());
            inStream = new DataInputStream(socket.getInputStream());
            Scanner sc = new Scanner(System.in);

            while(true){
                System.out.println("Digite o seu lance para o produto: ");
                String MSnd = sc.nextLine();//le mensagem do console.
                outStream.writeUTF(MSnd);//manda mensagem para o servidor.
                outStream.flush();
            }
        } catch(Exception e) {System.out.println(e);}
    }




    public void run(){
        while (true) {
            try {
                MRcv = inStream.readUTF();//le mensagem do servidor.
                System.out.println("Servidor: " + MRcv);
            } catch(Exception e) {}
        }
    }


    public static void main(String args[]){
        new CliThread();

    }
}   

server

    import java.net.*;
import java.io.*;
import java.util.Scanner;

class SrvThread extends Thread {

    static int port = 9090;

    static DataOutputStream outStrem = null;

    static String MSnd= ""; //used in client

    Socket socket = null;
    ServerSocket serverSocket = null;

    DataInputStream inStrem = null;

    String MRcv= "";


    SrvThread(){ //init
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("Aguardando conexão com o cliente...");
            socket = serverSocket.accept();//aguarda conexao com o cliente.
            System.out.println("Conexão Estabelecida.");
            outStrem = new DataOutputStream(socket.getOutputStream());
            inStrem = new DataInputStream(socket.getInputStream());

            this.start();//inicia uma nova thread. O metodo run é executado.

            Scanner sc = new Scanner(System.in);
            while(true){
                System.out.println("Server Send: ");
                String MSnd = sc.nextLine(); //le string do console
                outStrem.writeUTF(MSnd);//envia string para o cliente.
                outStrem.flush();
            }
        } catch(Exception e){
            System.out.println(e);
        }
    }

    public void run(){
        try {
            while(true){
                MRcv = inStrem.readUTF();//le as strings do cliente
                System.out.println("Comprador1: "+MRcv);
            }
        } catch(Exception e) {
            System.out.println(e);
        }
    }

    public static void main(String args[]){
        new SrvThread();
    }
}

1 answer

0

Install more than one in the main method

public static void main(String args[]){
    new CliThread(); // cliente 1
    new CliThread(); // cliente 2
}

Browser other questions tagged

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