Client-Server Networking Between PHP Client and Java Server
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, b开发者_如何学编程ut 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
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
}
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.
精彩评论