I am using WAMP and I have a module which supports WebSockets. I intend to implement a standalone Java project which will start WAMP in a new Thread
, initializes a Java ServerSocket
at a given port
and at localhost as address and to listen to connections on that port
. So far I am able to start WAMP, but I have a problem with initializing the ServerSocket
and receiving actual messages. These are my Java classes:
WebSocket.java
package websocket;
import java.util.Scanner;
public class WebSocket {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String command = "";
Scanner scanner = new Scanner(System.in);
do {
String[] commandArray = command.split(" ");
Command.executeCommand(commandArray);
command = scanner.nextLine();
System.out.println("Command: " + command);
} while (!command.equals("exit"));
Command.exit();
System.out.println("Finished");
System.exit(0);
}
}
Command.java
package websocket;
public class Command {
private static WAMP wamp;
private static WS ws;
public static void WAMPMethod(String[] commandArray) {
if (wamp == null) {
wamp = WAMP.create(commandArray);
}
}
public static void WSMethod(String[] commandArray) {
if (ws == null) {
ws = WS.create();
}
}
private static void stopThread(Thread t) {
if (t != null) {
try {
t.join();
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static void exit() {
stopThread(wamp);
stopThread(ws);
}
public static void executeCommand(String[] commandArray) {
if (commandArray[0].equals("WAMP")) {
WAMPMethod(commandArray);
} else if (commandArray[0].equals("WS")) {
WSMethod(commandArray);
}
}
}
WAMP.java
package websocket;
public class WAMP extends Thread {
public String[] command;
public WAMP(String[] command) {
this.command = command;
}
public static WAMP create(String[] command) {
WAMP wamp = new WAMP(command);
wamp.start();
return wamp;
}
@Override
public void run() {
if (command[1].equals("Start")) {
StartWAMP();
}
}
private void StartWAMP() {
try {
Runtime runTime = Runtime.getRuntime();
runTime.exec("C:\\wamp\\wampmanager.exe");
System.out.println("WAMP is started");
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
WS.java
package websocket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class WS extends Thread {
public static int port = 4444;
public static WS create() {
WS ws = new WS();
ws.start();
return ws;
}
public WS() {
}
@Override
public void run() {
try {
InetAddress addr = InetAddress.getByName("127.0.0.1");
ServerSocket serverSocket = new ServerSocket(port, 50, addr);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String command = "";
System.out.println("WS Started");
while (!command.equals("exit")) {
command = in.readLine();
System.out.println(command);
}
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
When I start the Java project, I issue WAMP Start
and WS
commands and my output is:
WAMP Start
Command: WAMP Start
WAMP is started
WS
Command: WS
The WS Thread enters a waiting state at the line of
Socket clientSocket = serverSocket.accept();
After WAMP is started, I execute the following PHP script:
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$mess = "This is a message";
socket_sendto($sock, $mess, strlen($mess), 0, '127.0.0.1', 4444);
socket_close($sock);
however, my Java program does not continue its process, which means that it still did not establish a socket connection. How could I fix this, what am I missing?