Presentation is loading. Please wait.

Presentation is loading. Please wait.

Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.

Similar presentations


Presentation on theme: "Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun."— Presentation transcript:

1 Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun.

2 Spring 2006CPE 0907532 Special Topics in Computer Engineering2 Topics l Socket Constructors l Socket Options l Issues in Client/Server Programming

3 Spring 2006CPE 0907532 Special Topics in Computer Engineering3 Socket Class: Commonly Used Socket Class: Commonly Used Constructors I l Socket (InetAddress address, int port) throws java.io.IOException, Java.lang.SecurityException –creates a socket connected to the specified IP address and port –an exception is thrown if u a connection cannot be established or u connecting to that host violates a security restriction (e.g. when an applet tries to connect to a machine other than the machine from which it was loaded)

4 Spring 2006CPE 0907532 Special Topics in Computer Engineering4 Socket Class: Commonly Used Socket Class: Commonly Used Constructors II l Socket (InetAddress address, int port, InetAddress localAddress, int locaPort) throws java.io.IOException, javaJang.SecurityException –creates a socket connected to the specified IP address and port –bound to the specified local address and local port u useful in the case of multi-horned hosts (i.e., a machine where the localhost is known by two or more IP addresses).

5 Spring 2006CPE 0907532 Special Topics in Computer Engineering5 Socket Class: Commonly Used Socket Class: Commonly Used Constructors III l Socket (String host, int port) throws java.net.UnknownHostException, java.io.IOException, javaJang.SecurityException –allows a string to be specified, rather than an InetAddress –exception thrown u host name could not be resolved u a connection could not be established u a security restriction is violated

6 Spring 2006CPE 0907532 Special Topics in Computer Engineering6 Socket Class: Commonly Used Socket Class: Commonly Used Constructors IV l Socket (String host, int port, InetAddress localAddress, int locaPort) throws java.net.UnknownHostException, java.io.IOException, javaJang.SecurityException –creates a socket connected to the specified IP address and port –bound to the specified local address and local port u useful in the case of multi-horned hosts (i.e., a machine where the localhost is known by two or more IP addresses).

7 Spring 2006CPE 0907532 Special Topics in Computer Engineering7 Creating a Socket Under normal circumstances a socket is connected to a remote machine and port when it is created l Call to a socket constructor will return as soon as a connection is established l If remote machine is not responding –Constructor method may block for an indefinite amount of time –No guarantee how long a socket may block for –In mission-critical systems, place such calls in a second thread, to prevent an application from stalling

8 Spring 2006CPE 0907532 Special Topics in Computer Engineering8 Using a Socket: Some Methods l void close() throws java.io.IOException – closes connection u may or may not allow remaining data to be sent u flush any output streams before closing a socket connection l InetAddress getlnetAddress() –returns the address of the remote machine l InputStream getlnputstream() throws java.io.I0Exception –returns an input stream, which reads from the application this socket is connected to l OutputStream getoutputStream() throws java.io.IOException –returns an output stream, which writes to the application that this socket is connected to l More methods: See relevant Sun Documentation

9 Spring 2006CPE 0907532 Special Topics in Computer Engineering9 l It's important to know about some of these topics, although it might not be apparent how and when to use them. l Details are in Sun documentation - we are just trying to get some idea of what can be done. TCP Socket Options

10 Spring 2006CPE 0907532 Special Topics in Computer Engineering10 Socket Options l Various attributes that are used to determine the behavior of sockets. –i.e. settings that modify how sockets work l Setting options tells the OS/Protocol Stack the behavior we want.

11 Spring 2006CPE 0907532 Special Topics in Computer Engineering11 Option types l Many socket options are boolean flags indicating whether some feature is enabled (true) or disabled (false). l Other options are associated with different data types, e.g. int, representing time. l Some options are readable only (we can’t set the value).

12 Spring 2006CPE 0907532 Special Topics in Computer Engineering12 Setting and Getting option values Examples: l To increase the receive buffer size to 4,096 bytes.setReceiveBufferSize(4096); someSocket.setReceiveBufferSize(4096); l To determine the size of the current receive buffer int size = someSocket.getReceiveBufferSize();

13 Spring 2006CPE 0907532 Special Topics in Computer Engineering13 Some Options l SO_KEEPALIVE l SO_RCVBUF l SO_SNDBUF l SO_LINGER l SO_TIMEOUT

14 Spring 2006CPE 0907532 Special Topics in Computer Engineering14 SO_KEEPALIVE someSocket.setSoKeepAlive(true); l Boolean option: enabled means that a socket should send a probe to peer if no data flow for a “long time”. l Used by TCP - allows a process to determine whether peer process/host has crashed. –Application doesn’t have any control over how often KEEPALIVE probes are sent. –A better solution than KEEPALIVE is to instead modify the timeout socket option.

15 Spring 2006CPE 0907532 Special Topics in Computer Engineering15 SO_RCVBUF SO_SNDBUF l Integer values options – request to change the receive and send buffer sizes. –Change is not guaranteed u Some operating systems may not allow this socket option to be modified –Ignore any requests to change value l Can be used with TCP and UDP sockets l With TCP, this option effects the window size used for flow control - must be established before connection is made. –someSocket.{g|s}et{Send|Receive}BufferSize(..);

16 Spring 2006CPE 0907532 Special Topics in Computer Engineering16 SO_LINGER l Used to control whether and how long a call to close will wait for pending ACKS. l connection-oriented sockets only. l setSoLinger(boolean onFlag, int duration); l getSoLinger(); returns duration (-1 if option is disabled)

17 Spring 2006CPE 0907532 Special Topics in Computer Engineering17 SO_LINGER l If a TCP socket connection is closed, data may still be queued for delivery (e.g. IP datagram lost and must be resent). u SO_LINGER option used to control whether and how long a call to close will wait for pending ACKS. l connection-oriented sockets only. l setSoLinger(boolean onFlag, int duration); // Enable linger, for fifty seconds someSocket.setSoLinger( true, 50 ); l getSoLinger(); returns duration (-1 if option is disabled)

18 Spring 2006CPE 0907532 Special Topics in Computer Engineering18 SO_LINGER usage l Disables/enables immediate return from a close() of a TCP Socket. –Enabling this option with a non-zero Integer timeout means that a close() will block pending the transmission and acknowledgement of all data written to the peer, at which point the socket is closed gracefully. –Upon reaching the linger timeout, the socket is closed forcefully, with a TCP RST. l Enabling the option with a timeout of zero does a forceful close immediately. l If the specified timeout value exceeds 65,535 it will be reduced to 65,535.

19 Spring 2006CPE 0907532 Special Topics in Computer Engineering19 SO_TIMEOUT l By default, I/O operations are blocking. –An attempt to read data from an InputStream will wait indefinitely until input arrives u If the input never arrives, the application stalls (unless multi- threading is used). l Enable SO_TIMEOUT option –A read request to the InputStream of a socket starts a timer –When no data arrives in time and the timer expires, a java.io.InterruptedIOException is thrown u retry IO u notify user u connection aborted

20 Spring 2006CPE 0907532 Special Topics in Computer Engineering20 SO_TIMEOUT (cont.) l Used to tell the socket to use non-blocking read. l getSoTimeout() returns the current setting (by default 0, or disabled, representing a blocking read). –e.g. to tell socket to interrupt reading if 5 seconds pass by, use: mySocket.setSoTimeout(5000);

21 Spring 2006CPE 0907532 Special Topics in Computer Engineering21 Nagle’s Algorithm l When using IP datagrams, overhead exists for each packet (e.g. IP and TCP header information) l If only a few bytes at a time are sent in each packet, the size of the header information will far exceed that of the data.

22 Spring 2006CPE 0907532 Special Topics in Computer Engineering22 Nagle’s Algorithm (cont.) l TCP may send only one datagram at a time –When an acknowledgment comes back for each IP datagram u A new packet is sent containing any data that has been queued up. l Bandwidth consumed by packet header information is decreased l Network latency increased l Systems that require quick response times (e.g. telnet) are slowed –TCP_NODELAY Socket Option is used to disable Nagle’s algorithm

23 Spring 2006CPE 0907532 Special Topics in Computer Engineering23 TCP_NODELAY Socket Option // Disable Nagle’s algorithm for faster response times someSocket.setTcpNoDelay(false); // Get the state of the TCP_NODELAY flag boolean state = someSocket.getTcpNoDelay();

24 Spring 2006CPE 0907532 Special Topics in Computer Engineering24 l This was just an overview –there are many details associated with the options described. –There are many options that haven’t been described. –UNIX Network Programming is one of the best sources of information about socket options. Socket Options Summary Not ALL options are (fully) supported by Java.

25 Spring 2006CPE 0907532 Special Topics in Computer Engineering25 Issues in Client Programming l Identifying the Server. l Looking up an IP address. l Looking up a well known port name. l Specifying a local IP address l UDP client design. l TCP client design.

26 Spring 2006CPE 0907532 Special Topics in Computer Engineering26 Identifying a TCP/IP server l Need an IP address, protocol and port. –We often use host names instead of IP addresses. –usually the protocol (UDP vs. TCP) is not specified by the user. –often the port is not specified by the user.

27 Spring 2006CPE 0907532 Special Topics in Computer Engineering27 Services and Ports l Many services are available via “well known” addresses (names). l There is a mapping of service names to port numbers.

28 Spring 2006CPE 0907532 Special Topics in Computer Engineering28 Specifying a Local Address l When a client creates and binds a socket it must specify a local port and IP address. l Typically a client doesn’t care what port it is on: mySocket = new DatagramSocket() give me any available port !

29 Spring 2006CPE 0907532 Special Topics in Computer Engineering29 Local IP address l A client can also ask the operating system to take care of specifying the local IP address: myAddress = InetAddress.getLocalHost(); Give me the appropriate address

30 Spring 2006CPE 0907532 Special Topics in Computer Engineering30 UDP Client Design l Establish server address (IP and port). l Allocate a socket. l Communicate with server (send, receive) l Close the socket.

31 Spring 2006CPE 0907532 Special Topics in Computer Engineering31 TCP Client Design l Establish server address (IP and port). l Allocate a socket. l Communicate with server (through given streams). l Close the connection.

32 Spring 2006CPE 0907532 Special Topics in Computer Engineering32 Closing a TCP socket l Many TCP based application protocols support multiple requests and/or variable length requests over a single TCP connection. l How does the server known when the client is done (and it is OK to close the socket) ?

33 Spring 2006CPE 0907532 Special Topics in Computer Engineering33 Partial Close l One solution is for the client to shut down only it’s writing end of the socket. l The socket call provides this function. l The shutdownOutput() socket call provides this function. mySocket.shutdownOutput(); mySocket.shutdownOutput(); –shutdownOutput() flushes output stream and sends TCP-connection termination sequence. –shutdownInput() closes input stream and discards any further information (further read()s will get -1)

34 Spring 2006CPE 0907532 Special Topics in Computer Engineering34 Server Design IterativeConnectionlessIterativeConnection-Oriented ConcurrentConnection-OrientedConcurrentConnectionless

35 Spring 2006CPE 0907532 Special Topics in Computer Engineering35 Concurrent vs. Iterative Iterative Small, fixed size requests Easy to program Iterative Small, fixed size requests Easy to program Concurrent Large or variable size requests Harder to program Typically uses more system resources Concurrent Large or variable size requests Harder to program Typically uses more system resources

36 Spring 2006CPE 0907532 Special Topics in Computer Engineering36 Connectionless vs. Connection-Oriented Connection-Oriented EASY TO PROGRAM transport protocol handles the tough stuff. requires separate socket for each connection. Connection-Oriented EASY TO PROGRAM transport protocol handles the tough stuff. requires separate socket for each connection. Connectionless less overhead no limitation on number of clients Connectionless less overhead no limitation on number of clients

37 Spring 2006CPE 0907532 Special Topics in Computer Engineering37 Statelessness l State: Information that a server maintains about the status of ongoing client interactions. l Connectionless servers that keep state information must be designed carefully! Messages can be duplicated!

38 Spring 2006CPE 0907532 Special Topics in Computer Engineering38 The Dangers of Statefullness l Clients can go down at any time. l Client hosts can reboot many times. l The network can lose messages. l The network can duplicate messages.

39 Spring 2006CPE 0907532 Special Topics in Computer Engineering39 Concurrent Server Design Alternatives One process per client Spawn one thread per client Preforking multiple processes Prethreaded Server

40 Spring 2006CPE 0907532 Special Topics in Computer Engineering40 One child process per client l Traditional Unix server l Parent process needs to clean up after children!!!!

41 Spring 2006CPE 0907532 Special Topics in Computer Engineering41 One thread per client l Use new Thread().start(); l Using threads makes it easier (less overhead) to have sibling processes share information. l Sharing information must be done carefully (use synchronized) Watch out for deadlocks!

42 Spring 2006CPE 0907532 Special Topics in Computer Engineering42 Pre-threaded Server l Can have the main thread do all the calls to accept() and hand off each client to an existing thread.

43 Spring 2006CPE 0907532 Special Topics in Computer Engineering43 What’s the best server design for my application? l Many factors: –Expected number of simultaneous clients. –Transaction size (time to compute or lookup the answer) –Variability in transaction size. –Available system resources (what resources can be required in order to run the service).

44 Spring 2006CPE 0907532 Special Topics in Computer Engineering44 Server Design l It is important to understand the issues and options. l Knowledge of queuing theory can be a big help. l You might need to test a few alternatives to determine the best design.


Download ppt "Client/Server Design Issues Based on Java Network Programming and Distributed Computing, Chapter 6 Also Online Java Tutorial, Sun."

Similar presentations


Ads by Google