Reputation: 167
I have a server that will be running on another machine and I need to debug with two different machines. Is there a way to virtually debug the server since everything runs ok on my machine but when i put it on another machine everything wrong? I dont have another machine in my possession (I can only host and see results) .
public class fss { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; boolean allowed = true; // int port = Integer.parseInt(args[0]); int port
= 60000;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.out.println("Could not transmit on port: " + port);
System.exit(-1);
}
while (listening) {
//Take the ip of the client in number form
allowed = true;
Socket socket = serverSocket.accept();
DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
String clientAddress = socket.getRemoteSocketAddress().toString();
clientAddress = clientAddress.substring(1);
for (int i = 0; i < clientAddress.length() - 1; i++) {
if (clientAddress.substring(i, i + 1).equals(":")) {
clientAddress = clientAddress.substring(0, i);
}
}
File f = new File("forbidden.txt");
if (f.exists()) {
BufferedReader forbidden = new BufferedReader(new FileReader("forbidden.txt"));
String addr;
while ((addr = forbidden.readLine()) != null) {
if (Character.isLetter(addr.charAt(0))) {//if the address is in a letter form
addr = InetAddress.getByName(addr).toString();
for (int i = 0; i < addr.length() - 1; i++) {
//System.out.println(addr.substring(i, i + 1));
if (addr.substring(i, i + 1).equals("/")) {
addr = addr.substring(i + 1);
}
}
}
if (clientAddress.equals(addr)) {
allowed = false;
break;
}
}
if (allowed == true) {
new MultiThread(socket).start();
} else {
outToClient.writeBytes("Connection refused" + "\n");
socket.close();
forbidden.close();
}
} else {
new MultiThread(socket).start();
}
}
serverSocket.close();
}
}
Upvotes: 0
Views: 290
Reputation: 76709
One of the answers has already indicated how to enable JDWP on the server. This would require that you have appropriate permissions to open the necessary port as well.
If you do not have this permission, I would humbly suggest using a logger. Your code is completely bereft of any logging calls that would have aided in a scenario where you do not have a lot of control over the runtime environment.
You could start with using the logging framework within Java - java.util.logging
, but you'll find log4j or slf4j to be more intuitive.
Upvotes: 0
Reputation: 8036
You will have to enable debug on server JVM. This is typically done via following JVM args -
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787
You then need to connect to the process using server's IP Address and the above debug port - 8787
Upvotes: 1