How to create a TCP socket in Java?
Socket socket = new Socket(hostname, port);
http://docs.oracle.com/javase/tutorial/networking/sockets/index.html
How to create such a TCP connection that it terminates only when I say that otherwise it remains open?
They will remain open until you or another person closes it.
How to use keepalives to find out if a server or client is available?
It depends on you. You can send different messages, one of which is a heartbeat, which says that the other end you are alive.
A common task when sending binary messages is to send the length as an unsigned short or int value. I use the message "length" 0 as a heartbeat.
source share