SDSU CS 580 Client-Server Programming
Fall Semester, 2002
Socket Options
Previous    Lecture Notes Index    Next    
© 2002, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 15-Oct-02

Contents of Doc 15, Socket Options



References

Java On-line API http://java.sun.com/j2se/1.4.1/docs/api/

VisualWorks Internet Client Developer's Guide, Socket Programming Chapter 2, docs/NetClientDevGuide.pdf in VW 7 distribution


Unix Network Programming, Stevens, 1990, Berkeley Sockets chapter 6.

TCP/IP Illustrated Vol 1, Stevens, 1994, chapter 20.


Doc 15, Socket Options Slide # 2

Socket Options




Doc 15, Socket Options Slide # 3
Setting Socket Options - VisualWorks

One method to set standard TCP socket options

Uses Berkley sockets naming conventions

SocketAccessor>> setOptionsLevel: SocketAccessor SOL_SOCKET
      name: optionToSet
      value: optionValue.

setOptionsLevel: is always set to SocketAccessor SOL_SOCKET

Values for name: are found in SocketAccessor class under constants-socket options protocol

Example

| serverSocket |
serverSocket := SocketAccessor newTCPserverAtPort: 4444.
serverSocket 
   setOptionsLevel: SocketAccessor SOL_SOCKET
   name: SocketAccessor SO_RCVBUF
   value: 56 * 1024.


Doc 15, Socket Options Slide # 4
Reading Current Values

SocketAccessor>> getOptionsLevel: SocketAccessor SOL_SOCKET
      name: option

Returns result in a byte array

To interpret the bytes


   | childSocket rawBytes bufferSize |
   childSocket := SocketAccessor 
            newTCPclientToHost: 'rugby.sdsu.edu'
            port: 8008.
   rawBytes := childSocket 
            getOptionsLevel: SocketAccessor SOL_SOCKET
            name: SocketAccessor SO_RCVBUF.
   rawBytes changeClassTo: UninterpretedBytes.
   bufferSize := rawBytes longAt: 1.
   ^bufferSize



Doc 15, Socket Options Slide # 5

Timeouts


Socket will time out after specified time of inactivity

Java JDK 1.4 and later

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


VisualWorks

Does not support SO_RCVTIMEO or SO_SNDTIMEO

Need to use non-stream access for timeouts

Doc 15, Socket Options Slide # 6

Buffer Size


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


Doc 15, Socket Options Slide # 7
Default Buffer Size

Depends on platform

Has changed over time

OS
Receive buffer Size
Solaris
32KB
Mac OS 10
32KB+ (33304 bytes)


Doc 15, Socket Options Slide # 8
Setting the Buffer Size – VisualWorks

Setting the Receive buffer

aSocketAccesor setOptionsLevel: SocketAccessor SOL_SOCKET
   name: SocketAccessor SO_RCVBUF
   value: newBufferSize.


Setting the Send buffer

aSocketAccesor setOptionsLevel: SocketAccessor SOL_SOCKET
   name: SocketAccessor SO_SNDBUF
   value: newBufferSize.


Doc 15, Socket Options Slide # 9
Setting the Buffer Size – Java

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


Doc 15, Socket Options Slide # 10
Java Example

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 );
   }


Doc 15, Socket Options Slide # 11
Java Example Continued

   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();
         }
      }


Doc 15, Socket Options Slide # 12
Java Example Continued

   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");
         }
      }
   }

Doc 15, Socket Options Slide # 13

Nagle’s Algorithm



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


Doc 15, Socket Options Slide # 14
Java
class Socket 
   {
   void setTcpNoDelay(Boolean noDelay) throws SocketException
   void getTcpNoDelay() throws SocketException
   }

Set noDelay to true to turn delay off



VisualWorks

aSocketAccesor setOptionsLevel: SocketAccessor SOL_SOCKET
   name: SocketAccessor TCP_NODELAY
   value: delayValue.

Set delayValue to

or use the shorter method

aSocketAccesor tcpNoDelay: noDelay


Doc 15, Socket Options Slide # 15

Linger on close


Determines what happens when a socket is closed

How long does the socket remain after close to



Default is to



Doc 15, Socket Options Slide # 16

Keep Alive



Send packet on inactive connection to prevent timeouts

At least 2 hour delay between sending keep alive packets

Long delay limits it usefulness


Doc 15, Socket Options Slide # 17

Urgent (Out of Band) Data


Urgent data can be read out of order



Java 1.4





Copyright ©, All rights reserved.
2002 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 15-Oct-02    Next