简体   繁体   中英

Program freezes when trying to send a string with a socket

I'm coding a basic Naval Battle game to play with a friend across a LAN network. I'll quickly describe how it works before passing to the problem:

  1. We decided to use a 10x10 grid and send fleet information with a string.
  2. We use unified client/server functions: at launch the program controls if there's already a server listening on the IP specified - if so the program acts like a client, otherwise like a server.
  3. After that a new frame is opened on each pc. This frame contains a 100 JButton array. There each player disposes his fleet and, when finished, confirms his disposition.

When a player confirms his ships location, the program is supposed to send to the other a string, created by appending "t" (if a part of a ship is on the sector) of "f" (if no ships are on the sector). At the time of confirmation, the string contains 100 chars.

However, when we hit confirm on each pc, both programs freeze and don't communicate at all.

Sending the string with frmSend

private position.frmDeck deck;
public static String myFleet;
private gioco.frmInterface interface;
private setup.frmStart start;

public frmSend(){
    initComponents();
}

public frmSend(posiziona.frmDeck f, setup.frmStart a, String s) {
    initComponents();
    deck=f;
    start=a;
    myFleet=s;
    f.setEnabled(false);
}

private void Continua(java.awt.event.ActionEvent evt) {                          
    try{
        start.sendReceive(stringaCampo);
        start.setDatoPassato(null);
        while(start.getDatoPassato().equals(null))
        {
            start.sendReceive(null);
        }
    }
    catch(Exception e){System.out.println(e.getMessage());}
    interface=new game.frmInterface(start);
    interface.setVisible(true);
    deck.dispose();
    dispose();
}

sendReceive method

public Socket sktC,sktS; //S stands for server, C for client
public ServerSocket s;
public InputStream iC,iS;
public OutputStream oC,oS;
public BufferedReader rC,rS;
public BufferedWriter wC,wS;
public boolean isClient=false;
private String sentString;
public void sendReceive (String s){
    if(s==null){
        //null parameter, i receive data
        if(isClient){
            try {
                sentString=rC.readLine();
            } catch (IOException ex) {}
        }
        else{
            try {
                sentString=rS.readLine();
            } catch (IOException ex) {}
        }
    }
    else{
        //i send the string
        if(isClient){
            try {
                wC.write(s);
            } catch (IOException ex) {}
        }
        else{
            try {
                wS.write(s);
            } catch (IOException ex) {}
        }
    }
}

I hope my description is sufficiently clear.

i think the problem is caused because the streams are never closed in the receiving side. Take a look at some goods examples http://www.ase.md/~aursu/ClientServerThreads.html

Are you writing a newline character over the socket every time? You are using readLine, which is a blocking operation that continues to read until it receives the newline character. Since it appears your code is synchronous it will hang until the read is complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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