Problems with PHP connecting to a Java program using a Port - java

Hoping someone can shed some light on this.
I have a PHP program which opens a port and sends some text to a java program listening on that port. Basically, there could be many instances of this PHP program connecting over this port to this one java program. The java program contacts an API, retrieves the answer, and then sends the information back over the port to the PHP program.
This solution seems to work but sometimes I get a empty response in my PHP program. The java program runs run successfully but the answer string is not transmitted back through the port to the php program.
Does this solution sound valid? Will I ever have an instance where the java program will send back the wrong information to the PHP program? Can anyone see any problems here? Thanks a bunch!

"Opens a port" is ambiguous, I'm going to assume you mean "opens a TCP connection".
There's no reason that the scheme you outlined cannot work, but you haven't provided enough information to narrow down the cause of the problem you're having.
When your PHP script gets an empty response from your Java code, it could be because the connection was broken, but it's more likely that the code on one end or the other has a bug. Make sure you're testing all the result values for errors.
If you need help finding the problem, you will need to post more specifics, ideally the smallest example of real code (for both ends of the connection) you can get to exhibit the problem.

Does the Java program close or at least flush the socket's stream after writing?
Consult the basic Sun tutorial "All About Sockets" for details and proper code examples.

Related

Process of uploading to a server

There doesn't seem to be a comprehensive explanation of the exact process being undertaken for uploading files from an android device to a server. So far I have pieced together some information on the methods for preparing the data however the "plugging in" if you will is somewhat more confounding.
What is it that is absolutely necessary for the transaction to take place? What information is required on the app side and what is the server looking for? How does the server get the data without first being preprogrammed to receive it?
I feel that simply setting up a ftp server is not enough so I'm wondering what else is involved. Any help for a novice would be gladly appreciated, thank you.

How would I go about Transfering data between JAVA and PHP?

I have a java application that has information that I need to display on a webpage, sorta like a chat room, The way I'm currently doing it is by having the java application listen for connections on a specific port and then I have a PHP script that connects every second to update the information listed on the page, What I'm trying to find out is there a better way of doing this? I haven't done anything like this before so sorry If this method is really stupid, I'm just looking for the best way to accomplish this. All constructive comments are welcome.
EDIT:
I forgot to mention that I'm doing this over TCP and I do not have access to MySQL so I can't store anything In a database then have the PHP script go and retrieve it..
Well it depends strongly on the flow, when should the communicate? If PHP needs data from Java then just do a call and parse the data back in xml's. Else if it is a lot like a chat, there should be a way to socket it, although I have never tried it.

Creating an inputstream for use with JSCH API. (Java)

Why am I trying to do this?
Right now I'm trying to make a multi tabbed SSH client for use with a few servers. I have 8 at the moment, soon to be 9. As you can imagine, there are a few redundant tasks one has to do when working with Linux. Connecting to each server to make changes one at a time is a terribly tedious process. That's why I'm trying to make an SSH client that can connect to multiple servers at the same time so I can send a command ONCE to have it affect all servers I own.
How far am I right now?
I have a nice UI set up that can connect, log-in, and receive data from the servers. For input, the API requires I specify an inputstream. If I specify System.in as my inputstream, I can then run the program, and have whatever I type into console be broadcast out to the different servers, via the API.
The problem
is that no end user will ever want to work with a separate console to use this program. It will look dinky. So I need some way to take input from the text field to send it through a specified inputstream. That means I'll need an inputstream that never closes unless the program closes. Like System.in. Also, I can't easily redefine the stream once I set it. I searched for an answer yesterday for around 10 hours. Couldn't find anything. If anyone can help, please do. Thank you.
I need
an inputstream that works exactly like an outputstream. It stays open even when nothing is being sent through it, but as soon as it gets data, the data is sent automatically to anything that is using it. This API is very strange, but this last inputstream part is the only thing keeping me from finishing up my program. Thank you for your time.
JSCH sudo su command "tty" error
I was using the API incorrectly. Stupid, yes. I don't want anyone else making the same mistake though. I guess I was following a bad example found somewhere else on the internet.
Essentially, you don't even need to set the input stream. You just need to use the output stream that already exists. Write directly to the output stream. Pretty sure I was trying to do this at 3am last night. It was right in front of me the whole time.

