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). 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>
&lt;form method="post" action="app.php"&gt;
&lt;input type="submit" name="submit" value="Go"&gt; 
&lt;/form&gt;
</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

What are you asking for?

First, Java runs CLIENT-SIDE not server side. PHP is not a client it only runs SERVER-SIDE.
So, your terminology doesn’t make sense. Your error shows an error at line 29 of ChatApp.php file.
BUT, your HTML file call App.php not ChatApp.php.
Lastly, your error line in the PHP is
socket_set_block($this->socket)
This is actally
socket_set_block($this->;socket) This means you are using internals of socket_set_block function
But, this is not set as a function in the code you showed up. Perhaps you mean the general socket
function. If so, remove the THIS-> from this call. http://php.net/manual/en/function.socket-set-block.php

Hope that helps…

PS: Oh, just realized you set up a program to act as a chat-server and a PHP-connection to it and that is why you said server and client backwards. Sorry for that, but, use PHP-connection not PHP-client for that.

I changed it to : socket_set_block($socket) after I had a look at that like. I then ran the program (Java server + PHP connection) through BASH. It was successful and there is no error this time. When I reuploaded the PHP script to the website though, it still gives me a “Connection Timed Out” error message. My question is why is it timing out and how can I fix it?

Thanks,
cc11rocks

I also tried it this way too :

<?php

$server = "***.***.***.***";
$port = "****";

$socket = fsockopen($server, $port, $eN, $eS);

if ($socket) {

    fwrite($socket, "Hello world!");
}

?>

PS : Can I edit my posts?

The code above generated the following (would have posted above, but I don’t know how/if I can edit my posts) : Warning: fsockopen() [function.fsockopen]: unable to connect to ***.***.**.: (Connection timed out)

Not sure, but, try these small changes. I added a timeout to the fsockopen to insure there is an actual timeout set. And, I added an error display if the socket connection fails. In this way, you can see the error with more detail. This might shed some light on why it’s timing out…

[php]

<?php $server = "***.***.***.***"; $port = "****"; $socket = fsockopen($server, $port, $eN, $eS, 30); if (!$socket) { echo "$errstr ($errno)
\n"; } else { fwrite($socket, "Hello world!"); } ?>

[/php]

Also, from the PHP site (PHP.NET), found the sample of how to set up a PHP Socket connection which was very close to yours. Maybe there is some minor difference I do not see. Here that one is:

[php]

<?php $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)
\n"; } else { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: www.example.com\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?>

[/php]

I’m getting the exact same error, but on the latter code I’m receiving this additionally : Connection timed out (110). I’m pretty sure you understand, but I am hosting this on a website (000webhost). When I run it on the desktop, it works 100% (and I don’t use the localhost, I use the real IP as in the real world). When I run it on the website, the connection times out. I’m just trying to make this as clear as possible so there is little/no communication breakdown, we don’t waste each others time, and we can get it fixed sooner.

Thanks,
cc11rocks

cc11Rocks,

I may have found it. I searched for “php port socket” on the 000webhost site. I was checking to see if they block ports as a lot of sites do. Most comments I found state they do not block ports. But, I did find a note from someone who sounded knowledgeable. He said that you can NOT use an IP address. You have to use the www (domain name) instead. Further searching shows they do their sites on virtual servers. So the direct root to them is thru the domain name NOT the IP address…

Try your domain name instead of the IP and see if you get the same 110 error.

Good luck…

The Java server is hosted on my computer of course. I would have to register for a domain name, correct? Is there any way I can do a forward (000webhost > Domain > My Computer’s IP)? I do realize this is no longer a PHP or Java program so if you want to close this, go ahead. If I have the powers to close it, I won’t do so unless you tell me to do so or give me an answer. ErnieAlex, I thank you so very much for helping me with this issue, going so far and to find multiple solutions for the program connection problem and doing research on the 000webhost forums and such. You have gone above and beyond the free “programming” (the issue wasn’t my code :stuck_out_tongue: ) support. Also, thank you PHPHelp. I will use this forum for any more PHP issues I encounter. Thanks for having me here.

cc11rocks

Would setting the dnsdomainname (Linux) work?

CC11Rocks,
No problem, thank me AFTER we solve it, LOL… Perhaps I do not understand what you have in place.
You told me you were hosted on 000webhost. In the last message you said you were hosted locally.
Also, you said it works on the desktop but, not on the host. But, if it is hosted locally, then it is basically
on the same system.

So, let’s recap what you have totally. So, far, I heard the following:

Hosted on 000webhost. (This means a website most likely with a www.xyz.com)
Not registered domain. (So, NO www.xyz.com, not a problem as the IP should work.)
Java server. (Still not sure what that means. The link you gave at the bottom of it goes nowhere.)
(Also, it looks like Java code, but, does not look like server code. Explain what this does.)
HTML which is just a form that calls the PHP code. (Why not put the php inside that code?)
And, the PHP code which looks like standard socket connection code.

