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.