Java InputDataStream没有显示任何内容



你好StackOverflow社区!现在我正在开发2D多人竞速游戏。我遇到的问题是,客户端将汽车详细信息发送到服务器,服务器成功读取并试图将其发送回,但客户端无法读取。此外,客户端事件似乎没有用DataInputStream代替run((。有人能给我一点建议吗?

客户代码:

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");
}
}
}

和服务器代码:

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();
}

}

rfsRunnable = new ReadFromServer(in);

线程不是魔法。这没有任何作用。

您正在寻找的是:

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

start()方法启动一个线程。该线程一旦启动,就会在提供的Runnable的run((方法中开始执行。

相关内容

  • 没有找到相关文章

最新更新