The Java Server is up and running. It is on one of my open ports (set on my router). It is also not blocked by the firewall (it is white listed). This happens if it is on the webpage : The PHP client just times out and does not successfully connect to the server. I see this message after 30 seconds : “Connection timed out”. If I run it in BASH (Linux command line), it works :
cc11rocks@cc11rocks-1005HA ~/Desktop/PHP/ChatApp $ php ChatApp.php
PHP Fatal error: Using $this when not in object context in /home/cc11rocks/Desktop/PHP/ChatApp/ChatApp.php on line 29
cc11rocks@cc11rocks-1005HA ~/Desktop/PHP/ChatApp $ java Server
Got something
It’s at least working on the desktop, so I assume it has something to do with PHP and not Java at all.
HTML File :
[php]
<html>
<head>
</head>
<p>
<form method="post" action="app.php">
<input type="submit" name="submit" value="Go">
</form>
</p>
</body>
</html>
[/php]
PHP File :
[php]
<?
$host = "***.***.**.";
$port = "";
$timeout = 30; //timeout in seconds
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or die("Unable to create socket\n");
socket_set_nonblock($socket)
or die("Unable to set nonblock on socket\n");
$time = time();
while (!@socket_connect($socket, $host, $port))
{
$err = socket_last_error($socket);
if ($err == 115 || $err == 114)
{
if ((time() - $time) >= $timeout)
{
socket_close($socket);
die("Connection timed out.\n");
}
sleep(1);
continue;
}
die(socket_strerror($err) . "\n");
}
socket_set_block($this->socket)
or die("Unable to set block on socket\n");
?>
[/php]
Server.java
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(****);
} catch (IOException e) {
System.err.println("Could not listen on port: ****.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.println("Got something");
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
I have posted a modified version of this at http://www.coderanch.com/forums/jforum?module=posts&action=edit&post_id=2571022&start=0