I know Java can act as a client for reading/writing named pipes, but I need another program which acts as a server.
In this case the program I am communicating with must act as the client, rather than the server. Is it possible for Java to act in server mode for named pipes?
EDIT: In named pipes (Windows) there are client and server modes. A server must first be established before a client can connect to it. I have a legacy application which acts as a 'client', this means that it connects to what it assumes is an already established named pipe.
I have a new java application which I would like to have communicate with this legacy app using named pipes. I have only found examples of how to use Java named pipes in connection to previously established named pipes.
Well on linux and mac you can always have java emit to the console one line at a time. Example:
In one terminal window to this:
mkfifo myPipe
java -jar mydataserver.jar > mkfifo
In a second terminal window do this:
while read line; do echo "What has been passed through the pipe is \
${line}"; done<myPipe
Yes, you can create named pipe on the Java server using JNA library https://github.com/java-native-access/jna
It is clearly shown in the following test: https://github.com/java-native-access/jna/blob/master/contrib/platform/test/com/sun/jna/platform/win32/Kernel32NamedPipeTest.java
API of JNA wrapper is the same as Win32 hence you will be able to use all the features and power of named pipes on Windows.
Related
I'm trying to make a fuse file system on top of an Mysql database, and use this from a windows client and this client request is passed on to Java Server on linux side over SAMBA!!
I need to know how to setup this kind of implementation
Working:
Step 1 : Client on windows side will Login.
Step 2 : After login user gives an 'ls' command.
Step 3 : This command should be proccessed as a C function
Step 4 : C function in turn issues the command as an argument to Java server using JSONCpp or Thrift
Step 5 : This Function calls on to Java server running on Linux.
Step 6 : Java Server interacts with the Mysql database and fetches the result
Step 7 : This result is should be displayed on client terminal on windows.
Write a fuse filesystem that has the callbacks you want to handle.
Forward the fuse callbacks over a local socket to which your Java code listens.
Java server runs the query and returns the result through the socket (in 2).
Fuse system return the information it reads from the socket (in 2)
Share your fuse system over samba.
I don't exactly understand which language functions do you want to call from which language.
But there are two primary ways of cross-language communications.
You can simply use Sockets. Create a socket connection in language A's program that listens on a particular port. In another language B, pass in arguments to a function in A's program, which is listening for an input. And it will execute the function and return an output to B's program. You can easily find socket examples for JAVA and C online.
Communicate by extending languages. This is more low-level than socket. It is basically calling function/library in another language A from a different language B. For example you can extend Python to C like this. And you can call C library functions from JAVA using Java Native Interface.
Depending on your use and comfort levels you can use any of above methods for cross-language service implementations.
For the windows side, you can start with DokanMirror. Implements most call backs and is a good enough framework to start with. Linux side will mostly be your own code, so as the Socket interface.
Using TCP/IP sockets is an overkill and be careful about thread hangs and timeouts.
a. Make the login a userspace application that will directly connect to your linux side and authenticate.
b. This userspace application will then install and mount a virtual drive (using dokan).
c. In the dokan-callbacks (in userspace) you can use tcp/ip to connect to you linux box.
What do you mean by over Samba? If you intend to share your filesystem, then perreal's answer (point 5) is correct.
I am trying to make a terminal emulator in Java. The java program will accept the commands from user, and show its output to them. I can emulate simple commands like 'ls', but I don't know how to handle commands like 'cd'. This is because, I am using exec() method for executing terminal commands. So, all the commands are executed at current directory. The commands like 'cd ..' are executed, but then they have no persistent effect, because each command is separately executed by exec().
Any Ideas How I can emulate a whole session??
If you are executing commands with exec(), you are not writing a terminal emulator; you are writing a shell. In that case, you will need to keep track of things the shell keeps track of, like environment variables and working directory.
If you really want to write a terminal emulator, you would be talking to a shell process through a pseudo-terminal. Then your program would just be keeping track of the things a terminal keeps track of, like the line state and what appears on the screen.
Working with a pseudo-terminal from Java will be a little tricky, because most of the documentation assumes you are using a C api. man pty should get you started. Your Java process will have to open the master side of the pseudo-terminal with FileStream objects. I'm not sure there is a way within Java to get a child process to open the slave side of the pseudo-terminal; you might have to invoke a shell command with exec() that starts another shell command with standard input/output/error redirected to the slave side of the pseudo terminal.
JSch is a pure Java implementation of SSH2.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
http://www.jcraft.com/jsch/
You should really give a try to Ganymed.
Ganymed SSH-2 for Java is a library which implements the SSH-2
protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to
connect to SSH servers from within Java programs. It supports SSH
sessions (remote command execution and shell access), local and remote
port forwarding, local stream forwarding, X11 forwarding, SCP and
SFTP.
http://www.ganymed.ethz.ch/ssh2/
Ganymed along with apache FTP client you can also download and upload files.
Also there is a inbuilt example code for terminal emulation in Ganymed.
The following is a link to a project which is did using Ganymed along with apache FTP client.
GITHUB
Happy Coding!!
Is it possible for a JAVA application to get the file system directory structure of another host on the same LAN?
For example:
Machines A and B are on the same LAN. A is the localhost and B is the remote host. Is it possible for a JAVA application executing on A to get the directory structure (essentially a 'ls -R') of B?
One way to do this is by issuing the ls -R command over the ssh protocol.
For ssh library recommendations, see SSH library for Java
Yes, although you'd have to use something like SSH to open a remote terminal session to execute the ls command. There's no way to natively do that with just Java without opening a socket and machine B somehow cooperating with the request. (That would be a huge security hole if you could...)
I have two secured linux servers. In one machine my Java application is running. I need to run Linux commands on second machine from first machine in Java. How might I do this?
Jsch (here) allows you to connect to a remote server using SSH and executes shell commands easily (and lot of other things like SCP, SFTP...). There is not a lot of documentation, but you have a few really helpful implementation examples here (and even an example of what you want to do here).
You can also combine Jsch with Expect4j and this way have a better control on the commands you want to execute (nice example here).
Essentially, you need to open an ssh connection to the other server from within your Java application. The OpenSSH site has some useful information on libraries that will give you ssh support in Java.
It looks like Ganymed SSH-2 for Java is the nicest of the pick there, but I haven't used any of them so you will need to look at what you need.
Once you have an ssh connection, you will be able to run commands just as if you logged in using any other ssh client.
You can do it a number of ways; however, nearly every way involves a network connection.
You could write a client-server pair of Java programs, with the client connection to a server and submitting the command.
You could write your Java to use an existing server, like sshd, telnetd, rsh, ftpd, or a pre-existing other server which allows commands at the remote end.
You could leverage an architecture which handles certain aspects of establishing a client-server pair, like RMI, SOAP, CORBA, etc.
In the end Java supports tons of networking options, so you have more ways of doing this than you think. Just make sure you don't do it in a web browser, as those JVMs are launched sandboxed, and you can't get out of the sandbox without some assistance.
It might be easier to check out Sockets, as you can do what you're trying to do without having to get any external libraries set up.
On the host machine, you want to set up a ServerSocket object, and from the client machine you open a Socket. I don't have time to type up a whole example, but check this out for a simple way to set up a server-host connection over the Internet in Java.
http://zerioh.tripod.com/ressources/sockets.html
Once you get that set up, you want to input your shell command from the ServerSocket on the computer that should execute the command, and do something around the lines of
String command = "get this from the ObjectInputStream attached to your ServerSocket";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(command) ;
pr.waitFor() ;
BufferedReader buffer = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ;
String line;
while ( ( line = buffer.readLine() ) != null )
{
System.out.println(line);
}
The tricky part is setting up a realiable host-client connection with the Sockets, but if you're doing something simple you should be fine with the example from the link above.
Suppose I launch a Java application:
java -cp whatever.jar com.example.Start
Process launches ok and keeps running with PID 1314.
Now I would like the system to fire a method by users request.
How can I use bash to signal the running PID and have it fire a method?
My thought is to have bash echo data to the Java processes via a named pipe, which I'm pretty sure Java has support for.
To communicate with a Java process, you would normally use RMI from another process (this could be in the same JAR)
However, if you want a pure bash/unix utilities solution, you could have the application listen on a port for commands and send back responses. This means you could use plain telnet to send commands and get output. One example of this is to use a http server with wget or you could have a simple socket based solution.