To solve this, first what does the entire project do? A chat server of some sort? And the main database and server parts are local? Or, are they hosted? Are you hosting it locally and trying to get to it from a web page. Let’s start at the beginning. What does it do, list the parts. Also, PHP can only run on a server, even though you can have a server install on your local machine. Javascript runs on client-side browsers only, not on a server. Java can run on basically anything as a program.

If it is just a chat-server, there are TONS of ways to do that with easy code. Or at least give me the correct link on the java-link that is bad.

Not sure where to send you next. (I am gone tonight and a lot this weekend. Superbowl party, etc.) But, let me know more info…

My domain name AND hosting is on 000webhost. My Java server (to connect with PHP/Java programs) is run on my computer. The PHP program is trying to connect to the Java server hosted on MY computer, hence why I don’t have a domain name. I’ve been programming Java Applets but they are very outdated. This is the main reason why I want to do PHP connection with my Java server. I don’t care what host I use, just that : it’s free, I get a domain/subdomain name, I get a large amount (100MB minimum) of bandwidth, and it supports PHP (I realize this is a large list). 000webhost supports that (except for the PHP direct connect to IP), which is why I use it. The immediate main reason I am learning PHP is because I want to port my Java Applet chat program to PHP (client side only). I’ve been working on it for several months and I feel my users deserve and would appreciate more a web-based client side. The Java server won’t change one bit. Right now, I just want to be able to connect from the PHP program to my Java server. Once I establish the connection between them, I can start porting. This is actually how I started the chat program (initial testing to make sure the Java applet and server could connect). Current setup of program : Java Applet embedded in website on 000webhost (Client) > Java Program on my computer (Server). Testing/Final Setup (PHP > Java) : PHP Program on 000webhost (Acts like Client) > Java Program on my computer (Server). If you have any more questions or do not understand, please feel free to make me clarify. I do understand that their is a communication issue between us on some definitions of “client” and “server”, hence I put “acts like client”. My definition of client : Doesn’t run on my computer, connects to the Java server on my computer, users access the client. My definition of server : Doesn’t run on users’ computers, clients connect to this from the client, users access the server through the client, but not directly. Hope this was detailed enough for you. If you would like to see a live setup/source code of the actual chat program (client + server), I would be more than happy to show you on a private basis. Be warned : the server and client are coded in Java and contain about 2,000 lines of code. The current chat application is very irrelevant, but you asked and will help you understand “my situation”. The client for this chat application can be found here : http://www.chatapp.comuf.com/AppletClient(Public).java .

Thanks,
cc11rocks

cc11Rocks,
Thanks for the extra explanations. I was NOT clear on what you were doing. I may be leaving for the evening, but, will look into it more when I get back. I do know that there are some minor issues with ports and firewalls with socket connections. (Some ports are blocked on some ISP’s and could cause problems.)
Have you trapped the actual socket connection in the PHP code to see what the real error actually is? You do this by using the “TRY” command. You try an operation and if it doesn’t work, the error is given back to you so you can display error numbers and error messages. Here is a link that explains it somewhat. It might help to acquire the actual error you are getting. (Some errors are hidden so well, they do not show until captured this way…)
http://www.w3schools.com/php/php_exception.asp

Hope you weren’t upset I asked for more info. I have used a PHP socket and did not have any issues. But, it was not involved with Java… Try the “try” and see if it gives you more info on the time-out error.
More research for you later on…

I set up my own web server using Apache. It took awhile to set up the PHP stuff, but it seems to be working fine. I also hooked my IP up to a myDomain.thierDomain.com site and it seems to be peachy. Thanks for all your help. I’m not mad at all and I hope you aren’t. I could have just done this from the beginning. Because I have a server set up on this machine, why not also set up the webpage too? I can’t believe I haven’t thought of it before. Thank you so much! By the way, PHP and Java connected and the PHP program is now receiving error code 111 (because the Java server shuts down after it connects). +1 to ErnieAlex!

cc11rocks

Well, get’n a little further… (I didn’t mean you were mad, me neither! Just frustrated an answer was quick to find.)

I was looking around and found this link:


some talk on it about your problem. It doesn’t have an answer as I can see, but, brought up some
items that may be your problem. Also, I played a lot with VB and VB.NET and had a lot of trouble with
sockets if the password was not correct caps-wise. And, the way the server reads passwords could
be an issue. Some read as strings, some encrypted, Not sure how Java handles that.

I will look into the error some more for you…

Sponsor our Newsletter | Privacy Policy | Terms of Service