线程,套接字和流

时间:2010-02-28 14:54:49

标签: java network-programming

在我的程序中,我想将一些信息发送到另一台带有套接字的计算机。第一个套接字发送一些文本抛出服务器,当第二个套接字收到此信息时,它会向第一个套接字发送一个答案。问题是另一个线程收到答案,我得到的唯一的东西是死锁。 这是服务器端:

else if(msgArray[0].compareTo("game_request") == 0){
                if(userMap.containsKey(msgArray[1])){
                    Socket socketPlayerTwo = userMap.get(msgArray[1]);
                    otherPlayer = msgArray[1];
                    sendResult(socketPlayerTwo, "game_offer?"+username);
                    Boolean willPlay =Boolean.valueOf(recieveRequest(socketPlayerTwo).split("?")[1]);
                    if(willPlay)
                        playingUser.put(username,msgArray[1]);
                    sendResult(socket, "game_accept?"+willPlay);

                }
            }

这是客户方:

private void showGameRequest(String usernameOther) {
        try{
            int status = GameWindow.getInstance().showNotification("Game Offer","The user " + usernameOther + " offers you a game!\nDo you agree?",SWT.ICON_QUESTION|SWT.YES|SWT.NO);
            if (status == SWT.YES){
                otherPlayer = usernameOther;
                sendRequest("send_message_to_user?user="+usernameOther+"&value=true");
                GameWindow.getInstance().getDisplay().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        GameWindow.getInstance().startNewGame();

                    }
                });
            }
            else
                sendRequest("send_message_to_user?user="+usernameOther+"&value=false");
            }
        catch (IOException exc){

        }
    }

这是sendRequest方法:

private void sendResult(Socket socket,String request) throws IOException{
    DataOutputStream writer = new DataOutputStream(socket.getOutputStream());
    writer.writeUTF(request);
    writer.flush();
}

客户端套接字在类Server

中创建
while (true) {
        try {
            Socket socket = serverSocket.accept();
            new GameThread(socket,databaseManager);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

当用户通过登录级别时,它将被放入hashmap中:

if(msgArray[0].compareTo("user_info") == 0){
                Integer flagUser = -1;
                String[] userInfo = {msgArray[1].substring(msgArray[1].indexOf("=") + 1, msgArray[1].indexOf("&")),msgArray[1].substring(msgArray[1].lastIndexOf("=")+ 1, msgArray[1].indexOf(";"))};
                Boolean status = databaseManager.checkUser(userInfo[0], userInfo[1]);
                if(status){
                    if(!userMap.containsKey(userInfo[0])){
                        userMap.put(userInfo[0], socket);
                        username = userInfo[0];
                        flagUser = 0;
                    }
                    else
                        flagUser = 1;
                }
                sendResult(socket, "user_info_status?"+flagUser.toString()+"");
            }

我知道我死的原因是什么,但我无法解决。当第一个用户将信息发送给其他用户时,他等待响应。当然其他用户发送响应但该响应是来自其他线程的句柄。所以死锁来自阻塞线程的read方法。如何在没有死锁的情况下将信息从一个套接字发送到另一个套接字?

public GameThread(Socket socket, DatabaseManager databaseManager) {
    this.socket = socket;
    this.databaseManager = databaseManager;
    parser = new RequestParser(); 
    authorizationControl = new AuthorizationControl(databaseManager);
    communication = new SocketCommunication(socket);
    start();
}

1 个答案:

答案 0 :(得分:0)

你能告诉我们更多你的代码吗?

sendResult(socketPlayerTwo, "game_offer?"+username);
recieveRequest(socketPlayerTwo);
sendRequest("send_message_to_user?user="+usernameOther+"&value=true");

首发。 从小小的角度来看,我发现你对使用它们的顺序有问题。据我所知,你有一个inputSteram.readObject()方法阻塞了某个地方等待来自另一方的消息。