7

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!

0

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your message of length zero would never get sent unless it has some application protocol around it. I assume that's what you mean?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.