-1

I'm programming a server with Java with multiple Threads, buy very early I encountered a very odd glitch.

Here's my code :

package net.twh;

import java.net.DatagramSocket;
import java.util.Scanner;

public class Server {

    public static EntryThread entry;
    public static ServerPhases phase;
    public static DatagramSocket serverSocket;

    public static boolean stopProgram;

    public static Scanner keyboard;

    public static void main(String[] args) {
        Configuration.port = 17550;

        keyboard = new Scanner(System.in);

        entry = new EntryThread();

        entry.run();

        while (!stopProgram)
        {
            String line = "";
            System.out.printf("> ");
            line = keyboard.next();

            System.out.printf(line);

            if (line == "close")
            {
                entry.stop = true;
                stopProgram = true;
            }
        }
    } 

}

And here's my thread :

package net.twh;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class EntryThread extends Thread {

    public boolean stop;

    public EntryThread() {
        stop = false;
    }

    public void run() { 
        Runtime.getRuntime().addShutdownHook(new Thread(){public void run(){ // AUTOCLOSE NE PAS SUPPRIMER !!!!!
            try {
                Server.serverSocket.close();
                System.out.println("The server is shut down!");
            } catch (Exception e) { /* failed */ }
        }});

        try {
            Server.serverSocket = new DatagramSocket(Configuration.port);
            byte[] receiveData = new byte[8];

            System.out.printf("Listening on udp:%s:%d%n",
            InetAddress.getLocalHost().getHostAddress(), Configuration.port);     
            DatagramPacket receivePacket = new DatagramPacket(receiveData,
                               receiveData.length);
            while(!stop) {
                Server.serverSocket.receive(receivePacket);
                byte[] data = receivePacket.getData();
                System.out.println("RECEIVED: " + new String(data));
                InetAddress IPAddress = receivePacket.getAddress();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

The message when recieving a packet works and the Scanner works, but the "> " and string i entered doesn't show up.

PearlSek
  • 90
  • 9

3 Answers3

3

You are not starting the thread, you are only executing the run() method of the thread sequentially. The right way to start a thread is calling entry.start() instead of entry.run().

See also:

https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

JMSilla
  • 1,249
  • 9
  • 17
0

The stream of System.out is a PrintStream which flushes its output only on newlines (see this answer). But you can explicit call flush() or print a newline (which would even look better in your example):

    while (!stopProgram)
    {
        String line = "";
        System.out.print("> ");
        System.out.flush();
        line = keyboard.next();

        System.out.println(line);

        if ("close".equals(line))
        {
            entry.stop = true;
            stopProgram = true;
        }
    }

As I don't understand why you used printf() without any formatting I replaced it by the appropriate methods.

BTW: JMSilla is right, your code does nothing concurrently but that is not the question here.

Community
  • 1
  • 1
Arne Burmeister
  • 18,467
  • 8
  • 50
  • 89
0

Now you called run method instead of start(),run() method in Thread is that start creates new thread while run doesn't create any thread and simply execute in current thread like a normal method call.

entry = new EntryThread();
entry.run();

Use

entry.start()
Midhun Pottammal
  • 868
  • 1
  • 8
  • 23