sfg
sfg

Reputation: 13

Java Socket sends data only for the first time

I making a client server application. I start the server and then the client. When i click any button in ClientGui class it sends the message but when i click any other button again the data is not sent. I send the data throw the following lines of code.

btn1.setOnAction(e->{
            dout.println("x1");
        });
btn2.setOnAction(e->{
            dout.println("x2");
        });

They only work and send data for the first time.The following is the ClientGui class code.

import java.io.PrintStream;
import java.io.DataInputStream;
import java.net.Socket;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.application.Platform;

public class ClientGui extends AnchorPane {

    protected final Button btn1;
    protected final Button btn2;
    volatile String message = "";
    volatile String recievedMessage = "";
    static Socket s;
    static DataInputStream din;
    static PrintStream dout;

    public ClientGui() {
        btn1 = new Button();
        btn2 = new Button();
        
        try{
            s = new Socket("localhost", 5555);
            din = new DataInputStream(s.getInputStream());
            dout = new PrintStream(s.getOutputStream());
        }catch(Exception ex){}

        setId("AnchorPane");
        setPrefHeight(400.0);
        setPrefWidth(490.0);

        btn1.setAlignment(javafx.geometry.Pos.CENTER);
        btn1.setLayoutX(118.0);
        btn1.setLayoutY(86.0);
        btn1.setMnemonicParsing(false);
        btn1.setPrefHeight(48.0);
        btn1.setPrefWidth(75.0);
        btn1.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        btn1.setFont(new Font(24.0));
        btn1.setOnAction(e->{
            dout.println("x1");
        });

        btn2.setAlignment(javafx.geometry.Pos.CENTER);
        btn2.setLayoutX(221.0);
        btn2.setLayoutY(86.0);
        btn2.setMnemonicParsing(false);
        btn2.setPrefHeight(48.0);
        btn2.setPrefWidth(75.0);
        btn2.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
        btn2.setFont(new Font(24.0));
        btn2.setOnAction(e->{
            dout.println("x2");
        });

        getChildren().add(btn1);
        getChildren().add(btn2);

        ClientHandler ch = new ClientHandler();
        ch.start();
    }
    
    class ClientHandler extends Thread{
        
        public void run(){
            while(true){
                
                try{
                    if(message.equals("stop"))
                        break;
                    recievedMessage = din.readLine();
                    playMove(recievedMessage);
                }catch(Exception ex){}
             
                try{
                    din.close();
                    dout.close();
                    s.close();
                }catch(Exception ex){}
            }
        }
        
    }
    
    public void playMove(String play){
        char position = play.charAt(1);
        System.out.println(position);
        Platform.runLater(new Runnable(){
            public void run(){
                switch(position){
                    case '1':
                        btn1.setText("x");
                        break;
                    case '2':
                        btn2.setText("x");
                        break;   
                    default:
                        break;
                }
            }
        });
    }
}

The Gui is run in class called ClientServerTic here is the class code.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ClientServerTic extends Application {

    @Override
    public void start(Stage stage){
        ClientGui root = new ClientGui();

        Scene scene = new Scene(root); 
        stage.setTitle("Tic Tac Toe");
        stage.setScene(scene);  
        stage.show();
    }

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

}

The server listens for messages and sends the message to all clients . Here is the server code.

//package clientservertic;

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

public class Server{

  ServerSocket serverSocket;

  public Server() throws Exception{//constructor

    serverSocket = new ServerSocket(5555);

    while(true){

      Socket s = serverSocket.accept();
      new GameHandler(s);
    }

  }

  public static void main(String[] args) throws Exception{
    System.out.println("Server is listening!!!");
    new Server();
  }

}

class GameHandler extends Thread{

  DataInputStream dis;
  PrintStream ps;
  Socket s;
  static Vector<GameHandler> clientsVector = new Vector<GameHandler>();//array of chat threads

  public GameHandler(Socket clientSocket) throws Exception{//constructor

    dis = new DataInputStream(clientSocket.getInputStream());

    ps = new PrintStream(clientSocket.getOutputStream());

    s = clientSocket;

    clientsVector.add(this);
    start();//start the thread

  }

  public void run(){
    while(true){
      String str = "";
      try{
        str = dis.readLine();//get any new messages  

        if(str.trim().equals("STOP")){

          ps.close();
          dis.close();
          s.close();

        }else{

          sendMessageToAll(str);//send the message to all the clients

        }
      }catch(Exception ex){}

    }
  }

  public void sendMessageToAll(String str){
    //loop on all chat threads and send the message

    for(GameHandler ch : clientsVector){
      ch.ps.println(str);
    }
  }

}

Upvotes: 1

Views: 138

Answers (1)

islam hany
islam hany

Reputation: 148

In this block of code you are closing the connection in the second try catch statement after the first loop so your loop becomes short circuited

while(true){
                
                try{
                    if(message.equals("stop"))
                        break;
                    recievedMessage = din.readLine();
                    playMove(recievedMessage);
                }catch(Exception ex){}
             
                try{
                    din.close();
                    dout.close();
                    s.close();
                }catch(Exception ex){}
            }
        }

What you should do is place the second try catch after the while loop and it will work just fine.

while(true){

                try{
                    if(message.equals("stop"))
                        break;
                    System.out.println("before");
                    recievedMessage = din.readLine();
                    playMove(recievedMessage);
                }catch(Exception ex){}
}
            
try{
    din.close();
    dout.close();
    s.close();
}catch(Exception ex){}

Upvotes: 1

Related Questions