1

I am working on Battleship swing app that communicates through sockets.

private ServerSocket server;
private Socket connection;
private PrintWriter out;
private Scanner in;

I make a connection and setup output and input streams

out = new PrintWriter(connection.getOutputStream(), true);
in = new Scanner(connection.getInputStream());

Then user clicks on field that he thinks there is a ship, and coordinates get sent

public void sendXY(String cord)
{
    out.print(cord);
}

Then the method gets called that waits for opponents app to respond if there is a ship or not (true|false).

public void readHit()
{
    boolean k = true;
    while(k)
    {
        if(in.hasNext()) //<--app hangs at this line
        {
            Fields.hit = in.nextBoolean();
            k = false;  
        }
    }
}

But when I test this, my app hangs at first call to in.hasNext().

Svajs
  • 29
  • 1
  • 9
  • 1
    Have you read the javadoc? – Sotirios Delimanolis Dec 12 '13 at 14:54
  • If you mean the line that says "This method may block while waiting for input to scan.", then that doesn't apply to my example because it gets next element later, but not necessarily before hasNext is first called – Svajs Dec 12 '13 at 14:57
  • The print method does probably not send a complete token. My first guess would to use println. Blocking on hasNext is the preferred behaviour, the alternative would be to burn 100% CPU with while(true). – Dorus Dec 12 '13 at 14:59
  • I tried using println instead, but problem persists. EDIT: I changed the sending of cord to println as well as sending of hit to println and it works now. Thank you. – Svajs Dec 12 '13 at 15:02

4 Answers4

1

You could first test if there are bytes available in the connection inputstream:

InputStream instream = connection.getInputStream();
in = new Scanner(instream);

Then replace

if(in.hasNext())

with

if (instream.available() > 0)
geert3
  • 7,086
  • 1
  • 33
  • 49
1

From JavaDoc https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext()

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

Looking inside sources, we can find this:

public boolean hasNext() {
    ensureOpen();
    saveState();
    while (!sourceClosed) {
        if (hasTokenInBuffer())
            return revertState(true);
        readInput();
    }
    boolean result = hasTokenInBuffer();
    return revertState(result);
}

It looks that method blocks if Source Stream is open and there are no bytes available. Try to find another alternatives to solve this problem. You could try to create new thread for every client, which will call hasNext()

Maxian Nicu
  • 2,166
  • 3
  • 17
  • 31
0

From the javadoc:

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0

The scanner is waiting for a token, the print method does not send a complete token unless String cord contains some kind of terminator. Try to use println instead.

Dorus
  • 7,276
  • 1
  • 30
  • 36