【发布时间】:2017-11-07 02:15:31
【问题描述】:
我最近正在制作一个小型 2D 平台游戏,并尝试实现一些 Online-2-Player 功能。有一个服务器程序,当然还有客户端。它的工作原理是这样的:您的客户端可以在 ID 1 和 ID 2 之间进行选择,并将 ID 直接发送到服务器,因此服务器可以决定接收哪些数据以及将哪些数据发送回您(客户端 1 想要客户端 2 的坐标例如)。
现在的问题是发送的数据以某种方式被操纵并且存在错误的值。此外,如果我发送了一个真正的布尔值,我的客户端会停在我发送布尔值的那一行,并且不会继续运行代码!
我在新的客户端程序中一一重建“连接”方法,但问题与我的游戏客户端相同!
感谢您的建议!经过长时间的谷歌搜索,似乎没有人遇到同样的问题:-(
来自(重建的)客户端的代码:
import java.io.*;
import java.net.Socket;
public class main {
public int x=5,y=10,id=2;
public int rX=0,rY=0;
public boolean receivedAttacked = false;
public boolean attacked=false;
public static void main(String[] args) {
System.out.println("Connecting...");
new main();
}
public main() {
try {
Socket socket = new Socket("localhost",6789);
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
DataInputStream din = new DataInputStream(socket.getInputStream());
dout.writeByte(1);
dout.writeInt(id);
dout.flush();
dout.writeByte(2);
dout.writeInt(x);
System.out.print("X: " + x + " / ");
dout.flush();
dout.writeByte(3);
dout.writeInt(y);
System.out.print("Y: " + y + " / ");
dout.flush();
dout.writeByte(4);
System.out.print("Attacked: " + attacked + " / " + "\n");
dout.writeBoolean(attacked);
dout.flush();
int byteIndex = 3;
byte byteRead;
for(int i = 0; i < byteIndex; i++) {
byteRead = din.readByte();
switch(byteRead) {
case 1:
rX = din.readInt();
System.out.print("RECEIVED:" + "x: " + rX + " / ");
case 2:
rY = din.readInt();
System.out.print("Y: " + rY + " / ");
case 3:
receivedAttacked = din.readBoolean();
System.out.print("rAttacked: " + receivedAttacked + " / " + "\n");
default:
}
}
System.out.println("PAUSE");
dout.close();
din.close();
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
服务器代码(只是处理所有输入和输出的线程):
package me.server.lel;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.JFrame;
public class ServerThread extends Canvas implements Runnable {
Socket incoming; //the client socket so to say
public int id;
public int x1 = 0,y1 = 0,x2 = 0,y2 = 0;
public boolean attacked1 = false, attacked2 = false;
public ServerThread(Socket incoming, int id) {
this.incoming = incoming;
this.id = id;
}
@Override
public void run() {
try {
//Reading the input and saving it in global variables
DataInputStream in = new DataInputStream(incoming.getInputStream());
byte index;
int byteAmount = 4;
for(int i = 0; i < byteAmount; i++ ){
index = in.readByte();
switch(index) {
case 1:
id = in.readInt();
break;
case 2:
if(id == 1) {
x1 = in.readInt();
} else if(id == 2) {
x2 = in.readInt(); ;
}
break;
case 3:
if(id == 1) {
y1 = in.readInt();
} else if(id == 2) {
y2 = in.readInt();
}
case 4:
if(id == 1) {
attacked1 = in.readBoolean();
} else if(id == 2) {
attacked2 = in.readBoolean();
}
default:
}
}
//Writing back the values of the opposite player
DataOutputStream out = new DataOutputStream(incoming.getOutputStream());
if(id == 1) {
out.writeByte(1);
out.writeInt(x2);
out.flush();
out.writeByte(2);
out.writeInt(y2);
out.flush();
out.writeByte(3);
out.writeBoolean(attacked2);
out.flush();
} else if(id == 2) {
out.writeByte(1);
out.writeInt(x1);
out.flush();
out.writeByte(2);
out.writeInt(y1);
out.flush();
out.writeByte(3);
out.writeBoolean(attacked1);
out.flush();
}
} catch(SocketException e) {
System.out.println("User Disconnected: " + incoming.getLocalAddress());
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
如您所见,我在客户端代码中做了一些控制台输出。因此,如果我按原样运行程序,这就是控制台输出:
Connecting...
X: 5 / Y: 10 / Attacked: false /
RECEIVED:x: 0 / Y: 33554432 / rAttacked: false /
rAttacked: false /
因此,您可以看到收到的每个值都有某种错误,除了布尔值。现在,如果我进入我的客户端代码并将“attacked”布尔值设置为 true,输出就会像这样运行:
Connecting...
X: 5 / Y: 10 / Attacked: true /
所以我假设代码只是停止并且没有继续执行代码(发生在我的客户端甚至停止滴答作响)
抱歉,帖子太长了,希望我的代码足以让你们检查一下!非常感谢!
【问题讨论】:
-
这里没有“崩溃”的证据。
标签: java sockets networking server