Java Socket Programming in Eclipse - java

I was trying to create a socket program in JAVA using eclipse. I have the server and client code ready but I am not understanding how to run the code in eclipse for the server and client in the same project. Should I
1] have main method in both the server and the client and start running them individually, or
2] Should I use threads to run each of them separately, or
3] If I were to add another client in the network then how do I run that as well.
Please do help me and let me know which method should I adopt.
Thanks for your help.

You can do either #1 or #2. Eclipse has no issue with running/debugging multiple Java processes concurrently, nor is there anything invalid about having a single Java process that starts up both the server and client on separate threads.
However, I would suggest that in the real world it is most likely that you will not always be starting the server and client processes on the same machine, at the same time. So I think option #1 makes the most sense. You server and client apps should be able to run independently of each other, whether you're inside of Eclipse or not.
So if you do #1, then to add another client to the network you simply spin up another client process, the same way you did with the first client (Right click on the class -> Run As -> Java Application). You can start as many as you like that way. Though if you want to start up a bunch of them (like, say, for load testing), then consider creating another class with its own main() method that just spins up a bunch of clients on separate threads.

I think it would be better if you put individual main methods in the client and the server. This helps with the debugging and would help you determine which client is currently running if each client is run separately.

You can probably have a server java class file with main method and this is started by the eclipse. You can also have a client java class which have multi threaded to start a few clients. To identify the interaction between the server and the client, you can enable debug perspective in eclipse. This link should be able to give you idea on how you can start coding.

Related

Auto-run on Windows startup

I have a client-server application in Java and it is basically a remote control software. I need client to run in Windows startup, I searched and found some ways to do that.
1- Adding shortcut or file to Startup folder
I can't do that since there is a possibility to user can delete it from folder.
2- Writing it to registry
That's exactly what I want but, I don't want to use 3rd part applications or sources. I couldn't find a way to do that on my own.
3- Wrapping it as a windows service
That's also okey for me, however, there will be multiple client's so i need to do that process each time. Thus I can't use it.
If you can help me, give me an idea or indicate my mistakes on topic, I will appreciate.
You could use a Task Scheduler. This would allow you to run it on boot as well as other types of triggers.
https://www.howtogeek.com/138159/how-to-enable-programs-and-custom-scripts-to-run-at-boot/

Launch two Java programs from the same code

I have a Java program which is divided into client and server code. However, for standalone users, I want both the server software and client software to be launch with the click of one button. I have had success embedding the http server with the server software. What I need now is a way to launch the two programs (as two instances) from a single place which can send information to the programs that it has spawned. How can I achieve this?
P.S. I have used JavaFX for the client program so it is better if I have a way of launching JavaFX programs as well.
Can always make a Java program that launches two Process obecjts after building the environment (classpath, correct java home etc taking in to account environment and OS). There should be reusable code in java based installers but you would have to comb thru them for what you need.
A simple program could just expect certain sub folders to have the server and client programs (along with resources like images etc), build up 2 process objects (makes sense to start the server first, maybe first check if the port the server is to listen on is free, if not, is another instance of server already running etc)

How can my Android app communicate with a native daemon?

So, I have a custom Android application running as System (built as a system app in my ROM). And Ive written a native daemon that works fine too and runs as root. However, I was wondering how the two can communicate? I know I can have the native daemon create a network socket and have the App connect to it, but using a local socket for IPC seems even better, but I am struggling to find example code for it.
I figured I could create the socket like as described here but I have no idea how to communicate with it in Java.
Any help would be much appreciated.
Also, at first I wrote a native library and used JNI to communicate with it from Java, but quickly realized that it obviously runs in the context of the app, so it cant be run as root, which is a requirement I have, and something the native daemon can do fine. Is using sockets the best alternative here or would Binder work too?
Thanks a ton!
The solution here was to look at AOSP and see how installd communicates with the Installer java app. In init.rc you see how the named local socket is created and in installd.c you see how native code communicates with the socket. In Installer.java you see how its done from Java land.
Dont think Binder would be the right way to go for my particular use case.

Java application to execute shell files from different servers

I would like to know if the following is possible:
I have to create a Java application that runs .sh files from different servers, I have my class to execute shells, with Runtime and Process, it runs .sh files from my computer, the thing now is that I would like to know if instead of my location be
process = runtime.exec("/home/user/Documents/example.sh");
could be:
process = runtime.exec("180.150.2.***/server/user/Documents/serverExample.sh");
and the thing is, that to get the .sh files from server, I have to login, this application could be a desktop application or a web application, but has to be in Java, so, how could I do this?
I appreciate your help.
Chema.
Basically, I don't think you can do that, the way you are trying. The Runtime.exec(...) will delegate to the OS to perform the actual execution.
There are any number of ways to achieve what you want, either purely in Java or via additional utilities based on the OS.
You could SSH or telnet to the remote machine and execute the commands via those interfaces.
You could write a client server app, where the server would allow you to send commands to it to be executed on your behalf (but you must understand that this is a massive security risk).
Check out Jsch or Ganymed SSH. I have used the latter to perform ssh/scp tasks programmatically.

Java GUI + Server Issue

So far, I've been searching far and wide on the internet for the best practice when it comes to writing a Java server with a GUI (for stopping and starting it). I know that I have to run the server in a separate thread as the GUI but I was also thinking that it my be a little less complicated if I made each piece, the server and the GUI, in its own Java program or process. If I go with the latter, how do I interact with the server from the GUI if they aren't the same application. Would I just kill the process from Java. I really need some help (or maybe a tutorial).
P.S. I'm going to write the client for other PC's and android phones in the house so I could create a simple messaging system in the network for easy copy and paste between devices so I was considering building the GUI and server in with the client so I could distribute that and it would be two way but I'm not sure if thats the right approach or not.
Thanks!
You sir, are looking for an Application Server, like JBoss. Interact with it using web pages as the GUI. Use HTTP as your messaging protocol (POST and GET). Use Eclipse for Java EE
and read some tutorial / guide. It might be some to learn now, but you'll benefit from it later.
For your requested functionality, create a web application with a servlet for receieving HTTP requests first.
I don't think there is a 'right' way to do this. If as you say, you want to have more than one client (an app running on PCs and an app running on android phones), then you are better off going for a client-server architecture, where your server and your client are two (or more in the case of the different GUIs you want to create) different programs.
The way they can communicate is also open for you to choose. You can go low level and connect through sockets, or you could use HTTP and create web clients.
If you give us a bit more context about what you want to create (server and client are way too generic words) then we could give more tailored questions.

Categories

Resources