Client-Server Programming
Spring Semester, 2005 Socket Options |
||
---|---|---|
© 2005, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 15-Feb-05 |
CS 580 Client-Server Programming Spring Semester, 2005
Java On-line API http://java.sun.com/j2se/1.4.1/docs/api/
Unix Network Programming, Stevens, 1990, Berkeley Sockets chapter 6.
TCP/IP Illustrated Vol 1, Stevens, 1994, chapter 20.
Internetworking with TCP/IP, BSD Socket Version Vol. 3, Comer, Stevens, Prentice-Hall, 1993
Copyright ©, All rights reserved. 2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 580 Spring 05 | Doc 7, Socket Options Slide # 2 |
CS 580 Spring 05 | Doc 7, Socket Options Slide # 3 |
Socket will time out after specified time of inactivity
Both Socket and ServerSocket class support:
void setSoTimeout(int timeoutInMilliseconds) throws SocketException void getSoTimeout() throws SocketException
Must be sent before performing a read
Read throws SocketTimeoutException when socket times out
Not normally used on ServerSockets
CS 580 Spring 05 | Doc 7, Socket Options Slide # 4 |
Each TCP socket has
Buffers are in the TCP stack space (not the VM)
Buffer size should:
TCP does not allow the sender to overflow the receiver’s buffer
So the receiver’s receive buffer as large as the sender’s send buffer
Buffers larger than 64KB require special set up
CS 580 Spring 05 | Doc 7, Socket Options Slide # 5 |
Depends on platform
Has changed over time
OS |
Receive buffer Size |
Solaris |
32KB |
Mac OS 10 |
32KB+ (33304 bytes) |
CS 580 Spring 05 | Doc 7, Socket Options Slide # 6 |
void setReceiveBufferSize(int size) throws SocketException
int getReceiveBufferSize() throws SocketException
void setSendBufferSize(int size) throws SocketException
int getSendBufferSize() throws SocketException
A Socket object has both a send & receive buffer
A ServerSocket only has a receive buffer
CS 580 Spring 05 | Doc 7, Socket Options Slide # 7 |
In this example the default buffer size will be fine
Setting the buffer size just to show how to do it
import java.net.*; import java.io.*; import java.util.Date; public class ServerWithTimeout extends Thread { static final int CLIENT_TIMEOUT = 3 * 1000; // in milliseconds static final int BUFFER_SIZE = 16 * 1024; ServerSocket acceptor; public static void main(String[] args) throws IOException { int port = Integer.parseInt( args[1]); ServerWithTimeout server = new ServerWithTimeout( port ); server.start(); } public ServerWithTimeout(int port ) throws IOException { acceptor = new ServerSocket(port); acceptor.setReceiveBufferSize( BUFFER_SIZE ); }
CS 580 Spring 05 | Doc 7, Socket Options Slide # 8 |
public void run() { while (true) { try { Socket client = acceptor.accept(); processRequest( client ); } catch (IOException acceptError) { // for a later lecture } } } void processRequest( Socket client) throws IOException { try { client.setReceiveBufferSize( BUFFER_SIZE); client.setSoTimeout( CLIENT_TIMEOUT); processRequest( client.getInputStream(), client.getOutputStream()); } finally { client.close(); } }
CS 580 Spring 05 | Doc 7, Socket Options Slide # 9 |
void processRequest(InputStream in,OutputStream out) throws IOException { BufferedReader parsedInput = null; PrintWriter parsedOutput = null; try { parsedInput = new BufferedReader(new InputStreamReader(in)); parsedOutput = new PrintWriter(out,true); String inputLine = parsedInput.readLine(); if (inputLine.startsWith("date")) { Date now = new Date(); parsedOutput.println(now.toString()); } } catch (SocketTimeoutException clientTooSlow) { parsedOutput.println("Connection timed out"); } } }
CS 580 Spring 05 | Doc 7, Socket Options Slide # 10 |
Delays transmission of new TCP packets while any data remains unacknowledged
Allows TCP to merge data into larger packets before sending
Introduced to avoid lots of small packets across a WAN
Delay is on be default
CS 580 Spring 05 | Doc 7, Socket Options Slide # 11 |
class Socket { void setTcpNoDelay(Boolean noDelay) throws SocketException void getTcpNoDelay() throws SocketException }
Set noDelay to true to turn delay off
CS 580 Spring 05 | Doc 7, Socket Options Slide # 12 |
Determines what happens when a socket is closed
How long does the socket remain after close to
Default is to
CS 580 Spring 05 | Doc 7, Socket Options Slide # 13 |
Send packet on inactive connection to prevent timeouts
At least 2 hour delay between sending keep alive packets
Long delay limits it usefulness
CS 580 Spring 05 | Doc 7, Socket Options Slide # 14 |
Urgent data can be read out of order
Java 1.4
Copyright ©, All rights reserved.
2005 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.