Client-Server Networking Between PHP Client and Java Server - java

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

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.

Related

How to make Android app wait for a message from Python on a local server without internet?

Sorry if this is a silly question, I'm still a beginner in Android and couldn't find an answer to my question. I'm making an Android application that sends an input to a server and executes a Python script on that server to process the given input and generates an output. I was successful in sending data from Android client to Python server using SSH.
I can also use SSH to retrieve the output back to the Android client. However, the Python script takes some time to generate the output, and I can't seem to find a way for the Android client to wait for the Python script to finish generating the output. I was able to do this on internet connection with using Firebase database and have the Python script upload the output into Firebase database and have the Android client listen for changes in database. But I'm looking for a way to do this locally without internet (i.e. Firebase).
So is there a way to make my Android application wait for a message from Python to know it has finished with generating the output so it can retrieve it back using SSH or any other way?
Was a protocol what was send a code what was suppost to say if that 2 files are the same. but i do not remember the name. For u will be more easy to send the storage information's in bits (to the device) and after to start that important transfer. when the device will lost the connection will need to compare the real storage memory with the first information's send it and if has not match to ask the user to reconnect to internet or something like that. Maybe is helping until some one will give u the answer.

Tomcat websockets and separate java process communication

I have a tomcat 7.0.53 server running for the purpose of communicating between java and the website through websockets. I wrote the website script as well as the websocket server java code and they communicate and work correctly. However the information I want passed to the website is from another java program running in the background on a Linux machine. The problem lies however with that when I try to pass messages into a sendmessage type of function on the websocket server java code, it thinks that no sessions are open to send the websocket message to even if I do have the website opening waiting for a message from the separate java process to be passed through the websocket server. I found out this problem arises since I have to make a new instance of the websocket server when passes messages from my separate java process into the websocket server sendmessage function and thus there are no websocket sessions in that instance. I am stumped as to a way around this problem other then to making a socket between my websocket server and the other java process running which will be messy and want to avoid. Does anyone else know anyway else this can be done with communicating a separate java process with a websocket server for tomcat, or is this just impossible to do without making a socket?
I'm not entirely sure I understand your question. Could you share the code you are using to provide more context as to what you are attempting server side?
I believe you should be able to create a websocket client endpoint from the second java program. You can find examples of java websocket clients in the web such as the following:
http://www.hascode.com/2014/11/creating-different-websocket-chat-clients-in-java/
hope this helps

Java BlueCove - Sending information from server to client

I'm writing a bluetooth related application, and I'm using a API called BlueCove if you're familiar with it.
I managed to send some text from the client to a server, however I'm not familiar with the API for sending information from a server to a client so I couldn't send any information back to the client. I want to know how to do that.
Could anyone point me to it? I'm really unfamiliar with the API. Thanks
Turns out to be a really simple thing. Bluetooth provides different ways to communicate between devices, and one of them is using DataStream. Set up the following on both server and client side, and they'll be able to talk to each other:
StreamConnection conn = (StreamConnection)Connector.open(url);
DataOutputStream output= new DataOutputStream(conn.openOutputStream());
DataInputStream input = new DataInputStream(conn.openInputStream());
Whatever is put in DataOutputStream on one end, it'll come out in DataInputStream on the other side of the connection, regardless whether it's a server/client. DataInputStream and DataOutputSream's API are found in the link.

Making a client-server chat - running into issues with connecting client to client

I am trying to learn how to make a multiple-client chatting program. Im following the Oracle tutorial on Custom Networking, but its not really what I am looking for. So far, I have no code of my own to share, all of it is copied from the Oracle tutorial and I think pasting it here would be a copyright infringement(??).
link at: http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html+
client code link: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockClient.java
anyway, I have the server-client working where the server tells knock knock jokes, the client reads and prints, then the user replies and so on. how would i start to edit it to have the client talk to the other clients directly?
im sorry, i have no background with networking at all. if anyone can direct me to a informative source better suited to my goals i would appreciate it.
as in the Knock-Knock example, each client connects to the server, but they not mutually directly connected.
There is a solution to make a forwarding sever :Arrange each client an id, and clients use id to identify their talking partners.
To do this, you have to modify the client to server data format from a plain string to a tuple like (String,Id). And, when the server receive the data, it parses out the id, get the corresponding client socket and then write the data.
The required level of complexity just went up a notch as your going to need some sort of "interprocess communications" infrastructure to allow client to client communication (possibly via sockets marshalled by the server?)

Using PHP to communicate with a desktop application

Well, I've been playing around with php for the last week or so, and I am wondering how I would use it to get data from a Java application. i.e: the php script sends a request to the server and asks for a response. In my particular case I want to do just that: I would like to have a java application waiting, and the php would "ask a question" to detemine if it is on or off (the server would not respond if it is off, and would if it is on - type of thing).
My question is: How do I communicate with a php script through java. How do I make requests to an application through php?
Any ideas?
If your PC is the server, then you can write a Java based server that listens to a socket -> then do something in java program when communication is received. Here's a simple example.
For real life implementation though, I'd suggest you use the PHP/Java Bridge instead. It's much faster and optimized for this sort of operations.
PHP has the ability to make web calls, open and communicate through sockets, SOAP, RPC, etc. It all depends on how your Java program would be listening.
Additionally, PHP can be written as a socket server, so your Java program could talk to it via web calls, socket or any other sort of server technology you choose.

Categories

Resources