Search code examples
javasocketsclientdatainputstream

Java InputDataStream shows nothing


Hello StackOverflow Community! Right now I'm working on 2D multiplayer racing game. The issue I faced up with, is that client sends car details to the server, and server reads them successfully and attempts to send them back BUT client can't read it. Besides it seems that client event doesn't lacuna run() with DataInputStream. Can anybody give me a piece of advise of something?

Client Code:

private Socket socket;
private int playerID;

private ReadFromServer rfsRunnable;
private WriteToServer wtsRunnable;

private void connectToServer(){
    try{
        socket = new Socket("localhost",5000);
        DataInputStream in = new DataInputStream(socket.getInputStream());
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        playerID = in.readInt();
        System.out.println("You are Player#" + playerID);

        if (playerID==1){
            System.out.println("Waiting for Player #2 to connect");
        }

        rfsRunnable = new ReadFromServer(in);
        wtsRunnable = new WriteToServer(out);

        rfsRunnable.waitForStartMessage();
    }catch (IOException ex){
        System.out.println("IOException from connect to server");
    }
}



private class ReadFromServer implements Runnable{

    private DataInputStream dataIn;

    public ReadFromServer(DataInputStream in){
        dataIn = in;
        System.out.println("Read from server runnable");
    }

    public void run(){
        try{
            while (true){
                if (foreignKart != null){
                    foreignKart.locationX=dataIn.readInt();
                    foreignKart.locationY=dataIn.readInt();
                    foreignKart.speed = dataIn.readInt();
                    foreignKart.currentImage = dataIn.readInt();
                    foreignKart.ready = dataIn.readBoolean();

                    System.out.println("test");
                }
            }
        }catch (IOException ex){
            System.out.println("IOException from RFS run");
        }
    }

    public void waitForStartMessage(){
        try{
            String startMessage = dataIn.readUTF();
            System.out.println("Message form server" + startMessage);

            Thread readThread = new Thread(rfsRunnable);
            Thread writeThread = new Thread(wtsRunnable);

            readThread.start();
            writeThread.start();
        }catch (IOException ex){
            System.out.println("IOException form waiting message");
        }
    }

}

private class WriteToServer implements Runnable{

    private DataOutputStream dataOut;

    public WriteToServer(DataOutputStream out){
        dataOut = out;
        System.out.println("Write to server runnable");
    }

    public void run(){
        try{

            while (true){
                if (homeKart != null) {
                    dataOut.writeInt(homeKart.locationX);
                    dataOut.writeInt(homeKart.locationY);
                    dataOut.writeInt(homeKart.speed);
                    dataOut.writeInt(homeKart.currentImage);
                    dataOut.writeBoolean(homeKart.ready);
                    dataOut.flush();
                }

                try{
                    Thread.sleep(25);
                }catch(InterruptedException ex){
                    System.out.println("Interrupted exception from WTS run");
                }
            }

        } catch (IOException ex){
            System.out.println("IOException from WTS run");
        }
    }

}

and Server Code:

import java.io.*;
import java.net.*;

public class RaceServer {

private ServerSocket ss;
private int numberPlayers;
private int maxPlayers;

private Socket p1Socket;
private Socket p2Socket;

private ReadFromClient p1ReadRunnable;
private ReadFromClient p2ReadRunnable;

private WriteToClient p1WriteRunnable;
private WriteToClient p2WriteRunnable;

private int pl1X, pl1Y, pl1speed, pl1currentImage;
private int pl2X, pl2Y, pl2speed, pl2currentImage;

private boolean ready1, ready2;

public RaceServer() {

    System.out.println("------RACE GAME SERVER-----");
    numberPlayers = 0;
    maxPlayers = 2;

    pl1X = 425;
    pl1Y = 550;
    pl1speed = 0;
    pl1currentImage = 12;
    ready1 = false;

    pl2X = 425;
    pl2Y = 500;
    pl2speed = 0;
    pl2currentImage = 12;
    ready2 = false;


    try {
        ss = new ServerSocket(5000);
    } catch (IOException ex) {
        System.out.println("IOException from Race Server Constructor");
    }
}

private class ReadFromClient implements Runnable {

    private int playerID;
    private DataInputStream dataIn;

