package pkg2.pkg3;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
class pachet {
double adresaS;
double adresaC;
String com;
String raspuns;
pachet(double adrS, double adrC, String c, String r) {
adresaS = adrS;
adresaC = adrC;
com = c;
raspuns = r;
}
}
class Client extends Thread {
pachet p;
double adresaServer = 1234;
double adresaClient = 5678;
String comanda = "null";
public String citire() throws IOException {
System.out.println("Commands: Help, Information");
System.out.println("Input your comand: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String com = br.readLine();
return com;
}
//pachet P=new pachet(adresaServer,adresaClient,comanda);
public void run() {
System.out.println("Hi!");
try {
comanda = citire();
p = new pachet(adresaServer, adresaClient, comanda, null);
(new mediu(p)).start();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
//p = new pachet(adresaServer, adresaClient, comanda, null);
//(new mediu(p)).start();
}
public static void afis(pachet y) {
pachet n = y;
System.out.println(n.raspuns);
}
}
class mediu extends Thread {
pachet pach;
mediu i;
mediu(pachet p) {
pach = p;
}
public void run() {
(new server(pach)).start();
}
public static void p(pachet h) {
pachet w = h;
Client.afis(w);
}
}
class server extends Thread {
String a = "Help";
String b = "Information";
pachet pac;
server(pachet p) {
pac = p;
}
public void run() {
if (a.equals(pac.com)) {
//System.out.println("S-a cerut ajutor!");
pac.raspuns = "Help was asked";
//(new mediu(pac)).start();
mediu.p(pac);
} else if (b.equals(pac.com)) {
pac.raspuns = "Information were asked";
mediu.p(pac);
} else {
pac.raspuns = "Didn't find command";
mediu.p(pac);
}
}
}
public class Main {
public static void main(String[] args) {
for(int i=0; i<3; i++) {
(new Client()).start();
}
}
}
If I run the code it will ask three times, how can I make the Client start the second thread and not pass them after the loop?
Hi!
Commands: Help, Information
Input your comand:
Hi!
Commands: Help, Information
Input your comand:
Hi!
Commands: Help, Information
Input your comand: