|Blocking Read||Graceful Shutdown|
|Server Side Socketing|
To add to the misery, Windows partially ignores the timeout. On connect, the JVM (Java Virtual Machine) tries to resolve the hostname to IP/port. Windows tries a netbios ns query on UDP (User Datagram Protocol) port 137 with a timeout of 1.5 seconds, ignores any ICMP (Internet Control Message Protocol) port unreachable packets and repeats this two more times, adding up to a value of 4.5 seconds. I suggest putting critical hostnames in your HOSTS file to make sure they are resolved quickly. Another possibility is turning off NETBIOS altogether and running pure TCP/IP on your LAN (Local Area Network).
Since TCP/IP sends no packets except when there is traffic, without Socket.setKeepAlive( true ), it has no way of noticing a disconnect until you start trying to send (or to a certain extent receive) traffic again. Java has the Socket.setKeepAlive( true ) method to ask TCP/IP to handle heartbeat probing without any data packets or application programming. Unfortunately, you can’t tell it how frequently to send the heartbeat probes. If the other end does not respond in time, you will get a socket exception on your pending read. Heartbeat packets in both directions let the other end know you are still there. A heartbeat packet is just an ordinary TCP/IP ACK packet without any piggybacking data.
When the applications are idling, your applications could periodically send tiny heartbeat messages to each other. The receiver could just ignore them. However, they force the TCP/IP protocol to check if the other end is still alive. These are not part of the TCP/IP protocol. You would have to build them into your application protocols. They act as are-you-still-alive? messages. I have found Java’s connection continuity testing to be less that 100% reliable. My bullet-proof technique to detect disconnect is to have the server send an application-level heartbeat packet if it has not sent some packet in the last 30 seconds. It has to send some message every 30 seconds, not necessarily a dummy heartbeat packet. The heartbeat packets thus only appear when the server is idling. Otherwise normal traffic acts as the heartbeat. The Applet detects the lack of traffic on disconnect and automatically restarts the connection. The downside is your applications have to be aware of these heartbeats and they have to fit into whatever other protocol you are using, unlike relying on TCP/IP level heartbeats.
However, it is simpler to use the built-in Socket.setKeepAlive( true ) method to ask TCP/IP to handle the heartbeat probing without any data packets or application programming. Each end with nothing to say just periodically sends an empty data packet with its current sequence, acknowledgement and window numbers.
The advantage of application level heartbeats is they let you know the applications at both ends are alive, not just the communications software.
For a server to accept connections from the outside world, first it opens a ServerSocket on a port, but not connected to any client in particular.
ServerSocket serverSocket = new ServerSocket( port );
Then it calls accept, which blocks until a call comes in.
Socket clientSocket = serverSocket.accept();
At that point a new ordinary Socket gets created that is connected to the incoming caller. Usually the server would spin off a Thread, or assign a Thread from a pool to deal with the new Socket and loop back to do another accept.
You can set up your miniature server even if you don’t have a domain name. They can get to you by name: ip:port e.g. 188.8.131.52 :2222. Even if your are behind a firewall, you use the external facing IP (Internet Protocol) of the firewall. You must then configure your firewall to let incoming calls through and to direct them to the correct server on the lan.
There is a mysterioous method Socket.setTcpNoDelay( true ) to disable Nagle’s algorithm. As is typical, there is no explanation what Nagle’s algorinthm is. My TCP/IP text book makes no mention of it. If you are dealing with near real-time data then you may want to look into disabling Nagle’s algorithm. That algorithm attempts to ensure that TCP doesn’t send lots of undersized IP packets by buffering-up submitted data and keeping it for typically for a few milliseconds to see if you are going to give it some more data that could go into the same packet. I am not sure if flush is sufficient to send a packet on its way immediately.
This page is posted
Optional Replicator mirror
|no blog for this page||Canadian
Your face IP:[184.108.40.206]
You are visitor number|