简体   繁体   English

在同一套接字上接收多个消息

[英]Receive multiple messages on same socket

I've been studying Java for 4 months and decided to make a simple Texas Holdem game with javaFX. 我学习Java已有4个月了,决定用javaFX制作一个简单的Texas Holdem游戏。 The client connects to the Server and the server creates a new thread for each client (max 10 clients). 客户端连接到服务器,服务器为每个客户端(最多10个客户端)创建一个新线程。

My problem is to handle multiple messages at the same time. 我的问题是同时处理多个消息。 If you look at my code below you will understand: (This is the client) 如果您查看下面的我的代码,您将会了解:(这是客户端)

while (true){
            messageIn = in.readUTF();

            if (messageIn.equals("seat")){
                String name = in.readUTF();
                String seat = in.readUTF();
                gameScreenController.addPlayerToSeat(name, seat);
            }
            else if (messageIn.equals("hand")){
                String seat = in.readUTF();
                int handCard1 = in.readInt();
                int handCard2 = in.readInt();
                gameScreenController.handCardToSeat(handCard1, handCard2, seat);
            }
            else if (messageIn.equals("flopp")){
                int floppCard1 = in.readInt();
                int floppCard2 = in.readInt();
                int floppCard3 = in.readInt();
                gameScreenController.flopp(floppCard1, floppCard2, floppCard3);
            }
            else if (messageIn.equals("turn")){
                int turnCard = in.readInt();
                gameScreenController.turn(turnCard);
            }
            else if (messageIn.equals("river")){
                int riverCard = in.readInt();
                gameScreenController.turn(riverCard);
            }   
        }
  1. the client connects 客户端连接
  2. It will listen for messages 它将监听消息

As you can see, it is possible for the messages to collide if you recieve a playername and playerseat at the same moment as you receive the flopp cards. 如您所见,如果您在收到flopp卡的同时收到玩家名称和玩家座位,则消息可能会发生冲突。

My question is how I can avoid this? 我的问题是如何避免这种情况? What do I need or what could I search for to solve this. 我需要什么或可以寻找解决方案。 I will be grateful for all hints or answers I could get! 我将不胜感激所有提示或答案! :) :)

On the server, a player is represented as a thread, reading and writing the data socket obtained by the accept. 在服务器上,播放器被表示为线程,用于读取和写入由accept获取的数据套接字。 The "Table" (or client control sw) is responsible for directing game events to individual or all players, passing messages to be sent to the pertaining thread(s). “表”(或客户端控件sw)负责将游戏事件定向到单个或所有玩家,并将要发送的消息传递到相关线程。

Some events should be held back until the current deal (or round) reaches its end, eg, a new player sitting down at the table. 应推迟一些事件,直到当前交易(或回合)结束为止,例如,有新玩家坐在桌旁。 This can be implemented by setting up a queue to each player/thread, to be read whenever the game reaches the inter-round state. 这可以通过为每个玩家/线程设置一个队列来实现,每当游戏达到回合状态时都将读取该队列。

During a deal, a state machine can control what is acceptable from a player - although the players should be programmed according to the protocol: eg, when asked what to do, must respond with whatever action the game permits. 在交易期间,状态机可以控制玩家可接受的内容-尽管应该根据协议对玩家进行编程:例如,当被问到做什么时,必须以游戏允许的任何动作做出响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM