I am creating a custom 2d turn based game engine in Java, and so far everything works great. Currently, I have come to the dreaded networking :), and I made a test code, prototype of the network that I would like to create. It uses Java's MulticaseSocket class, DatagramPackets and UDP (obviously). I chose to go with this because it is turn based, so there is no need for direct connection to a server, instead it is more like everyone talking to everyone, with ONE person broadcasting their movements, attacks and such, and everyone else listening. My problem here is, that my serializable class, VarnaPacket, is sent but not recieved on the other end. If I replace the sending code to sen a string, it recieves it, but it does not recieve my serializable object. Can someone PLEASE help!?
VarnaNetwork.java
package Network;
import java.io.*;
import java.net.*;
import Statics.EngineOptions;
public class VarnaNetwork extends Thread{
boolean gameRunning = true;
boolean myTurn = false;
MulticastSocket socket;
InetAddress address;
public VarnaNetwork() throws IOException {
socket = new MulticastSocket(EngineOptions.PORT_NUM);
address = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
socket.joinGroup(address);
}
public void run()
{
try
{
socket = new MulticastSocket(EngineOptions.PORT_NUM);
address = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
socket.joinGroup(address);
if(VarnaNetworkDataStructure.PLAYER_ID == 1)
{
myTurn = true;
}
else
{
recv();
}
}
catch (Exception e)
{
}
}
public void send(int player)
{
try
{
if(myTurn)
{
if(player != VarnaNetworkDataStructure.PLAYER_ID)
{
System.out.println("my turn is over!");
myTurn = false;
}
/*ByteArrayOutputStream b_out = new ByteArrayOutputStream();
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject("" + new VarnaPacket(player));*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject("" + new VarnaPacket(player));
byte[] b= baos.toByteArray();
//byte[] b = b_out.toByteArray();
//byte[] buf = new byte[256];
/*String dString = System.console().readLine("Input: ");
buf = dString.getBytes();
if(VarnaNetworkDataStructure.PLAYER_ID != VarnaNetworkDataStructure.TOTAL_PLAYERS)
{
if(dString.compareTo("start " + (VarnaNetworkDataStructure.PLAYER_ID + 1)) == 0)
{
System.out.println("My turn is over!");
myTurn = false;
}
}
else
{
if(dString.compareTo("start " + 1) == 0)
{
System.out.println("My turn is over!");
myTurn = false;
}
}*/
//System.out.println("Sending data..!");
InetAddress group = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
DatagramPacket packet = new DatagramPacket(b, b.length, group, EngineOptions.PORT_NUM);
socket.send(packet);
}
}
catch(Exception e)
{
}
}
public void recv()
{
while(!myTurn)
{
try
{
socket = new MulticastSocket(EngineOptions.PORT_NUM);
address = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
socket.joinGroup(address);
DatagramPacket packet;
byte[] buf = new byte[65535];
packet = new DatagramPacket(buf, buf.length);
System.out.println("Trying to recieve");
socket.receive(packet);
System.out.println("Gotchya");
ByteArrayInputStream b_in = new ByteArrayInputStream(packet.getData());
ObjectInputStream o_in = new ObjectInputStream(b_in);
VarnaPacket p = (VarnaPacket) o_in.readObject();
System.out.println("player turn is:" + p.getTurn());
/*if(p.getTurn() == VarnaNetworkDataStructure.PLAYER_ID)
{
myTurn = true;
System.out.println("HOORAY its my turn!");
}
else
{
System.out.println(p.getTurn());
}*/
/*String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
if(received.compareTo("start " + VarnaNetworkDataStructure.PLAYER_ID) == 0)
{
System.out.println("My turn is starting!");
myTurn = true;
}*/
}
catch (Exception e)
{
}
}
}
/*public void run() {
while(gameRunning){
try{
if(!myTurn)
{
socket = new MulticastSocket(EngineOptions.PORT_NUM);
address = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
socket.joinGroup(address);
DatagramPacket packet;
byte[] buf = new byte[65535];
ByteArrayInputStream b_in = new ByteArrayInputStream(buf);
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
ObjectInputStream o_in = new ObjectInputStream(b_in);
VarnaPacket o = (VarnaPacket) o_in.readObject();
b_in.reset(); // reset so next read is from start of byte[] again
if(vpacket.PLAYER_TURN == VarnaNetworkDataStructure.PLAYER_ID)
{
myTurn = true;
System.out.println("HOORAY its my turn!");
}
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
if(received.compareTo("start " + VarnaNetworkDataStructure.PLAYER_ID) == 0)
{
System.out.println("My turn is starting!");
myTurn = true;
}
}
else
{
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject(vpacket);
byte[] b = b_out.toByteArray();
//byte[] buf = new byte[256];
String dString = System.console().readLine("Input: ");
buf = dString.getBytes();
if(VarnaNetworkDataStructure.PLAYER_ID != VarnaNetworkDataStructure.TOTAL_PLAYERS)
{
if(dString.compareTo("start " + (VarnaNetworkDataStructure.PLAYER_ID + 1)) == 0)
{
System.out.println("My turn is over!");
myTurn = false;
}
}
else
{
if(dString.compareTo("start " + 1) == 0)
{
System.out.println("My turn is over!");
myTurn = false;
}
}
//System.out.println("Sending data..!");
if(vpacket.PLAYER_TURN != VarnaNetworkDataStructure.PLAYER_ID)
{
myTurn = false;
}
InetAddress group = InetAddress.getByName(VarnaNetworkDataStructure.SERVER_ADDRESS);
DatagramPacket packet = new DatagramPacket(b, b.length, group, EngineOptions.PORT_NUM);
socket.send(packet);
}
}catch (IOException e) {
e.printStackTrace();
gameRunning = false;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
try {
socket.leaveGroup(address);
} catch (IOException e) {
e.printStackTrace();
}
socket.close();
}
public static byte[] getBytes(Object obj) throws java.io.IOException{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte [] data = bos.toByteArray();
return data;
}*/
}
VarnaPacket.java
package Network;
import java.io.Serializable;
import java.util.Vector;
import Unit.Unit;
import World.Building;
public class VarnaPacket implements Serializable{
private static final long serialVersionUID = 4970897833545316377L;
private Vector<Unit>[] units;
private Vector<Building> buildings;
private int PLAYER_TURN;
public VarnaPacket(int turn)
{
PLAYER_TURN = turn;
units = VarnaNetworkDataStructure.units;
buildings = VarnaNetworkDataStructure.buildings;
}
public int getTurn()
{
return PLAYER_TURN;
}
}
If anyone can help me make this network and fix this dilemna of sending a serializble object, I would gladly appreciate. Oh, also please do not suggest an RMI, It is easier, but I really want efficiency rather than easiness (is that even a word? Oh well you know what I mean :D) Thank You!