java: Send method through socket - java

I'm trying to write a program that would send a function to several nodes, those nodes will receive the function and execute it, replying a result (through serialization). I've searched around a bit, but found nothing of help. An alternative might be to send a .class file over the socket and load it on the node, but i'm interested if anyone has found a simpler way
Edit: I'm trying to make a cluster with this client/server. And I want the client to always run on the nodes and accept any kind of work without having to recompile the client node code
Solution: seems there's no way around not sending a .class file. Have to send it, then load it at runtime

Java is not like some (mostly interpreted) languages where code is data. In order to do what you want with java source code, you would have to send the source code over the wire, compile it to a class file at the other end, and then load the class file. Apart from loading a class file, there is no other way to get new code into a running JVM.
P.S.: Opening up a port on your machine where you accept arbitrary code, and then execute whatever the client sends to you is like owning a Jewelry store, and leaving it un-locked and un-staffed every day when you go out for lunch.
P.P.S.: You could embed an interpreter in your application. E.g., you could embed JRuby, and then the client could send you Ruby code.

Try Hadoop Map/Reduce Framework:
http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html

Related

Write Arduino Serial output to file or program on PC

So I've been looking around for a way to write from an Arduino directly onto a file on the PC, and basically I've found out there's no native way to do so. I wanted to do this in order to then read the file from a C++/Java program, and use the information in it. I also wanted to do this in real-time at some point, so it would be kind of like sending information from the Arduino over to the Java/C++ program for processing.
However, I've seen multiple people state on other forums that you can link the Serial output to some program running on the PC, and then use that program to write the output to a file. However, each time, they neglect to write out how exactly to do this.
The main purpose I wanted to write from the Arduino directly to a file was to read this file from another (Java/C++) program, so the above would be great for me. So how can I get the Serial output into a Java (much more preferably, as I might want to use Swing later) or C++ program, to then use this information in the program itself, or write it to a file? Real-time sending would be a great help.
If the above isn't possible, MATLAB might do, but to be clear, I would much rather be able to interface with Java/C++. Or both Java and MATLAB.
EDIT: To be more specific about what exactly I'd like to do, it is to sort of 'trigger' the Java program to read from the Serial output when a new line has been written (so it reads each line separately) and store it in a string in the Java program, then process it, all at once, and then sleep until another new line is written to the Serial port.
The following Links show you, how you can implement the serial communication in Java and C++.
Java :
http://playground.arduino.cc/interfacing/java
C++ :
http://playground.arduino.cc/Interfacing/CPPWindows
If you want to write the data stream to a local file, you can do that, for instance, with ofstream (C++) or PrintWriter (Java).
Furthermore, there are a several additional libraries for other programming languages such as c# (cmdMessenger).
Just in case anyone was looking for the easiest way to do this, it is hands down to ignore C++ and Java and use MATLAB.
There's a great short tutorial at AllAboutEE that I used and it solved most of my problems. Instead of plotting the data at the end, just use fprintf in MATLAB to output the data to a file.

Protecting .jar with .htaccess?

I want to prevent decompiling for my game client.
Is there a option I can some how protect the .jar file with .htaccess or any other method so the client (Browsing user, that loads the client via a applet) won't be able to get the file & decompile it.
I always wondered if there is a solution for this, is there? Maybe creating a crypt code, and whenever the server tries to get the client jar, it will send a crypt code via GET, if the crypt code matches, it will load the client from that page, I don't know, there should be a solution ?
Thanks!
For a browser to execute an applet, it needs to download the jar file containing the applet code. An if it downloads it, it's available for decompilation. To protect you against that, you can obfuscate the byte-code, or not use an applet at all, and simply use a traditional web application, where the code is at server-side, and the client only see HTML and JavaScript code.
No. You could stop the browser getting the file, but then it would be unable to run it.
Maybe creating a crypt code, and whenever the server tries to get the client jar, it will send a crypt code via GET, if the crypt code matches, it will load the client from that page,
The user could see the code and still get the file.
There is nothing you can do to prevent the user getting hold of the unencrypted bytecodes.
NOTHING.
The user's machine needs the applet's bytecodes in unencrypted form in order to execute them. Since a user (with sufficient knowledge) has totally control over what his machine does, you can assume that control will extend to allowing him / her to intercept the bytecodes at some point where they have been decrypted.
And if the user can intercept the applet's bytecodes, then he / she can change them to defeat any clever stuff you put into the code. And that includes changing the bytecodes to defeat any measure that you use to detect changes to the bytecodes.
"just make it so they have to use it in the browser then check if they are accessing it from our url" Could that work?
Nope. The user can modify the applet to remove that check.
"you could also make php create a session variable with a hash in and check if that hah is present and matches our hashing algorithm"
Nope. The user can modify the applet to remove that check too.
I don't know, there should be a solution ?
Unfortunately, there is no practical solution ... apart from running everything that needs protecting on the server side.
By the way, this problem is not unique to Java. Someone with the technical knowledge and the motivation can defeat any tamper protection measures in Javascript too, or even in native code plugins that you get the user to install (if he is foolish enough).

An application is not working correctly after Java update from 6.21 to 6.31