parsing commands sent through email

I am really unsure as to where I should be posting this, but if this is the wrong place could someone direct me where to I could get an answer? I want to be able to send commands to my email address, and have the commands parsed and executed when the message is sent. I.e. I send an email and it contains this:
public class sentThruEmail {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I would want to configure my program to recognize when a new email comes from a sent address, to open it, compile it (in this case compile the java) and then execute it on the machine that the program is running on.
How can I go about figuring out how to do this? Any help would be wonderful, thanks!
EDIT: Or maybe the first step would be how to recognize an email was received from an address at all? In Java, how could I go about that: recognizing that an email was received and outputting something to the screen to alert the fact.
This would be a very bad idea. Blindly accepting email from an untrusted source, compiling it, and executing it is an enormous security hole.
There are several parts of this to consider:
Security
As others mention, there are security risks to consider, here. If that worries you (and it should!), you may want to consider some of the following:
Digitally signing these "command" emails, and verifying that signature before looking at the email
PGP is a popular choice for this
Running your program (which reads the emails) in a "sandbox" environment, such as being chroot'ed or in a jail
Only run a very limited set of commands - perhaps just ones you invent.
Getting Mail
If you still want to do this, given the security issues, you will need your program to read mail.
You probably want to use IMAP for this, or POP.
Parsing
Once you have the mail, you need to parse the contents.
You could just compile it directly if you are only sending code.
You could also send the code as an attachment with a certain MIME type to identify it. That way you could still send a 'normal' email (perhaps with commentary about what this code is for), but your program would be able to cleanly separate out the code.
Responding
How will you communicate results back? Or do you care?
You may want to send a reply email (use SMTP), or update a webpage. A webpage is nice since if you are running the web server locally, you can just write a file directly.
Examples
The standard "confirmation email" system has a lot of similarity to what you describe. Someone sends an email to an automated system, it reads it, does some processing, and replies. Search around for those systems and I'm sure you'll get started.
I created the Exquisite Corpse Emailer project, which does much of what you describe (but in Perl). It only accepts a very small set of limited commands, but it listens to an email address on IMAP, parses the text, updates a database as a result, etc.
But if you want to do it, look into sockets programming, as you would need to connect to your email provider (if he allows terminal login)

Remotely calling Java from php

I have a java program that runs on a couple different computers and I want to be able to be able to access them remotely. I don't need to do anything crazy, just some very basic input output (get and set type stuff). I could write a client type application and set each program to a server with RMI and do it that way but I will not always be on the same computer and do not want to have to carry the .java file around with me.
I'd prefer to write a simple php webpage that I can access from anywhere and have some very basic buttons that could send the commands to the different Applications and receive back some output.
Problems is that it would probably impossible to get my host to load anything other then php onto the server where I'd host my webpage.
Does anyone have any ideas? I've tried googling it and found a couple things out there that sort of sound like what I need but the more I look into them they seem to not be what I wanted after all.
If you really want to do this right I'd suggest looking at a Java web framework (I use Play! myself) to create a RESTful web service and then sending requests from your PHP code that will fetch JSON data.
A framework like Play will make it very easy to get a REST web service running. Just create some wrappers that invoke your existing java code and call renderJSON to output the result.
From the PHP code you'll issue the request using curl and then read the response using json_decode which will turn it into a handy php variable.
This also has the advantage that either end can be changed without the other noticing, as long as the json data format is kept the same.
Based on the way you described your setup you'll probably run into routing issues trying to reach your Java apps from your PHP host. I'd recommend you try something else: have your Java apps frequently check with your PHP app if there are any updates. You can simply use a Java URL connection and a timer:
http://download.oracle.com/javase/6/docs/api/java/util/Timer.html
http://download.oracle.com/javase/6/docs/api/java/net/URL.html

Categories

Resources