Reputation: 19
I am recently making a little 2D Platformer and tried to implement some Online-2-Player features. There's a Server Program and of course the client. It works like this: your client can choose between ID 1 and ID 2 and the ID is directly sent to the server, so the server can decide what data to receive and what data to send you back (Client 1 wants the coordinates of Client 2 for ex.).
The problem now is that the sent data somehow gets manipulated and there are wrong values. In addition, if I sent a true boolean my client stops right in the line where I sent the boolean and doesn't continue running the code!
I rebuilt the "connect" method 1 by 1 in a new Client Program and there is just the same issue than with my gameclient!
Thanks for your advice! After long googling it seems none got the same problem :-(
Code from (rebuilt) client:
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();
}
}
}
Server Code (Just the thread handling all inputs and outputs):
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();
}
}
}
So as you were able to see I did some Console Outputs in my Client-Code. So if I run the program as it is this is the console output:
Connecting...
X: 5 / Y: 10 / Attacked: false /
RECEIVED:x: 0 / Y: 33554432 / rAttacked: false /
rAttacked: false /
So you can see every value received is somehow wrong, except from the boolean. Now if I go into my client code and set the "attacked" boolean to true, the output just runs like this:
Connecting...
X: 5 / Y: 10 / Attacked: true /
So I assume the code just stops and doesn't go on with the code (that happened with my client which even stopped ticking then)
Sorry for the long post, and I hope my code is enough for you guys to check it! Thank you very much!
Upvotes: 1
Views: 355
Reputation: 191
You forgot your break
in your client cases
and in case 3
and case 4
in your server. So for example here, when your client sends the third byte, the server reads it, but also passes through your case 4
, which handles the boolean.
This might not be the source of the weird number values, but I'm pretty sure this is the cause of the boolean bug.
Try adding all your break
statements, so that it won't do some unintended crazy stuff with byte manipulation at execution.
Upvotes: 1