We have an application which is talking with Flash clients through sockets. The messages are in XML and not too long, up to 1000 chars.
After the update the messages aren't received correctly at the client side - one byte is missing at the end. The first message is correct, the byte is missing from the end of the second message and the Flash stops because of the parse error.
The application is running on Linux in the production, but I was able to replicate the error in my development environment in Windows.
There are two classes used for the communication:
WrappedInputStream
and
WrappedOutputStream
I can modify the Java source, but I'm not able to modify the Flash client.
Has anyone else experienced a similar issue after the Java update? Is there a way how to fix it?
I would write some unit tests where you are using a ByteArrayInputStream and a ByteArrayOutputStream and/or PipedInputStream + PipedOutputStream and try to simulate the problem.
i.e. if this really is a Java problem, you should be able to write a self contained test which fails (based on what you have seen fail with flash)
You could try to make sure that the WrappedOutputStream is being closed properly. Since the purpose of this stream is to send some data without closing the parent stream, there may have been some misunderstanding on the part of the developer, so the stream did not get closed.

SendMessage() is sending multiple messages instead of only one

I'm addapting a IR TV controller to the computer. So far I managed to read the controller data, map the keys and assign a great number of functions using JAVA robot class and prompt commands.
I want now to create play/pause, stop volume+ and volume - functions. Problem is it can't be done diretly through java. I know the right way to do it is by using JNI, but I just don't have the time to learn it right now.
The solution I found is to create exe files containing only the SendMessage function. For example, the code por the Play/Pase function would be:
#include <windows.h>
#define WM_APP_COMMAND 0x319
#define PLAY_PAUSE 0xE0000
int main() {
SendMessage((HWND)(~0), WM_APP_COMMAND, 0, PLAY_PAUSE);
return 0;
}
The program works, but instead of sending only one single message it keeps sending non-stop.
I have to question. The first, of course, is why the code is not working properly. Is there a break comand missing or something?.
Second is what does assigning ~0 (or 0xFFFF) to the windows handler means.
Thanks, i'm open to any kind of solution.
MSDN SendMesage:
If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
Broadcasting with SendMessage synchronously sends to all those windows. How this message is handled is app-dependent.
Yes - this approach is as risky as it sounds. Can you find another way to do what you want without HWND_BROADCAST ?
Rgds,
Martin
Ive come across the same problem trying to write a java webserver to control my media PC running mediaportal using only a web browser on the local network as the client but I'm limited to what is in the java.robot class only.
I think the idea of using an exe called from Java is the right one, maybe look at a program that does all the messages instead of having individual exe files.
I think this might be like what you are looking for:
http://wiki.team-mediaportal.com/index.php?title=1_MEDIAPORTAL_1/17_Extensions/System_%26_Utilities/SendMessage
~0 is HWND_BROADCAST. The message is sent to all top-level windows in the system. This program sends the message exactly once. Possibly the program is executed many times, or message is handled in multiple windows.
Use Spy++ to investigate this. Spy++ is part of Microsoft SDK and Visual Studio.

Advice on which language to use

I'm trying to create a web application which will get input from system.
What this application should do is to listen what happens when some shell scripts are executing and reporting the status trough web.
An example :
I'm copying thousands of records with shell script, and while this is still executing I'd like pass the current status of the script to the web interface. From shell script side I could echo something like "The files are being copied please wait for it to finnish".
Using which programming language from shell side would be the easiest way to pass this to the web interface? I intended to use JSP/JAVA for web.
Is this even the right way to think about this? Every suggestion is welcome
EDIT
Would something like http://nodejs.org/ be useful for this?
I'd use a named pipe (FIFO) instead. You simply write your output to the pipe and let the application read it. I'm not sure if there is any other way to get a more live system than this.
I'd recommend Perl as the back-end.
EDIT:
named pipes are a special type of files on UNIX. The abbreviation FIFO stands for "First In First Out". On LINUX Journal you can find an interesting read about named pipes.
Perl is a very powerful scripting language with many ready-to-use modules which you can find on http://cpan.org. You can find some answers here on SO about how/where to start learning Perl.
The Web part of your application can easily read a file or a database, so you just need to make sure that your shell scripts are outputting something for your Java code to update.
For example, if you run your shell script like this
./myscript.sh > mylog.log
Then in your Java code (note that you should not have logic in your JSP), you can read in the file to determine the status of the update, and output the result to your JSP.
It would be better to read the data in from a database, but that would involve you changing your shell script to output the data to a database.
You could put shell script output to some storage on server. And use AJAX on the web page to poll the server and get updates from that storage to the page.
If you will decide to use JSF for web page creation I can recommend "a4j:poll" component from ajax2jsf library. It is very simple and straightforward. Very easy way to poll server from the web page.
Writing to log file would be the simplest solution. And in programming, simple often means good.
If you really need very fast/realtime system, you should probably make these logs a database.
As for language, use what you like best. They all do support SQLite, including bash.
If I understand your question correctly you want to display a web page on a client machine that reports the status of a long running task on a server machine. If that is the case then you need to focus on something called AJAX IMO.
For what it is worth the simplest and easiest to understand implementation of AJAX that I know of is Apache Wicket.

Categories