Java TCP Connection

How to create a TCP socket in Java?

How to create such a TCP connection that it terminates only when I say that otherwise it remains open?

How to use keepalives to find out if a server or client is available?

Please, help!

+4
source share
2 answers

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.

+11
source

The java.net.Socket class is what you are looking for.

0
source

Source: https://habr.com/ru/post/1435896/


All Articles