Hi,
so I've got this multithreaded server which correctly waits for clients and assigns them to a worker thread. From there, the client is sent an object Packet
which contains variables Player
and Round
(simplified, it contains more data but these are the most important).
I'm building a simple top trumps game and I need two enable two players to play it via LAN.
My idea is to send a Packet
object back and forth. The apps would populate the GUI based on the packet's contents.
Threads decide weather to send or receive based on Round
:
This is being run in a threads on both the server and the client (+ some house-keeping code):
private void whileChatting() throws IOException { // Allows communication.
do {
if (this.packet.getRound() % 2 == 0) {
this.packet = receivePacket();
} else if (this.packet == null || this.packet.getRound() % 2 != 0) {
sendPacket();
}
displayCardImage(this.packet.getP2().getPlayerDeck().peek(), this.cardHand_L);
displayCardImage(this.packet.getOnTable(), this.cardTable_L);
displayDeckSizes(this.handDeckSize_L, this.packet.getP2().getPlayerDeck().size());
displayDeckSizes(this.opponentDeckSize_L, this.packet.getP1().getPlayerDeck().size());
displayRoundNumber(this.roundNumber_L, this.packet.getRound());
this.stop = true;
} while (!this.stop);
}
Flush is being called in each sendPacket();
.
The problem starts when I try to send the packet back! The client properly receives it, does something to it but then refuses to send it back (send and receive methods have System.out "I sent this" or "I got this").
When I click a button in the client, it unpacks the packet in a local variables which are used to generate a new packet and then the new packet is passeds into a new thread (which, in theory, should send it to the server since I flagged setSend(True)
):
this.c.setPacket(new Packet(this.round, this.p1, this.p2, this.tableCard));
this.c.setStop(false);
this.c.setSend(true);
Thread connectionThread = new Thread(this.c);
connectionThread.start();
But in reality the new connectionThread
never even reaches sendPacket();
.
I've been trying all sorts of crazy things for 3 hours now. What am I not understanding?