PHP Client, Java Server

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). The PHP client just times out and does not successfully connect to the server. I see this message after 30 seconds : “Connection timed out”. I have just started PHP programming. I need to get this working so I can start rebuilding my Java clients in PHP.

[php]

<p>
<form method="post" action="ChatApp.php">
<input type="submit" name="submit" value="Go"> 
</form>
</p>
[/php] [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]

Java Server :

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();
    }
}

Please note that I have also posted a different version of this at coderanch under the name “John Price” (PHPHelp will not let me post a link)

Thank you,
cc11rocks

Could you moderators please delete this one? This was an accidental repost.

Sponsor our Newsletter | Privacy Policy | Terms of Service