Reputation: 43
I have to write a java client-server application, that simulates cars in garage. In the client side I shall have a small frame with 2 buttons - one to enter the garage and another to leave it. In the below code, you can see what I did. But when I run this and press the enter button my frame stuck. Any ideas what is wrong in my code? There is no error generated from the program.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Form extends JFrame implements ActionListener{
JButton btnEnter = new JButton("Enter Garage");;
JButton btnLeave = new JButton("Leave Garage");;
Client client = new Client();
public void start(){
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.CENTER);
layout.setHgap(25);
layout.setVgap(85);
btnEnter.addActionListener(this);
btnLeave.addActionListener(this);
JPanel content = new JPanel();
content.setLayout(layout);
content.add(btnEnter);
content.add(btnLeave);
setContentPane(content);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Welcome to the Garage");
setLocationRelativeTo(null);
setSize(500,250);
setVisible(true);
}
public void actionPerformed(ActionEvent e1) {
if(e1.getSource().equals(btnEnter)){
client.enter();
}
if(e1.getSource().equals(btnLeave)){
client.leave();
}
}
}
public class Client{
Socket socket;
PrintWriter out;
BufferedReader in;
String name = "Client";
Client() {
}
public void connect() throws IOException {
String server = null;
try {
InetAddress addr = InetAddress.getByName(server);
socket = new Socket(addr, 8080);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
} catch (UnknownHostException e) {
e.printStackTrace();
socket.close();
} catch (IOException e) {
e.printStackTrace();
socket.close();
}
}
public void enter(){
String stat = "";
try{
connect();
} catch(IOException e){
e.printStackTrace();
}
send("Client");
send("init_car");
//Taking Garage Stage
send("garage1");
try{
while(!stat.equals("garage_taken")){
System.out.println("taking??");
send("chk_stat");
stat = receive();
System.out.println(stat);
Thread.sleep(500);
}
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
}
public void leave(){
String stat = "";
send("garage2");
try{
while(!stat.equals("garage_released")){
send("check_status");
stat = receive();
Thread.sleep(500);
}
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
send("/quit");
}
public void send(String s) {
if (s.equals("/quit")) {
//g.serv.append("Closing connection with server...\n");
out.print("/quit");
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
//g.serv.append("Connection closed. You can close the window now.\n");
System.exit(0);
} else
out.println(s);
}
public String receive() throws IOException {
String s = "";
try {
s = in.readLine();
} catch (IOException e) {
socket.close();
}
return s;
}
public static void main(String args[]) {
new Form().start();
/*
try {
client.start();
}catch (InterruptedException e){
e.printStackTrace();
}*/
}
}
Please help me with this. I spent many hours trying to understand what is wrong, but since I am not experienced with Java I don't find a solution :(
I will post the rest of my code here. May be this will help:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
class OneClient extends Thread {
private Socket clSocket;
private BufferedReader in;
private PrintWriter out;
private String name="";
private String carStatus = "";
private Garage garage;
OneClient(Socket s, Garage gr) throws IOException{
clSocket = s;
garage = gr;
in = new BufferedReader(new InputStreamReader(clSocket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(clSocket.getOutputStream())),true);
start();
}
public void run(){
try{
while(true){
String str = in.readLine();
if(null==str || str.equals("/quit")) {
break;
}
if(name.equals("")) {
name=str;
System.out.println("\nNew name for a client: " + name);
Thread.currentThread().setName(name);
} else {
if(str.equals("init_car")){
System.out.println(name + " is comming to the garage.");
carStatus = str;
} else if(str.equals("garage1")){
carStatus = str;
System.out.println(name + " try to enter the garage...");
str = garage.takeGarage(name);
carStatus = str;
} else if(str.equals("garage2")){
carStatus = str;
str = garage.releaseGarage();
carStatus = str;
} else if(str.equals("check_status")){
out.println(carStatus);
}
}
}
} catch(IOException e){
} finally {
try{
clSocket.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
}
public class Server {
static final int PORT = 8080;
static ServerSocket servSocket;
static Garage garage;
public static void main(String args[]) throws IOException{
servSocket = new ServerSocket(PORT);
Timer timer = new Timer();
garage = new Garage();
//TIMER TASK FOR BOTS
timer.schedule(new TimerTask(){
public void run(){
new BotClient().runBots();
}
}, 1000, 2000);
System.out.println("Server is started!\n");
try{
while(true){
Socket s = servSocket.accept();
try{
new OneClient(s, garage);
} catch(IOException e){
e.printStackTrace();
s.close();
}
}
} finally {
servSocket.close();
}
}
}
public class Garage {
static boolean isGarageFree;
static int counter=0;
static int limit=30;
Garage(){
isGarageFree = true;
}
public String takeGarage(String name){
Thread.currentThread().setName(name);
counter++;
if(counter>limit) isGarageFree = false;
else isGarageFree = true;
while(!isGarageFree){
try{
Thread.sleep(500);
} catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep( (int) (Math.random()*500 + 1500) );
} catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Garage is taken by " + name + ".");
System.out.println("\nThere are " + counter + " cars in the garage.\n");
return "garage_taken";
}
public String releaseGarage(){
try{
Thread.sleep( (int) (Math.random()*5000 + 2000));
System.out.println("\n" + Thread.currentThread().getName() + " is releasing the garage...");
} catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Garage is Released by " + Thread.currentThread().getName() + "!\n");
counter--;
System.out.println("\nThere are " + counter + " cars in the garage.\n");
return "garage_released";
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class BotClient extends Thread{
static int count = 0;
BufferedReader in;
PrintWriter out;
InetAddress addr;
Socket socket;
String name = "";
BotClient(){
}
void send(String s){
if(s.length()==0){
out.println("END");
System.out.println("Closing...");
try{
socket.close();
}
catch (Exception expt){
System.out.println(expt);
}
System.exit(0);
}
else out.println(s);
}
String receive() throws IOException{
return in.readLine();
}
public void init() throws IOException{
count++;
name = "Car " + count;
this.setName(name);
try{
String server = null;
InetAddress addr = InetAddress.getByName(server);
socket = new Socket(addr, Server.PORT);
in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
}catch (Exception e){
System.out.println("Exception: "+e);
System.out.println("Closing...");
socket.close();
}
}
public void run(){
String stat = "";
try{
init();
} catch(IOException e){
e.printStackTrace();
}
send(name);
send("init_car");
send("garage1");
try{
while(!stat.equals("garage_taken")){
send("check_status");
stat = receive();
Thread.sleep(500);
}
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
send("garage2");
try{
while(!stat.equals("garage_released")){
send("check_status");
stat = receive();
Thread.sleep(500);
}
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
send("/quit");
}
public void runBots(){
new BotClient().start();
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
I am quite sure problem is somewhere in the Frame class, because if I run the client class without using JFrame it is working.
Upvotes: 3
Views: 1746
Reputation: 22074
The gui freezes, because you are looping in the leave
function. If you want your GUi to stay responsive, you should consider using a SwingWorker
thread. However, this will not help if your server doesn't send the proper response, but at least the GUI will stay updated and responsive.
Try something like this:
public void leave()
{
SwingWorker<Void, Void> w = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground() throws Exception
{
String stat = "";
send("garage2");
try{
while(!stat.equals("garage_released")){
send("check_status");
stat = receive();
Thread.sleep(500);
}
} catch(IOException e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
send("/quit");
return null;
}
};
w.execute();
// here you can block to wait for the server to be done or show some info dialog, which in turn can be closed from the thread when done
}
Upvotes: 1