user1673627
user1673627

Reputation: 271

Java TCP Connection

How do I create a TCP socket in Java?

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

How do I use keepalives to know whether the server or client is still available?

Please Help!

Upvotes: 7

Views: 19161

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533432

How do I create a TCP socket in Java?

Socket socket = new Socket(hostname, port);

http://docs.oracle.com/javase/tutorial/networking/sockets/index.html

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

They will remain open until either you or the other end closes it.

How do I use keepalives to know whether the server or client is still available?

That is up to you. You can send different messages, one of which is a heartbeat which tells the other end you are alive.

A common thing to do if you are sending binary messages is to send the length as an unsigned short or int value. I use a message of "length" 0 as a heartbeat.

Upvotes: 14

Philipp
Philipp

Reputation: 69663

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

Upvotes: 0

Related Questions