0

So I'm making a Client-Server program in java that is focused on changing data on the server. Here is the code for the client side:

package com.company;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args){
        try {
            DatagramSocket socket=new DatagramSocket(4001);
            while(true){
                Scanner jin=new Scanner(System.in);
                String text=jin.nextLine();
                byte[] word=new byte[256];
                int command=Integer.parseInt(text);
                word=text.getBytes();
                DatagramPacket packet=new DatagramPacket(word, word.length, InetAddress.getByName("localhost"), 4000);
                socket.send(packet);

                if (command==0){
                    word=new byte[256];
                    packet=new DatagramPacket(word, word.length);
                    socket.receive(packet);
                    word=packet.getData();
                    System.out.println(new String(word));
                }

                else if (command==1){
                    word=new byte[256];
                    packet=new DatagramPacket(word, word.length);
                    socket.receive(packet);
                    System.out.println(new String(word));

                    int broj=jin.nextInt();
                    word=Integer.toString(broj).getBytes();
                    packet=new DatagramPacket(word, word.length, InetAddress.getByName("localhost"), 4000);
                    socket.send(packet);

                    word=new byte[256];
                    packet=new DatagramPacket(word, word.length);

                    socket.receive(packet);
                    System.out.println(new String(word));

                    new ClientHandler(socket).start();
                }

                else if(command==2){
                    word=new byte[256];
                    packet=new DatagramPacket(word, word.length);
                    socket.receive(packet);
                    System.out.println(new String(word));

                    jin=new Scanner(System.in);
                    int broj=Integer.parseInt(jin.next());

                    System.out.println("Test");
                    byte[] word1=Integer.toString(broj).getBytes();
                    System.out.println(new String(word1));
                    packet=new DatagramPacket(word1, word1.length, InetAddress.getByName("localhost"), 4000);
                    socket.send(packet);
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The problem lies in the if statement where:

else if (command==2)

After I enter a valid number the program doesn't move forward. After some debugging I found out that the problem lies in the Scanner or:

int broj=Integer.parseInt(jin.next());

The scanner doesn't register the inputted value and doesn't print "test" and the program gets stuck. Anyone know a solution?

David Mason
  • 121
  • 7
  • 1
    Probably related (can't verify now): [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045) – Pshemo Nov 26 '19 at 22:41
  • 1
    Don't create multiple `Scanner` objects on `System.in`. Move the `Scanner jin=new Scanner(System.in);` line up before the `try` statement, then delete the `jin=new Scanner(System.in);` line down in the `else if(command==2){` block. Then follow the advice in the link provided by @Pshemo. – Andreas Nov 26 '19 at 22:48
  • your code worked for me. I think the problem is with the client it keeps waiting for a response `socket.receive(packet)`, this blocks the thread until it gets a response. think to move it to another thread, or make sure you app gets the response, I used Netcat to simulate the client using the following cmd `nc -u localhost 4001`. https://imgur.com/a/LWb1RVu – Mohamed Nov 26 '19 at 22:56

1 Answers1

0
  1. You're making Scanner more than once; this is no good. Have one scanner (create it outside the while loop).

  2. Don't mix nextLine and any other next method. Either use only nextLine, or never use nextLine. If you want to grab 'the next string, until a newline' (versus 'the next string until a space, which is what next() normally does), set the delimiter: scanner.useDelimiter("\r?\n") immediately after making the scanner means that next() will grab entire lines now.

NB: You'll commonly see advice about throwing in an extra nextLine after a non-next call. This is extremely common, but bad advice. Fix the delimiter instead, as above.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37