RMIclient,
THIS IS THE CLIENT
package rmiclient;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.*;
public class RMIClient {
public static void main(String[] args) {
RMIClient client = new RMIClient();
System.setSecurityManager(new RMISecurityManager());
if(args.length == 0)
{
System.out.println("Please enter the correct server, exiting.");
System.exit(0);
}
client.runMenu(args);
}//end Main()
public void runMenu(String[] args) {
while (true) {
int commandToRun = 0;
int numberOfThreads;
Scanner commandScanner = new Scanner(System.in);
Scanner threadScanner = new Scanner(System.in);
System.out.println();
System.out.println("Enter the number for the command that you wish to execute.");
System.out.println("1. Date & Time \n"
+ "2. Server Uptime \n"
+ "3. Memory Usage \n"
+ "4. Netstat \n"
+ "5. Current Users \n"
+ "6. Running processes \n"
+ "7. Exit");
commandToRun = commandScanner.nextInt();
System.out.println();
if (commandToRun == 7) {
System.out.println("Exiting");
System.exit(0);
} else if(commandToRun > 7 || commandToRun < 1) {
System.out.println("Enter a valid command");
}
/* else {
if(commandToRun == 1 || commandToRun == 4)
{
System.out.println("Enter number of threads to run");
numberOfThreads = threadScanner.nextInt();
clientThreads thread[] = new clientThreads[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
thread[i] = new clientThreads(args, commandToRun);
}
for (int i = 0; i < numberOfThreads; i++) {
thread[i].start();
try{
thread[i].join();
}
catch(Exception e){}
}
}
else{
numberOfThreads = 1;
clientThreads thread[] = new clientThreads[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
thread[i] = new clientThreads(args, commandToRun);
}
for (int i = 0; i < numberOfThreads; i++) {
thread[i].start();
try{
thread[i].join();
}
catch(Exception e){}}
}*/
}
}
}
class clientThreads extends Thread {
int commandToRun;
String host;
public clientThreads(String[] args, int commandToRun) {
this.commandToRun = commandToRun;
this.host = args[0];
}
public void run() {
String serverResponse = "";
try {
Registry registry = LocateRegistry.getRegistry(1225);
RMIInterface stub = (RMIInterface)registry.lookup("runCommand");
serverResponse = stub.runCommand(commandToRun);
} catch (Exception e) {
System.out.println(e);
}
System.out.println();
System.out.println(serverResponse);
}
}
RMIINTERFACE, Interface
package rmiclient;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RMIInterface extends Remote {
String runCommand(int commandToRun) throws RemoteException;
}
RMIserver, this is the server
package rmiserver;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
//import java.rmi.RMISecurityManager;
import java.io.*;
public class RMIServer implements RMIInterface {
public RMIServer() {}
public static void main(String[] args) {
try {
RMIServer obj = new RMIServer();
// System.setSecurityManager(new RMISecurityManager());
RMIInterface stub = (RMIInterface) UnicastRemoteObject.exportObject(obj, 0);
Registry registry = LocateRegistry.getRegistry(1225);
registry.rebind("runCommand", stub);
System.out.println("Server starting");
} catch (Exception e) {
System.out.println(e);
}
}
public String runCommand(int commandToRun) throws RemoteException {
Runtime runtime = null;
Process process = null;
String command = "";
String output = "";
String newLine = "";
BufferedReader inp = null;
try {
if (commandToRun == 1) {
System.out.println("Running command: Date and Time");
runtime = Runtime.getRuntime();
command = "date";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
output = inp.readLine();
} else if (commandToRun == 2) {
System.out.println("Running command: Uptime");
runtime = Runtime.getRuntime();
command = "uptime";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
output = inp.readLine();
} else if (commandToRun == 3) {
System.out.println("Running command: Memory Use");
runtime = Runtime.getRuntime();
command = "free";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((newLine = inp.readLine()) != null) {
output = output + "\n" + newLine;
}
} else if (commandToRun == 4) {
System.out.println("Running command: Netstat");
runtime = Runtime.getRuntime();
command = "netstat";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((newLine = inp.readLine()) != null) {
output = output + "\n" + newLine;
}
} else if (commandToRun == 5) {
System.out.println("Running command: Current Users");
runtime = Runtime.getRuntime();
command = "who";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((newLine = inp.readLine()) != null) {
output = output + "\n" + newLine;
}
} else if (commandToRun == 6) {
System.out.println("Running command: Running Processes");
runtime = Runtime.getRuntime();
command = "ps -e";
process = runtime.exec(command);
inp = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((newLine = inp.readLine()) != null) {
output = output + "\n" + newLine;
}
}
} catch (Exception e) {
System.out.println(e);
}
return output;
}
}
Please help, i feel like i'm pretty close to getting the client and server. were pretty close to getting them connected utilizing the SSH secure shell.
after the client and server start communicating it should be easy to finish this program, when finished it will be able to take input from client (a number 1-10) and based off that number give the response from the server.
Related
i have problem when i use php java bridge i got error like this
Warning: require_once(http://localhost:8080/JavaBridge/java/Java.inc): failed to open stream: HTTP request failed! in /Library/WebServer/Documents/test2.php on line 3
Fatal error: require_once(): Failed opening required 'http://localhost:8080/JavaBridge/java/Java.inc' (include_path='.:') in /Library/WebServer/Documents/test2.php on line 3
my tomcat has running at port 8080. i use force to use port 8080 but i still got error
here my standalone class for java bridge
package php.java.bridge;
import php.java.bridge.util.Thread;
import java.lang.reflect.Method;
import php.java.bridge.http.JavaBridgeRunner;
import php.java.bridge.util.Logger;
import java.io.File;
import javax.swing.Icon;
import java.awt.Component;
import javax.swing.JOptionPane;
import java.net.ServerSocket;
import java.io.IOException;
import php.java.bridge.http.TCPServerSocket;
import php.java.bridge.http.ISocketFactory;
public class Standalone
{
public static final int HTTP_PORT_BASE = 8080;
public static final int HTTPS_PORT_BASE = 8443;
public static ISocketFactory bind(final int logLevel, final String sockname) throws IOException {
ISocketFactory socket = null;
socket = TCPServerSocket.create(sockname, 20);
if (null == socket) {
throw new IOException("Could not create socket: " + sockname);
}
return socket;
}
protected static void disclaimer() {
System.err.println("Copyright (C) 2003, 2006 Jost Boekemeier and others.");
System.err.println("This is free software; see the source for copying conditions. There is NO");
System.err.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
}
protected void javaUsage() {
System.err.println("PHP/Java Bridge version " + Util.VERSION);
disclaimer();
System.err.println("Usage: java -jar JavaBridge.jar [SOCKETNAME LOGLEVEL LOGFILE]");
System.err.println("SOCKETNAME is one of INET_LOCAL, INET, HTTP_LOCAL, HTTP, HTTPS_LOCAL, HTTPS");
System.err.println("");
System.err.println("Example 1: java -jar JavaBridge.jar");
System.err.println("Example 2: java -jar JavaBridge.jar HTTP_LOCAL:8080 3 JavaBridge.log");
System.err.println("Example 3: java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=YOURPASSWD -jar JavaBridge.jar HTTPS:8443 3 JavaBridge.log");
System.err.println("The certificate for example 3 can be created with e.g.: jdk1.6.0/bin/keytool -keystore mySrvKeystore -genkey -keyalg RSA");
System.err.println("");
System.err.println("Influential system properties: threads, daemon, php_exec, default_log_file, default_log_level, base.");
System.err.println("Example: java -Djava.awt.headless=\"true\" -Dphp.java.bridge.threads=50 -Dphp.java.bridge.base=/usr/lib/php/modules -Dphp.java.bridge.php_exec=/usr/local/bin/php-cgi -Dphp.java.bridge.default_log_file= -Dphp.java.bridge.default_log_level=5 -jar JavaBridge.jar");
System.err.println("Example: java -Dphp.java.bridge.daemon=\"true\" -jar JavaBridge.jar");
}
protected void usage() {
this.javaUsage();
System.exit(1);
}
protected void checkOption(final String[] s) {
if ("--version".equals(s[0])) {
System.out.println(Util.VERSION);
System.exit(0);
}
this.usage();
}
private static boolean testPort(final int port) {
try {
final ServerSocket sock = new ServerSocket(port);
sock.close();
return true;
}
catch (IOException ex) {
return false;
}
}
private static int findFreePort(final int start) {
for (int port = start; port < start + 100; ++port) {
if (testPort(port)) {
return port;
}
}
return start;
}
public void init(final String[] s) {
String sockname = null;
int logLevel = -1;
// final String tcpSocketName = "9267";
final String tcpSocketName = "8080";
if (s.length > 3) {
this.checkOption(s);
}
try {
if (s.length > 0) {
sockname = s[0];
if (sockname.startsWith("-")) {
this.checkOption(s);
}
}
try {
if (s.length > 1) {
logLevel = Integer.parseInt(s[1]);
}
}
catch (NumberFormatException e2) {
this.usage();
}
catch (Throwable t) {
t.printStackTrace();
}
if (s.length == 0) {
try {
/* final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "8080";
}
catch (Throwable t2) {}
}
if (s.length == 0) {
TCPServerSocket.TCP_PORT_BASE = Integer.parseInt(tcpSocketName);
}
if (checkServlet(logLevel, sockname, s)) {
return;
}
final ISocketFactory socket = bind(logLevel, sockname);
if ("true".equals(System.getProperty("php.java.bridge.test.startup"))) {
System.exit(0);
}
JavaBridge.initLog(String.valueOf(socket), logLevel, s);
JavaBridge.init(socket, logLevel, s);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public static File getCanonicalWindowsFile(final String path) {
try {
return new File(path).getCanonicalFile();
}
catch (IOException e) {
return new File(path);
}
}
private static boolean checkServlet(final int logLevel, String sockname, final String[] s) throws InterruptedException, IOException {
if (sockname == null) {
return false;
}
if (sockname.startsWith("SERVLET_LOCAL:") || sockname.startsWith("HTTP_LOCAL:") || sockname.startsWith("HTTPS_LOCAL:")) {
Util.JAVABRIDGE_PROMISCUOUS = false;
System.setProperty("php.java.bridge.promiscuous", "false");
}
else {
if (!sockname.startsWith("SERVLET:") && !sockname.startsWith("HTTP:") && !sockname.startsWith("HTTPS:")) {
return false;
}
Util.JAVABRIDGE_PROMISCUOUS = true;
System.setProperty("php.java.bridge.promiscuous", "true");
}
final boolean isSecure = sockname.startsWith("HTTPS");
JavaBridge.initLog(sockname, logLevel, s);
sockname = sockname.substring(sockname.indexOf(58) + 1);
final String serverPort = (Util.JAVABRIDGE_PROMISCUOUS ? "INET:" : "INET_LOCAL:") + sockname;
Logger.logMessage("JavaBridgeRunner started on port " + serverPort);
Class runner = JavaBridgeRunner.class;
JavaBridgeRunner r;
try {
runner = Util.classForName("php.java.script.JavaBridgeScriptRunner");
final Method m = runner.getMethod("getRequiredInstance", String.class, Boolean.TYPE);
r = (JavaBridgeRunner)m.invoke(runner, serverPort, new Boolean(isSecure));
}
catch (Throwable e) {
r = JavaBridgeRunner.getRequiredInstance(serverPort, isSecure);
}
r.waitFor();
r.destroy();
return true;
}
private static final boolean checkGNUVM() {
try {
return "libgcj".equals(System.getProperty("gnu.classpath.vm.shortname"));
}
catch (Throwable t) {
return false;
}
}
public static void main(final String[] s) {
if (!System.getProperty("php.java.bridge.daemon", "false").equals("false")) {
final String[] args = new String[s.length + 8];
args[0] = System.getProperty("php.java.bridge.daemon");
if ("true".equals(args[0])) {
args[0] = "java";
}
args[1] = "-Djava.library.path=" + System.getProperty("java.library.path", ".");
args[2] = "-Djava.ext.dirs=" + System.getProperty("java.ext.dirs", ".");
args[3] = "-Djava.awt.headless=" + System.getProperty("java.awt.headless", "true");
args[4] = "-Dphp.java.bridge.asDaemon=true";
args[5] = "-classpath";
args[6] = System.getProperty("java.class.path", ".");
args[7] = "php.java.bridge.Standalone";
for (int j = 0; j < s.length; ++j) {
args[j + 8] = s[j];
}
try {
System.in.close();
System.out.close();
System.err.close();
}
catch (IOException e) {
System.exit(12);
}
new Thread(new Runnable() {
#Override
public void run() {
try {
Runtime.getRuntime().exec(args);
}
catch (IOException e) {
System.exit(13);
}
}
}).start();
try {
java.lang.Thread.sleep(20000L);
}
catch (Throwable t2) {}
System.exit(0);
}
try {
System.loadLibrary("natcJavaBridge");
}
catch (Throwable t3) {}
try {
final String cp = System.getProperty("java.class.path", ".");
File jbFile = null;
final boolean isExecutableJavaBridgeJar = cp.indexOf(File.pathSeparatorChar) == -1 && cp.endsWith("JavaBridge.jar") && (jbFile = new File(cp)).isAbsolute();
final File wd = getCanonicalWindowsFile(isExecutableJavaBridgeJar ? jbFile.getParent() : "");
final boolean sunJavaInstalled = new File("/usr/java/default/bin/java").exists();
final String javaExec = sunJavaInstalled ? "/usr/java/default/bin/java" : "java";
if (s.length == 0 && System.getProperty("php.java.bridge.exec_sun_vm", "true").equals("true") && ((sunJavaInstalled && checkGNUVM()) || isExecutableJavaBridgeJar)) {
final Process p = Runtime.getRuntime().exec(new String[] { javaExec, "-Dphp.java.bridge.exec_sun_vm=false", "-classpath", cp, "php.java.bridge.Standalone" }, null, wd);
if (p != null) {
System.exit(p.waitFor());
}
}
}
catch (Throwable t4) {}
try {
new Standalone().init(s);
}
catch (Throwable t) {
t.printStackTrace();
System.exit(9);
}
}
}
in this source code i force manual choice 8080 using this code
/* final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "8080";
}
and at tcpServer i force to choice that port use this code
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.InetAddress;
import java.io.IOException;
import java.net.ServerSocket;
public class TCPServerSocket implements ISocketFactory
{
public static int TCP_PORT_BASE;
private ServerSocket sock;
private int port;
boolean local;
public static ISocketFactory create(String name, final int backlog) throws IOException {
boolean local = false;
if (name == null) {
throw new NullPointerException("name");
}
if (name.startsWith("INET:")) {
name = name.substring(5);
}
else if (name.startsWith("INET_LOCAL:")) {
local = true;
name = name.substring(11);
}
final int p = Integer.parseInt(name);
final TCPServerSocket s = new TCPServerSocket(p, backlog, local);
return s;
}
private ServerSocket newServerSocket(final int port, final int backlog) throws IOException {
try {
if (this.local) {
return new ServerSocket(port, backlog, InetAddress.getByName("127.0.0.1"));
}
}
catch (UnknownHostException ex) {}
return new ServerSocket(port, backlog);
}
private void findFreePort(final int start, final int backlog) {
int port = start;
while (port < start + 100) {
try {
this.sock = this.newServerSocket(port, backlog);
this.port = port;
return;
}
catch (IOException e) {
++port;
continue;
}
}
}
private TCPServerSocket(final int port, final int backlog, final boolean local) throws IOException {
this.local = local;
if (port == 0) {
this.findFreePort(TCPServerSocket.TCP_PORT_BASE, backlog);
}
else {
this.sock = this.newServerSocket(port, backlog);
this.port = port;
}
}
#Override
public void close() throws IOException {
this.sock.close();
}
#Override
public Socket accept() throws IOException {
final Socket s = this.sock.accept();
s.setTcpNoDelay(true);
return s;
}
#Override
public String getSocketName() {
return String.valueOf(this.port);
}
#Override
public String toString() {
return (this.local ? "INET_LOCAL:" : "INET:") + this.getSocketName();
}
static {
// TCPServerSocket.TCP_PORT_BASE = 9267;
TCPServerSocket.TCP_PORT_BASE = 8080;
}
}
but my javabridge cannot open stream at that port why like that ? any idea for choice only port 8080 not choice the other ?
this problem close after i change this code
try { /*
final int tcpSocket = Integer.parseInt(tcpSocketName);
final int freeJavaPort = findFreePort(tcpSocket);
final int freeHttpPort = findFreePort(8080);
final int freeHttpsPort = findFreePort(8443);
final Object result = JOptionPane.showInputDialog(null, "Start a socket listener on port", "Starting the PHP/Java Bridge ...", 3, null, new String[] { "INET_LOCAL:" + freeJavaPort, "INET:" + freeJavaPort, "HTTP_LOCAL:" + freeHttpPort, "HTTP:" + freeHttpPort, "HTTPS_LOCAL:" + freeHttpsPort, "HTTPS:" + freeHttpsPort }, "HTTP_LOCAL:" + freeHttpPort);
//final Object result = 8080;
if (result == null) {
System.exit(0);
}*/
//sockname = result.toString();
sockname = "HTTP_LOCAL:8080";
System.out.println("sockname"+sockname);
}
so sockname use "HTTP_LOCAL:8080" not "8080"
I'm using a java server to facilitate online multiplayer in my game made with GameMaker Studio, the players will send data to the java server which will process the data and send it to the players. The problem is that when a player with a slow internet connection is not being able to handle the amount of data being send to it, it will cause the server to freeze for all players (the server will no longer process any data send by other players).
I have simulated slow internet speeds by using NetLimiter and setting the download speed of one laptop at 5 kb/s, while maintaining proper speed at other laptops. I have tried to send ACK packets from the java server to the client and if it doesn't respond in 1 second no more data will be send to that client (and eventually the client will be kicked). This has reduced the chance of freezing the server, but it will still happen occasionally.
Main.java
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.util.HashMap;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
public class Main
{
static ServerSocket serverSocket_;
static HashMap<String, ServerInformation> servers_;
static int verboseLevel_;
static int threadTimeout_;
static int masterPort_;
static int serverNumber_;
static int socketTimeOut_;
static {
Main.serverSocket_ = null;
Main.servers_ = new HashMap<String, ServerInformation>();
Main.verboseLevel_ = 5;
Main.threadTimeout_ = 10;
Main.masterPort_ = 6510;
Main.serverNumber_ = 1;
Main.socketTimeOut_ = 6000;
}
public static void main(final String[] args) {
try {
setupServerAndCleanup(Main.masterPort_);
while (true) {
handleIncomingConnection();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
static void setupServerAndCleanup(final int port) throws IOException {
(Main.serverSocket_ = new ServerSocket()).setReuseAddress(true);
Main.serverSocket_.bind(new InetSocketAddress(Main.masterPort_));
System.out.println("Server socket up and running on port " + Main.masterPort_);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
#Override
public void run() {
if (Main.serverSocket_ != null) {
try {
Main.serverSocket_.close();
System.out.println("Server socket closed, port released");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}));
}
static void handleIncomingConnection() throws IOException {
final Socket clientSocket = Main.serverSocket_.accept();
clientSocket.setSoTimeout(Main.socketTimeOut_);
final ClientThread client = new ClientThread(clientSocket);
client.start();
}
}
ClientThread.java
Case 1 is the part dealing with sending data to the clients, in particular this line:
thread2.out_.print(msg);
If more data is being send than one client can handle the server will freeze for all other clients as well.
import java.util.Iterator;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.util.regex.Pattern;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class ClientThread extends Thread
{
Socket clientSocket_;
String clientIp_;
String serverIp_;
ServerInformation server_;
PrintWriter out_;
BufferedReader in_;
boolean prepareTermination_;
boolean terminated_;
private static final Pattern numberPattern;
static {
numberPattern = Pattern.compile("\\d+");
}
public ClientThread(final Socket sock) {
this.clientSocket_ = sock;
this.clientIp_ = this.clientSocket_.getRemoteSocketAddress().toString();
this.serverIp_ = null;
this.server_ = null;
this.prepareTermination_ = false;
this.terminated_ = false;
}
#Override
public void run() {
try {
this.out_ = new PrintWriter(this.clientSocket_.getOutputStream(), true);
this.in_ = new BufferedReader(new InputStreamReader(this.clientSocket_.getInputStream()));
long lastActionTime = System.currentTimeMillis();
while (true) {
if (this.in_.ready() || System.currentTimeMillis() - lastActionTime >= 1000 * Main.threadTimeout_) {
if (System.currentTimeMillis() - lastActionTime >= 1000 * Main.threadTimeout_) {
//this.logDebugMessage(3, "Thread was killed due to prolonged inactivity (" + Main.threadTimeout_ + " seconds)");
this.terminateThread();
return;
}
final String tempInputLine;
if(((tempInputLine = this.in_.readLine()) == null )){
this.terminateThread(); //end thread
return;
}
else
{
lastActionTime = System.currentTimeMillis();
final String inputLine = tempInputLine.trim();
if (ClientThread.numberPattern.matcher(inputLine).matches()){
final int val = Integer.parseInt(inputLine);
switch (val) {
case 1: { //send data to other players
final int parseCount = Integer.parseInt(this.in_.readLine().trim());
final StringBuilder msg = new StringBuilder();
for (int j = 0; j < parseCount; ++j) {
msg.append(String.valueOf(this.in_.readLine().trim()) + "|");
}
for (final ClientThread thread2 : this.server_.ipToClientThread_.values()) {
if (thread2 != this) {
thread2.out_.print(msg);
thread2.out_.flush();
}
}
//this.logDebugMessage(5, "Packet for others: '" + msg.toString() + "'");
break;
}
case 2: { //remove game server
//this.logDebugMessage(1, "A game server has been deleted, ip: " + ipServer);
Main.servers_.remove(this.server_.ip_);
this.serverIp_ = null;
for (final ClientThread thread : this.server_.ipToClientThread_.values()) {
thread.prepareTermination_ = true;
}
this.terminateThread();
return;
}
case 3: { //connect new client
final String ipServer = this.in_.readLine().trim();
final String ipClient = this.in_.readLine().trim();
this.logDebugMessage(1, "A client wishes to connect to a server, client: " + ipClient + ", server: " + ipServer);
final ServerInformation info = Main.servers_.getOrDefault(ipServer, null);
if (info == null) {
System.out.println("Connection to the server failed, no such server in the server list");
this.out_.print("*" + 1 + "|" + 1 + "~" + "|");
this.out_.flush();
break;
}
this.server_ = info;
this.server_.ipToClientThread_.put(ipClient, this);
this.logDebugMessage(1, "Connection success");
this.logDebugMessage(5,"Map: " + this.server_.ipToClientThread_);
this.out_.print("*" + 1 + "|" + 2 + "~" + "|");
this.out_.flush();
break;
}
case 4: { //disconnect client
final String ipClient = this.in_.readLine().trim();
this.server_.ipToClientThread_.remove(ipClient);
this.logDebugMessage(1, String.valueOf(ipClient) + " disconnected from the server at " + this.server_.ip_);
this.serverIp_ = null;
this.terminateThread();
return;
}
case 5: { //host create new game
if (Main.serverNumber_ > 1000000) {
Main.serverNumber_ = 10;
}
Main.serverNumber_ += 1;
final String ipServer = Integer.toString(Main.serverNumber_); //unique server number
final String ipHost = this.in_.readLine().trim(); //host
final String name = this.in_.readLine().trim(); //Server name
final String description = this.in_.readLine().trim(); //class
final String servervar1 = this.in_.readLine().trim(); //max players
final String servervar3 = this.in_.readLine().trim(); //current lap
final String servervar4 = this.in_.readLine().trim(); //total laps
final String servervar5 = this.in_.readLine().trim(); //status
final String servervar6 = this.in_.readLine().trim(); //Password
final String servervar7 = this.in_.readLine().trim(); //Online version
final String servervar8 = this.in_.readLine().trim(); //Game server
final long servervar9 = System.currentTimeMillis(); //server creation time
//this.logDebugMessage(1, "A game server has been registered, ip: " + ipServer + ", name: " + name + ", description: " + description + ", servervar1: " + servervar1);
final ServerInformation gameServer = new ServerInformation(name, servervar1, servervar3, servervar4, servervar5, servervar6, servervar7, servervar8, servervar9, ipHost, ipServer, this.clientSocket_, this.out_, this.in_);
gameServer.description_ = description;
gameServer.ipToClientThread_.put(ipHost, this);
this.server_ = gameServer;
Main.servers_.put(ipServer, gameServer);
this.serverIp_ = ipServer;
break;
}
default: {
this.logDebugMessage(0, "Unrecognized case: '" + inputLine + "', " + val);
break;
}
}
}
else if (inputLine.length() > 0) {
this.logDebugMessage(0, "Unformated '" + inputLine + "'");
if (this.server_ != null) {
this.server_.out_.print(inputLine);
this.server_.out_.flush();
}
}
if (this.prepareTermination_) {
this.terminateThread();
return;
}
continue;
}
}
}
}
catch (SocketTimeoutException e) {
e.printStackTrace();
try {
this.terminateThread();
}
catch (IOException e2) {
e2.printStackTrace();
}
}
catch (IOException e3) {
e3.printStackTrace();
try {
this.terminateThread();
}
catch (IOException e4) {
e4.printStackTrace();
}
}
}
//debug messages
void logDebugMessage(final int requiredVerbose, final String msg) {
if (Main.verboseLevel_ >= requiredVerbose) {
System.out.println("[" + this.clientIp_ + "] " + msg);
}
}
//terminate thread
void terminateThread() throws IOException {
if (!this.terminated_) {
if (this.serverIp_ != null) {
Main.servers_.remove(this.serverIp_);
}
this.clientSocket_.close();
this.in_.close();
this.out_.close();
this.logDebugMessage(3, "Cleanup successful");
this.terminated_ = true;
}
}
}
How to avoid the server from freezing if more data is being send to a client than it can handle, so that the server can continue sending data to the other clients?
Edit
So I have tried using ExecutorService, but I must be doing something completely wrong because no data is being send by the java server.
for (final ClientThread thread2 : this.server_.ipToClientThread_.values()) {
if (thread2 != this) {
executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
public void run() {
thread2.out_.print(msg);
thread2.out_.flush();
}
});
executorService.shutdown();
}
}
It would be great if you could show me how to implement ExecutorService the right way.
If a delay in the client processing doesn't matter, this part should be done in a distinct flow execution for each client :
for (final ClientThread thread2 : this.server_.ipToClientThread_.values()) {
thread2.out_.print(msg);
thread2.out_.flush();
}
For example :
for (final ClientThread thread2 : this.server_.ipToClientThread_.values()) {
if (thread2 != this) {
new Thread(()-> {
thread2.out_.print(msg);
thread2.out_.flush();
})
.start();
}
}
Note that creating Threads has a cost. Using ExecutorService could be a better idea.
I'm writing a java program whose operation is summarized as follows:
The subfolder with the specified name is selected in the source
folder.
All the pdfs are taken and converted into txt files in
the target folder.
The name of an item is searched inside the
txt.
If the name is found then the pdf from source to target is
copied.
All the txt from the target folder are deleted, in this
way all the pdfs containing the searched word remain.
"It all works", the problem is that the txt files are not deleted from target and I can not understand why. Thanks in advance, best regards.
Launcher.java:
import java.lang.String;
import java.util.Scanner;
import java.io.File;
public class Launcher {
// campi
int status = 1;
static String source = "C:\\Users\\MyUser\\Desktop\\source\\";
static String target = "C:\\Users\\MyUser\\Desktop\\target\\";
public static void main(String [] args) {
// strings
String src = source;
String item = "";
// init
Scanner scan = new Scanner(System.in);
System.out.print("\nFornitore: ");
src += scan.nextLine().toUpperCase() + "\\";
System.out.print("Item: ");
item = scan.nextLine();
// notifier
Launcher launcher = new Launcher();
// threads
MakerThread maker = new MakerThread(launcher, src, target);
SearcherThread searcher = new SearcherThread(launcher, src, target, item);
CleanupThread cleaner = new CleanupThread(launcher, target);
// run
maker.start();
searcher.start();
cleaner.start();
}
}
MakerThread.java:
import java.io.File;
public class MakerThread extends Thread {
// campi
String src = "";
String target = "";
Launcher launcher;
// costruttore
public MakerThread(Launcher launcher, String src, String target) {
this.src = src;
this.target = target;
this.launcher = launcher;
}
// metodi
#Override
public void run() {
try{
synchronized (launcher) {
File folder = new File(this.src);
for (File f : folder.listFiles()) {
// spin-lock
while (launcher.status != 1){
launcher.wait();
}
if(f.isFile() && f.getName().endsWith("pdf")) {
System.out.println("PDF TROVATO: " + f.getName());
// input
String input = win_str(src + f.getName());
// output
String output = target;
output += f.getName().replaceAll(".pdf", ".txt");
output = win_str(output);
// command
String command = "cmd /C java -jar ./pdfbox-app-2.0.11.jar ExtractText ";
command += input + " " + output;
Process p1 = Runtime.getRuntime().exec(command);
p1.waitFor();
}
}
// set status, awake other threads
launcher.status = 2;
launcher.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
private String win_str(String str) {
return "\"" + str + "\"";
}
}
SearcherThread.java:
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
public class SearcherThread extends Thread {
// campi
String src = "";
String target = "";
String item = "";
Launcher launcher;
// costruttore
public SearcherThread(Launcher launcher, String src, String target, String item) {
this.target = target;
this.src = src;
this.item = item;
this.launcher = launcher;
}
// metodi
#Override
public void run() {
try{
synchronized (launcher) {
File folder = new File(this.target);
for(File f : folder.listFiles()) {
// spin-lock
while (launcher.status != 2){
launcher.wait();
}
if(f.isFile() && f.getName().endsWith("txt")) {
System.out.println("SEARCHING IN: " + f.getName());
// init
String line;
BufferedReader br = new BufferedReader(new FileReader(new File(target+f.getName())));
// txt search
while((line = br.readLine()) != null) {
if(line.contains(item)) {
System.out.println(" [" + item + "]" + " trovato in " + f.getName() + "\n");
// input
String pdf = f.getName().replaceAll(".txt", ".pdf");
String input = win_str(src+pdf);
// output
String output = win_str(target);
// command
String command = "cmd /C copy " + input + " " + output;
Process p1 = Runtime.getRuntime().exec(command);
p1.waitFor();
break;
}
}
}
}
// set status, awake other threads
launcher.status = 3;
launcher.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
private String win_str(String str) {
return "\"" + str + "\"";
}
}
CleanupThread.java:
import java.io.File;
public class CleanupThread extends Thread {
// campi
String target = "";
Launcher launcher;
// costruttore
public CleanupThread(Launcher launcher, String target) {
this.target = target;
this.launcher = launcher;
}
// metodi
#Override
public void run() {
try{
synchronized (launcher) {
File folder = new File(this.target);
for (File f : folder.listFiles()) {
// spin-lock
while (launcher.status != 3){
launcher.wait();
}
if(f.isFile() && f.getName().endsWith("txt")) {
System.out.println("CLEANING UP: " + f.getName());
// input
String input = win_str(target + f.getName());
// command
String command = "cmd /C del " + input;
Process p1 = Runtime.getRuntime().exec(command);
p1.waitFor();
}
}
// set status, awake other threads
launcher.status = 1;
launcher.notifyAll();
}
}catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
}
private String win_str(String str) {
return "\"" + str + "\"";
}
}
How can I make this use multiple threads as clients - who all perform a task each within the switch statement? I would also like the threads to perform these tasks and once they have carried them out they stop (currently these tasks are performed from user input).
There are 3 classes the server, client and developer database - which holds a constructor for the 'developer' objects within an ArrayList in the server. This array list is providing the information which is being passed to the clients upon their specific requests.
Here is my code: Client:
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;
public class QuoteOfTheDayClient_2 {
static String devName;
static String devSkill;
static Boolean devHire;
static Socket echoSocket = null; // holds the Socket object
static Scanner fromServer = null; // input stream from remote host
static Scanner fromServer1 = null; // input stream from remote host
static PrintWriter toServer = null; // output stream to remote host
static PrintWriter toServer1 = null; // output stream to remote host
static Scanner stdIn = new Scanner(System.in);
static String remoteServer = "localhost"; // are you 'local'?!
//String remoteServer = "193.61.190.96"; // remote host is not 'local'!
static int port;
static String reply;// holds reply from remote host
public static void searchForSkill(String skill) {
toServer.println(1);
String quoteNum = "php"; //Search for a specific coding language -
Specific to the entered parameters
System.out.println("Searching for Skill: " + skill + " in Developer
Database");
toServer.println(quoteNum);
while (!(reply = fromServer.nextLine()).isEmpty()) {
// System.out.println(reply1);
System.out.println(reply);// print reply to screen
}
fromServer.nextLine(); // Consume the newline
System.out.println();
}
public static void addDeveloper(String name, String skill,boolean
hire)
{
toServer.println(2);
System.out.println("Add Developer to system ");
stdIn.nextLine(); // flush newline
String quoteString = name;//stdIn.nextLine();
String quoteString1 = skill;//stdIn.nextLine();
boolean quoteString2 = hire;//stdIn.nextLine();
toServer.println(quoteString);
toServer.println(quoteString1);
toServer.println(quoteString2);
fromServer.next();
System.out.println("\nDeveloper entered: " + quoteString + "\n");
while (!(reply = fromServer.nextLine()).isEmpty()) {
// System.out.println(reply1);
System.out.println(reply);// print reply to screen
}
}
public static void checkAvailability(String developerName) {
try {
toServer.println(3);
System.out.println("Send Developer Name to Find Availability:
");
String quoteDel = developerName;
toServer.println(quoteDel);
while (!(reply = fromServer.nextLine()).isEmpty()) {
// System.out.println(reply1);
System.out.println(reply);// print reply to screen
}
} catch (NoSuchElementException e) {
}
}
public static void hireDeveloper(String name) {
toServer.println(4);
String quoteNum = name; //Search for a specific coding language -
Specific to the entered parameters
System.out.println("Searching for: " + name + " in Developer
Database");
toServer.println(quoteNum);
while (!(reply = fromServer.nextLine()).isEmpty()) {
// System.out.println(reply1);
System.out.println(reply);// print reply to screen
}
fromServer.nextLine(); // Consume the newline
System.out.println();
}
public static void printAllDevelopers(){
toServer.println(5);
while (!(reply = fromServer.nextLine()).isEmpty()) {
// System.out.println(reply1);
System.out.println(reply);// print reply to screen
}
fromServer.nextLine(); // Consume the newline
System.out.println();
}
public static void clientMenu(){
try {
port = 4444; // 17 for localhost
echoSocket = new Socket(remoteServer, port);
fromServer = new Scanner(echoSocket.getInputStream());
fromServer1 = new Scanner(echoSocket.getInputStream());
toServer = new PrintWriter(echoSocket.getOutputStream(), true);
toServer1 = new PrintWriter(echoSocket.getOutputStream(),
true);
OutputStream os = echoSocket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
} catch (Exception e) {
System.err.println("Exception: " + e);
System.exit(1);
} // try-catch
String reply;// holds reply from remote host
int command;
String reply1;
String devName="David Blaine";
String devSkill="Java";
System.out.println("Enter command: 1:Search Skill 2:Add
Developer 3:Search Availability 4:Hire Developer 5:Show all
Developers 6:Exit");
command = 0;
command = command+1;
// while (command != 0) {
for(int i=1;i<6;i++) {
command=i;
switch (i) {
case 1: {
// get quote
searchForSkill("PHP");
break;
}
case 2: {
// add quote
addDeveloper("David Blaine", "Java", true);
break;
}
case 3: {
// delete quote
checkAvailability("David Ferguson");
break;
}
case 4: {
hireDeveloper("Thompson");
break;
}
case 5: {
printAllDevelopers();
}
case 6: {
System.exit(6);
}
default:
System.out.println("*** Command not recognised ***");
break;
}
System.out.println("Enter command: 1:Search Skill 2:Add Developer
3:Search Availability 4:Hire Developer 5:Show all Developers
6:Exit");
command = stdIn.nextInt();
//}
}
toServer.println(command);
try {
echoSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
fromServer.close();
toServer.close();
stdIn.close();
}
public static void main(String[] args) throws IOException {
clientMenu();
} // main
} // EchoClient
Server code:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class QuoteOfTheDayServer_2 {
private static DeveloperDatabase[] developerList = new
DeveloperDatabase[7];
static DeveloperDatabase temp []=new DeveloperDatabase[7];
static String userAddDevName;
static String userAddCodeSkills;
static Boolean userAddHireAvailability;
static int command;
static List<DeveloperDatabase> list = new
ArrayList<DeveloperDatabase>(){
{
add(new DeveloperDatabase(1,"David Ferguson", "Java",
true));
add(new DeveloperDatabase(2,"Grant Thompson", "Java",
true));
add(new DeveloperDatabase(3,"Craig Jackson", "C++", true));
add(new DeveloperDatabase(4,"Kevin James","C++", true));
add(new DeveloperDatabase(5,"Greg Troupe", "Python",
true));
add(new DeveloperDatabase(6,"James Smith", "Python",
true));
add(new DeveloperDatabase(7,"Jamie Richie", "PHP", true));
add(new DeveloperDatabase(8,"Rory McGrattan", "PHP",
false));
}
};
public static String printJava(){
String message="";
for (int i=0;i<2;i++){
message +=list.get(i).toString();
message+=" \n";
}
return message;
}
public static String printC(){
String message="";
for (int i=2;i<4;i++){
message +=list.get(i).toString();
message+=" \n";
}
return message;
}
public static String printPy(){
String message="";
for (int i=4;i<6;i++){
message +=list.get(i).toString();
message+=" \n";
}
return message;
}
public static String printPHP(){
String message="";
for (int i=6;i<8;i++){
message +=list.get(i).toString();
message+=" \n";
}
return message;
}
public static void addDeveloper(String name,String
codeSkill,Boolean hire){
userAddDevName=name;
userAddCodeSkills = codeSkill;
userAddHireAvailability = hire;
System.out.println("*********Adding Developer***********");
System.out.println(userAddDevName+ " has been added to the
system");
list.add(new DeveloperDatabase(9,
userAddDevName,userAddCodeSkills, userAddHireAvailability));
System.out.println(list.get(8));
}//addDeveloper
public static String checkAvail(String clientMessage){
String message="";
for (int i=0;i<list.size();i++){
if
(clientMessage.equalsIgnoreCase(list.get(i).getDeveloperName())) {
message+= list.get(i).toString();
message+="\n";
}
} return message;
}
public static String printAllDevelopers(){
String message="";
for (int i=0;i<8;i++){
message +=list.get(i).toString();
message+=" \n";
}
return message;
}
public static void main(String[] args) throws IOException {
// Wrap the standard System.in stream with a Scanner so that
Scanner stdIn = new Scanner(System.in);
Scanner fromClient = null;
PrintWriter toClient = null;
PrintWriter toClient1=null;
int pNum = 4444;
ServerSocket sSocket = null;
Socket cSocket = null;
try {
sSocket = new ServerSocket(pNum);
} catch (IOException e) {
System.out.println("Couldn't listen on port: " + pNum + ";
" +
e);
System.exit(1);
}
// Continuously wait for client; accept them; and send a quote
to
them.
while (true) {
try {
System.out.println("Waiting for client ....");
// Blocking 'accept' i.e. wait for a client
cSocket = sSocket.accept();
System.out.println("Client connected from: " +
cSocket.getInetAddress());
// Set up the output stream for the server, i.e. a
PrintWriter stream
toClient = new PrintWriter(cSocket.getOutputStream(),
true);
toClient1 = new PrintWriter(cSocket.getOutputStream(),
true);
ObjectInputStream ois = new
ObjectInputStream(cSocket.getInputStream());
fromClient = new Scanner(cSocket.getInputStream());
try {
command = fromClient.nextInt();
//fromClient.nextLine();
} catch (InputMismatchException e){
System.out.print("Your selection can only be an
integer!");
}
System.out.println("Received command: " + command + "
from
client at " + cSocket.getInetAddress());
while (command != 0) {
switch (command) {
case 1: // get skills
{
System.out.println("GET skill");
fromClient.nextLine();
String quoteNum =
fromClient.nextLine();//receive from client
if (quoteNum.equalsIgnoreCase("Java")) {
toClient1.println(printJava());
} else if
(quoteNum.equalsIgnoreCase("C++")) {
toClient1.println(printC());
} else if
(quoteNum.equalsIgnoreCase("Python"))
{
toClient.println(printPy());
} else if
(quoteNum.equalsIgnoreCase("PHP")) {
toClient.println(printPHP());
}
else {
toClient.println("No developer with
those
skills found");
}
//
toClient.println(list.equals("Java"));//number -1 for array (0) to
client print
System.out.println("Pushed Developer
number: "
+ quoteNum + " to client at " + cSocket.getInetAddress());
//toClient.println(quotes[quoteNum]); //
return
quote //if "java' { get(0) get(1)
break;
}
case 2: // add quote
{
System.out.println("ADD Developer to
System");
fromClient.nextLine(); // flush newline
String quote = fromClient.next();
fromClient.nextLine();
String quote1 = fromClient.next();
fromClient.nextLine();
boolean quote2 = fromClient.nextBoolean();
toClient.println("The\t*************Developer
has been Added to the Developer Database********");
list.add(new DeveloperDatabase(9, quote,
quote1, quote2));
toClient.println(list.get(8).toString());
//addDeveloper(quote,quote1,quote2);
System.out.println(list.get(8));
//System.out.println("Received quote from
client:" + quote);
quote = quote + "\n\n";
//list.add((quote);
System.out.println("Added Developer: " +
quote);
break;
}
case 3: // Check if Developer is available
{
System.out.println("Check if developer is
Available\n");
fromClient.nextLine();
String quoteNum = fromClient.nextLine();
System.out.println("Checking availability
for:
" + quoteNum);
if (quoteNum.equalsIgnoreCase("Rory
McGrattan")) {
toClient.println("The Developer is not
available for hire");
} else {
toClient.println("The Developer is
available for hire");
}break;
}case 4: {
System.out.println("Hire A Developer");
String quoteNum =fromClient.next();
System.out.println("Client wants to hire:
"+quoteNum);
String message = quoteNum;
for(int i=0;i<list.size();i++){
if
(quoteNum.equalsIgnoreCase(list.get(i).getDeveloperName())){
list.get(i).hireDeveloper();
}//if
}//if
//System.out.println(message);
toClient.println("******* You have Hired:
"+
message+" ********");
break;
}
case 5: {// Print all Developers
String message="";
for (int i=0;i<8;i++){
message +=list.get(i).toString();
message+=" \n";
}
toClient.println(message);
}
default:
break;
}
try {
//command = fromClient.nextInt();
}catch(InputMismatchException e){
}
System.out.println("Received command: " + command +
"from client at " + cSocket.getInetAddress());
}
// Close all streams
toClient.close();
cSocket.close();
fromClient.close();
stdIn.close();
} catch (IOException e) {
System.out.println("Accept failed: " + pNum + "; " +
e);
System.exit(1);
}
}
} // main
} // QuoteOfTheDayServer_2
DeveloperDatabase code:
public class DeveloperDatabase {
private int id;
private String developerName;
private String codingLanguage;
private boolean freeForHire;
public DeveloperDatabase(){
}
public DeveloperDatabase(int id,String name,String codeSkills,boolean
hire){
this.id = id;
this.developerName = name;
this.codingLanguage=codeSkills;
this.freeForHire=hire;
}
public String toString() {
String message = "\tName: " +developerName;
message += "\n" + "\tCoding Language: "+ codingLanguage+"\n";
if (freeForHire== true) {
message += "\tThe developer is Available! for Hire!\n";
} else {
message += "\tThe developer is not Available for Hire!\n";
}
return message;
}//toString
public boolean getFreeForHire() {
if (freeForHire==true){
System.out.println("The Developer is free to hire!");
}else{
System.out.println("The Developer is not free to hire!");
}
return freeForHire;
}
public String getCodingLanguage() {
return codingLanguage;
}
public void hireDeveloper(){
if (freeForHire==true)
freeForHire = false;
System.out.println(developerName +" is Hired!");
}
public String getDeveloperName() {
return developerName;
}
public static void main(String[] args) {
}//main
}//class
you could use something like
//define executor service to have 5 thread
1)
ExecutorService service=Executors.newFixedThreadPool(5);
2)
//replace move code insde while (command != 0) into a separate class that implements runnable, lets call this Task.
public class Task implements Runnable{
private final Integer command;
public Task(Integer command) {
this.command=command;
}
#Override
public void run() {
//do something, like switch statement
}
}
3) create a helper method, and call this method
public void process(Task t){
service.submit(t);
}
I am following a tutorial to create a web crawler in java. When I run the code my crawledURL is null. *** Malformed URL :null in a infinite loop.
Can anyone explain to me why is this happening?
Here is the whole code:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.net.*;
public class WebCrawler {
public static Queue<String> Queue = new LinkedList<>();
public static Set<String> marked = new HashSet<>();
public static String regex = "http[s]://(\\w+\\.)*(\\w+)";
public static void bfsAlgorithm(String root) throws IOException {
Queue.add(root);
while (!Queue.isEmpty()) {
String crawledURL = Queue.poll();
System.out.println("\n=== Site crawled : " + crawledURL + "===");
//Limiting to a 100 websites here
if(marked.size() > 100)
return;
boolean ok = false;
URL url = null;
BufferedReader br = null;
while (!ok) {
try {
url = new URL(crawledURL);
br = new BufferedReader(new InputStreamReader(url.openStream()));
ok = true;
} catch (MalformedURLException e) {
System.out.println("*** Malformed URL :" + crawledURL);
crawledURL = Queue.poll();
ok = false;
} catch (IOException ioe) {
System.out.println("*** IOException for URL :" + crawledURL);
crawledURL = Queue.poll();
ok = false;
}
}
StringBuilder sb = new StringBuilder();
while((crawledURL = br.readLine()) != null) {
sb.append(crawledURL);
}
crawledURL = sb.toString();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(crawledURL);
while (matcher.find()){
String w = matcher.group();
if (!marked.contains(w)) {
marked.add(w);
System.out.println("Site added for crawling : " + w);
Queue.add(w);
}
}
}
}
public static void showResults() {
System.out.println("\n\nResults :");
System.out.print("Web sites craweled: " + marked.size() + "\n");
for (String s : marked) {
System.out.println("* " + s);
}
}
public static void main(String[] args) {
try {
bfsAlgorithm("http://www.ssaurel.com/blog");
showResults();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
while (!Queue.isEmpty()) {
String crawledURL = Queue.poll();
...
} catch (MalformedURLException e) {
crawledURL = Queue.poll();
second time you do not check is queue empty