Note that there are some explanatory texts on larger screens.

plurals
  1. POThreads,Sockets and Streams
    text
    copied!<p>In my program I want to send some information to another computer with sockets. The first socket send some text throw the server and when the second socket receive this information it send a answer to the first socket. The problem is that another thread receive the answer and the only thing which I get is deadlock. This is the server side:</p> <pre><code>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); } } </code></pre> <p>This is the client side:</p> <pre><code>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+"&amp;value=true"); GameWindow.getInstance().getDisplay().asyncExec(new Runnable() { @Override public void run() { GameWindow.getInstance().startNewGame(); } }); } else sendRequest("send_message_to_user?user="+usernameOther+"&amp;value=false"); } catch (IOException exc){ } } </code></pre> <hr> <p>Here is the sendRequest method:</p> <pre><code>private void sendResult(Socket socket,String request) throws IOException{ DataOutputStream writer = new DataOutputStream(socket.getOutputStream()); writer.writeUTF(request); writer.flush(); } </code></pre> <p>The client socket is created in the class Server</p> <pre><code>while (true) { try { Socket socket = serverSocket.accept(); new GameThread(socket,databaseManager); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>and it is put in hashmap when the user pass the login level:</p> <pre><code>if(msgArray[0].compareTo("user_info") == 0){ Integer flagUser = -1; String[] userInfo = {msgArray[1].substring(msgArray[1].indexOf("=") + 1, msgArray[1].indexOf("&amp;")),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()+""); } </code></pre> <p>I thing I know what is the reason of the deadlock but I can't solve it. When the first user send the information to the other user he wait for response. Of course the other user send a response but this response is handle from other thread. So the deadlock is from a read method which block the thread. How can I send information from one socket to another without deadlock?</p> <pre><code>public GameThread(Socket socket, DatabaseManager databaseManager) { this.socket = socket; this.databaseManager = databaseManager; parser = new RequestParser(); authorizationControl = new AuthorizationControl(databaseManager); communication = new SocketCommunication(socket); start(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload