DataOutputStream- Always writing to server?
For the purposes of a simple 2D game, I need my client program to always
be sending coordinates to a server.
So, I created a test to see if I could make both players have the same
velocity by sending and retrieving values from the server.
package main;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataSender implements Runnable{
private DataInputStream fromServer;
private DataOutputStream toServer;
Player player;
Player opponent;
public DataSender(DataInputStream fromServer, DataOutputStream
toServer, Player player, Player opponent){
this.fromServer = fromServer;
this.toServer = toServer;
this.player = player;
this.opponent = opponent;
}
@Override
public void run() {
while(true){
try {
toServer.writeInt(player.velX);
} catch (IOException e) {
e.printStackTrace();
}
try {
opponent.velX = fromServer.readInt();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The while(true) loop only executes one time upon thread creation. How can
I establish a constant stream of data?
No comments:
Post a Comment