How can two instances of an application communicate in Java? - java

I am developing a new Java Desktop app. Something like a media player. I want to load most of the resources in the background when the computer starts up. But the users can turn this option off form within the app or using some other utility. So, what I want to do is if a ban instance of the app is already running and the user starts the app again then I can communicate with the already running instance so that it can launch a new window?

The most known way to do that is to open a ServerSocket when first application starts on a well known port.
If ServerSocket fails to load, it's probably because an instance is already running.
In such a case, you can open a Socket and start to communicate your orders between both instances.
But you can also use far more sophisticated solutions, like Jini or JGroups.

Write the app so it has a server part
When it starts up, try to communicate to the server (if it is already running), and if that works, then the server should open a new window, and the client should die
This should give you an overview:
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

You could use ports.

Related

Streaming a python game through a jsp

So im creating this game using pygame, and i have to put it up on a website, but it has to run server-side since the game is gonna be using a database locally, and the client will be able to enter a web page and click on a button and the game is gonna run, i cant make the game run completely client side because then the game wont be able to connect to my local database
Any ideas?
The way I see it you have two options:
If network connection is intermittent (not so frequent), you can use javascript (AJAX specifically) to make HTTP calls to the server whenever you need to access your database.
If you are expecting frequent (continuous) requests (i.e. multiplayer games), you would need to keep a connection alive using either
Persistent HTTP request
TCP Socket
Websocket : You probably want to use this if you want cross-browser support.
Let me know if you have any other questions regarding the options above.

Java service with GUI tool

I have an application with GUI. GUI is used for settings management only. Now I need to turn this into Windows service. I thought of splitting my app into 2: service itself and GUI-tool for providing settings. Main problem is that I'm not sure, how this tools should "communicate". Settings are stored mostly in files, and after new settings applied, service should restart.
As for now, I thought of admin-tool sending few requests to service over TCP/IP, that also allows to control service from the network. Problem is that I need to hardcode, or use some text file, to set default port on which service would listen for admin-tool connections after it's installation.
May be there is any alternative solution, which is more suitable here?
You are creating a service. If all you need in the communication layer is being able to stop and start the services, then you don't need to open a port and start listening. The system gives you means to do that with commands that you can run. you are talking about windows, so for example you can run the command "sc start MyServiceName" to start service "MyServiceName". there is also a command called "net" which allows you to start and stop services. These OS commands can be then called from java code in various ways that are available to execute external code.
here is a link that shows how to do that with sc command, check the accepted answer: start-windows-service-from-java
here is another link that shows difference between two commands "sc" and "net": net-start-service-and-sc-start-what-is-the-difference
Note that "sc" supports starting services on remote machines: simplest-way-to-restart-service-on-a-remote-computer

How to create voice chat in web browser with java

I want to create a voice chat which runs on the web browser.
The basic idea is that when I run the server.jar file, it will listen to a socket for connection, and when I type the ip and port on another computer on web browser(ex. 1.1.1.1:8082), the server will accept the connection and display an voice chat applet. Server.jar should contain all voice handlers. For example, if we have three computers, one with the server running while other two has web browser applet running and connected to the server, user1 talking will transmit over to the server, which then transmit over to user2.
So far, I have the applet and the server, but I am having trouble using the web browser to open the applet. It seems I need to use servlet and apache tomcat for the server side to make this work.
Can anyone confirm that I need to use servlet and apache tomcat to achieve this? Or can anyone suggest a better way to approach this project?
if it's peer-to-peer, everything can be written inside the applet.
Oh and as far as i know, there is no java voip libraries so you're going to have to port one from a C/C++ library or write it yourself.

Hardware support from a web application

I have a web application running with support for some specific pieces of hardware. This is achieved in the following steps:
User runs a small installer that places java files (and a couple
others) on the client machine. The main piece is a jar called "hardwareManager"
User visits web app. The web app runs a java applet which, due to
a .java.policy file placed during the install, has permission to
interact with the client machine outside the browser sandbox.
The applet checks to make sure the hardwareManager is running,
and if not runs a command to start it.
User interacts with the web app which sends commands to the applet via
javascript. The applet then writes commands to a text file
on the client machine. The text file is constantly monitored by the
hardwareManager which runs any commands it reads in.
This works, but seems clunky. I have a couple ideas on how to improve it, but I don't know which, if any, are even worth trying.
Would it be better to set up the hardwareManager as a socketServer and have the applet connect directly to it, rather than going through text files? Is that even possible?
Is there a way to eliminate the applet altogether and have the javascript talk directly to the hardwareManager? Maybe by writing the hardwareManager to be a local http server? What port should it run on? Do javascript xss limitations fit in here somewhere?
It would be less clunky to start the Java application using Java Web Start. This would remove the need to daemonize or install the Java hardware manager.
Another alternative is to use a built-in browser inside Java. I supose this is not an option, since you depend heavily on Javascript (I suppose to provide a rich client experience).
If you already have to install something on the client machine, why did you make the choice to go with a web application?
Talking from experience: We had a Java EE application which needed to print to PoS printers at the client site. We installed a small "synchronizer" application that connects through SSH and synchronizes all clients files. Afterwards, it loads the JAR and executes the program. This program connects through RMI with the server and subscribes to a JMS queue to receive the print assignments.
Applied to your case: Why not let your Java application connect to the server directly? You can use HTTP, SOAP or even JMS over RMI. You can then launch the hardware command from the server (instead of from the limited JavaScript webbrowser environment). This way, you get tons of features: authentication, buffering of commands, and you can even share hardware between multiple clients.
Schematic:
<----AJAX------> Web browser
ApplicationServer
<---HTTP/SOAP--> Java hardware manager application
You can launch the Java application using Java Web Start, which allows you to update the application automatically (instead of needing to pass every client a new installer).

How to "control" a call in android?

im developing an app where one should be able to call and "reject" a call from the desktop using a small socket program to communicate with the phone over USB.
I got most of it in place, I can call a number from my desktop application, however, when a call is being made it is not under control of the application.
I run a service which starts a server socket thread, and then I bind the local listen port to my computer with adb forward tcp. When I send a CALL:123123 it will start a new call intent that calls the number "123123".
How would I go about making a call and then at some point ending it again?
I'm almost sure this is not possible to do programatically. Android doesn't let programs make manipulate phone calls without some user interaction due to security concerns.

Categories