    public ReadFromClient(int pid, DataInputStream in){
        playerID = pid;
        dataIn = in;

        System.out.println("Read From Client" + playerID + " Runnable created");
    }

    public void run(){
        try{
            while(true){
                if (playerID == 1){
                    pl1X = dataIn.readInt();
                    pl1Y = dataIn.readInt();
                    pl1speed = dataIn.readInt();
                    pl1currentImage = dataIn.readInt();
                    ready1 = dataIn.readBoolean();
                } else {
                    pl2X = dataIn.readInt();
                    pl2Y = dataIn.readInt();
                    pl2speed = dataIn.readInt();
                    pl2currentImage = dataIn.readInt();
                    ready2 = dataIn.readBoolean();
                }

                System.out.println("Player 1,IN X: "+pl1X);
                System.out.println("Player 2,IN X: "+pl2X);
            }
        }catch (IOException ex){
            System.out.println("Exception from RFC run");
        }
    }
}

private class WriteToClient implements Runnable {

    private int playerID;
    private DataOutputStream dataOut;

    public WriteToClient(int pid, DataOutputStream out){
        playerID = pid;
        dataOut = out;

        System.out.println("Write to client " + playerID + " Runnable created");
    }

    public void run(){
        try{
            while(true){
                if (playerID == 1){
                    dataOut.writeInt(pl2X);
                    dataOut.writeInt(pl2Y);
                    dataOut.writeInt(pl2speed);
                    dataOut.writeInt(pl2currentImage);
                    dataOut.writeBoolean(ready2);
                } else {
                    dataOut.writeInt(pl1X);
                    dataOut.writeInt(pl1Y);
                    dataOut.writeInt(pl1speed);
                    dataOut.writeInt(pl1currentImage);
                    dataOut.writeBoolean(ready1);
                }
                dataOut.flush();
                try {
                    Thread.sleep(25);
                } catch (InterruptedException ex){
                    System.out.println("InterruptedException from WTC run");
                }


                System.out.println("Player 1, OUT X: "+pl1X);
                System.out.println("Player 2, OUT X: "+pl2X);
            }

        }catch (IOException ex){
            System.out.println("IOException from WTC run");
        }
    }

    public void sendStartMessage(){
        try{
            dataOut.writeUTF("We now have two players");
        }catch (IOException ex){
            System.out.println("IOException from sendStartMessage");
        }
    }

}

public void acceptConnections(){
    try{
        System.out.println("Waiting for connections");

        while (numberPlayers < maxPlayers){
            Socket s = ss.accept();
            DataInputStream in = new DataInputStream(s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());

            numberPlayers++;
            out.writeInt(numberPlayers);
            System.out.println("Player #" + numberPlayers + " has connected");

            ReadFromClient rfc = new ReadFromClient(numberPlayers, in);
            WriteToClient wtc = new WriteToClient(numberPlayers, out);

            if (numberPlayers == 1){
                 p1Socket = s;
                 p1ReadRunnable = rfc;
                 p1WriteRunnable = wtc;
            }  else {
                p2Socket = s;
                p2ReadRunnable = rfc;
                p2WriteRunnable = wtc;
                p1WriteRunnable.sendStartMessage();
                p2WriteRunnable.sendStartMessage();

                Thread readThread1 = new Thread(p1ReadRunnable);
                Thread readThread2 = new Thread(p2ReadRunnable);
                readThread1.start();
                readThread2.start();

                Thread writeThread1 = new Thread(p1WriteRunnable);
                Thread writeThread2 = new Thread(p2WriteRunnable);
                writeThread1.start();
                writeThread2.start();
            }
        }

        System.out.println("No longer accepting connections");

    }catch (IOException ex){
        System.out.println("IOException from Accept Connections");
    }
}

public static void main(String[] args){
    RaceServer rs = new RaceServer();
    rs.acceptConnections();
}

}


Solution

  • rfsRunnable = new ReadFromServer(in);

    Threads aren't magic. This doesn't do anything.

    What you're looking for is:

    Thread readThread = new Thread(new ReadFromServer(in));
    readThread.start();
    

    The start() method starts a thread. That thread, once it launches, starts execution at the run() method of the provided Runnable.