This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to use setKeepAlive() ?

 
Ranch Hand
Posts: 116
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have written a little, simple Protocol to test the "setKeepAlive()"-method.

So, a little, simple echo-Server and a client.

I want that the Server keeps the connection to the client stable, wether or not the server has send a message to the client.

So, I try to work with the "KeepAlive()-Method" from the java.net-library.

But I am not able to find any examples with keepAlive() or any documentations.

Here is my example-protocol:

Server:



Client:


I hope you can help me.

Thanks in advance
 
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the problem is that you're trying to set KeepAlive after the connection has been established. Try a constructor that doesn't immediately connect to the endpoint (such as the parameterless constructor), call setKeepAlive(), and finally call connect().
 
Ralf Coby
Ranch Hand
Posts: 116
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I believe the problem is that you're trying to set KeepAlive after the connection has been established. Try a constructor that doesn't immediately connect to the endpoint (such as the parameterless constructor), call setKeepAlive(), and finally call connect().



Thanks for your help. I will try it tomorrow and then I will reply.

Greetz
 
Ralf Coby
Ranch Hand
Posts: 116
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have set setKeepAlive() before the connect-method connects to the server.

Surprisingly, on the one hand, a ACK-Flag hasn't been set by invoking the method.

Now, I get the exception below:



So, the client gets the message from the server, which the Client has send.
Then, I think, the client trys to reconnect to the server, but it isn't possible, because the client has closed its InputStreams and OutputStreams.

On the other hand, this exception tells me, that the client trys to get a "keepAlive"-Connection to the server.

So, i have to figure out how to close the Streams in the right moments.

by now, my code looks like this:

Client:





Server:

 
Ralf Coby
Ranch Hand
Posts: 116
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RESOLVED!
This works fiiiinnee!
There is no setKeepAlive(), but actually, this code does what I wanna do!

For anyone, who ist interessted about:

Client:




Server:
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralf Coby wrote:
For anyone, who ist interessted about:



Thanks for posting the final code. Have a cow !!

Ralf Coby wrote:
There is no setKeepAlive(), but actually, this code does what I wanna do!



The purpose of keep alive is *not* to prevent the other side (application) from closing the connection due to inactivity. After all, you are writing both sides -- if you don't want the other side to close the connection, then simply don't close the connection.

In general, the most common purpose is to prevent the connection from being torn down due to inactivity (by the network). This can happen if the connection is going through lots of routes / firewalls. Another purpose is if the network gets a partial disconnect (one side loses the connection, perhaps via a crash, but the other side doesn't detect it). This mechanism helps the network detect it, so that it can close the connection.

Henry
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic