Reputation: 93
I am writing two java programs that will communicate some information between them. I am running both of them on the same host.
I need to establish a pinging mechanism between them to identify if both of them are alive at any given point of time. I am guessing Java Socket programming is the best way to do this. But confused on how to differentiate between the programs when they run on the same localhost IP address (127.0.0.1).
Upvotes: 0
Views: 1232
Reputation: 513
You could create two sockets on two differents ports :
Ping B to A :
Application A :
ServerSocket socketA = new ServerSocket(socketPortA);
Socket socketB=socketA.accept(); //awaiting other application to "come"
byte ping=socketB.getInputStream().read();
Application B :
Socket toA=new Socket("127.0.0.1",socketPortA); //connect to A
toA.getOutputStream().write(1);
Upvotes: 2