Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : S words : socket.
If you write to a Socket, you usually need to call flush to force the data out onto the net. If you fail to do that, you could wait forever for a response because your complete query was never sent. You don't need flush if you are sending a steady stream of data that will push earlier data out onto the net.
If you read from a Socket, you can hang waiting forever if you use a blocking read. Socket.setSoTimeout controls the timeout. The read will eventually die when the Socket connection fails. This will happen when:
Java has the Socket.setKeepAlive( true ) method to ask TCP/IP to handle the heartbeat probing without any data packets or application programming. Unfortunately, you can't tell it how frequently to send these. If the other end does not respond in time, you will get a socket exception on your pending read. To handle this properly, you need to send your own heartbeat packets in both directions to let the other end know you are still there. You only need to sent them when there has been no traffic in X seconds.
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. 24.87.56.253:2222. Even if your are behind a firewall, you use the external facing IP 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.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 8129. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/socket.html | J:\mindprod\jgloss\socket.html | |||