views:

215

answers:

2

Hi there, I have a university project which is already 99% completed. It consists of two parts-website (PHP) and desktop (Java).

People have their accounts on the website and they wish to query different information regarding their accounts. They send an SMS which is received by desktop application which queries database of website (MySQL) and sends the reply accordingly. This part is working superbly. The problem is that some times website wishes to instruct the desktop application to send a specific SMS to a particular number. Apparently there seems no way other than putting all the load to the DB server... This is how I made it work. Website puts SMS jobs in a specific table. Java application polls this table again and again and if it finds a job, it executes it. Even this part is working correctly but unfortunately it is not acceptable by my university to poll the DB like this. :(

The other approach I could think of is to use client-server one. I tried making Java server and its PHP client. So that whenever an SMS is to be sent, the website opens a socket connection to desktop application and sends two strings (cell # and SMS message). Unfortunately I am unable to do this. I was successfully to make a Java server which works fine when connected by a Java client, similarly my PHP client connects correctly to a PHP server, but when I try to cross them, they start hating each other... PHP shows no error but Java gives StreamCorruptedException when it tries to read header of input stream.

Could someone please tell what I can try to make PHP client and Java server work together? Or if the said purpose can be achieved by another means, how?

Regards, Yasir

+1  A: 

You might try looking into Quercus. It's a server that runs PHP inside java. You can call java called directly from PHP as if it was native PHP functions. You won't have to worry about streams then.

Brent Baisley
Hmm so Quercus is a webserver. It means:1. my project will require to be run on a server which runs Quercus instead of Apache or IIS.2. Both web as well as desktop application require to be run on a single machine.These are two problems that may make it unacceptable again. Please correct me if I am taking it wrong. Also, thanks for your quick reply.
Muhammad Yasir
You mentioned you tried a java server and a php server, Quercus would provide both in one (it's free, like Apache). Both your PHP and Java clients should work fine connecting with a Quercus server. Quercus should solve your problem of "crossing them". Not sure what you mean by #2.
Brent Baisley
May be I was not clear enough in my question or comment. What I want to say is that I have a PHP application (website) and a Desktop application written in Java.Now this package is for non-tech, business type user. He/she would like to run Java desktop app on a pc; andwebsite online, by utilizing hosting provided by any generic web host. It is clear that normally used webservers by hosts are usually Apache or IIS. So if I bind user to use quercus, it will be a drawback and my uni won't accept it. Point 2 means user has to host website and desktop app on a single machine which is not desired.
Muhammad Yasir
When you said you made a "java server", I assumed you weren't restricted to just plain Apache/IIS. Most hosting providers do not support java on the basic tier.
Brent Baisley
+1  A: 

Wait... are you using object streams? According to the java documentation StreamCorruptedException is "Thrown when control information that was read from an object stream violates internal consistency checks." I doubt your PHP app is sending what Java considers a serialized object. Why don't you go low-tech and read a string? The following had worked for me back in the day:

       ServerSocket serverSocket = new ServerSocket(port);
       Socket clientSocket = serverSocket.accept();
       BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

       while((inputLine = in.readLine())!=null)
      {
        //Do whatever
      }
Manos Dilaverakis
Yea, this seems logical. I'll certainly give it a try today and will get back here with the result. Thanks for reply.
Muhammad Yasir
Your doubt was right buddy. PHP application really sends it in the form of a string. So I was able to do my task by using the code you provided :)Thanks a lot!
Muhammad Yasir