Sockets For Servers - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Sockets For Servers

Description:

Client connects to a Server and requests services from the server ... If a connection indication arrives when the queue is full, the connection is refused. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 24
Provided by: yda1
Category:

less

Transcript and Presenter's Notes

Title: Sockets For Servers


1
Sockets For Servers
  • Instructors
  • Fu-Chiung Cheng
  • (???)
  • Associate Professor
  • Computer Science Engineering
  • Tatung University

2
Contents
  • The ServerSocket class
  • Examples

3
Server Socket
  • A socket is a connection between two hosts
  • Client connects to a Server and requests services
    from the server
  • Servers are like receptionists who sit by the
    phone and wait for incoming call.
  • Servers dont know in advance who will call
  • Java uses ServerSocket for servers

4
ServerSocket
  • Basic life cycle of a server
  • A new ServerSocket is created on a particular
    port using a ServerSocket() constructor
  • The ServerSocket linstens for incoming
    connection on the port (method accept() blocks
    until a client attempt to make a connect)
  • Server accepts a connection and returns a Socket
    objects connecting the client and the server
  • Using Sockets getInputStream() and
    getOutputStream for sending to and receiving data
    from the client
  • The client, the server or both close the
    connection

5
ServerSocket
  • ServerSocket public ServerSocket(int port)
  • Creates a server socket on a specified port.
  • A port of 0 creates a socket on any free port.
  • The maximum queue length for incoming connection
    indications (a request to connect) is set to 50.
  • If a connection indication arrives when the queue
    is full, the connection is refused.

6
ServerSocket
  • public ServerSocket(int port, int backlog)
  • Creates a server socket and binds it to the
    specified local port number, with the specified
    backlog.
  • A port number of 0 creates a socket on any free
    port.
  • The maximum queue length for incoming connection
    indications (a request to connect) is set to the
    backlog parameter.
  • If a connection indication arrives when the queue
    is full, the connection is refused.

7
ServerSocket
  • public ServerSocket(int port, int backlog,
    InetAddress bindAddr)
  • Create a server with the specified port, listen
    backlog, and local IP address to bind to.
  • The bindAddr argument can be used on a
    multi-homed host for a ServerSocket that will
    only accept connect requests to one of its
    addresses.
  • If bindAddr is null, it will default accepting
    connections on any/all local addresses.
  • The port must be between 0 and 65535, inclusive.

8
ServerSocket Example
  • Example 11-1 LocalPortScan on page 353
  • List all the services for this machine
  • Output from Win 2000
  • There is a server on port 135.
  • There is a server on port 139.
  • There is a server on port 445.
  • There is a server on port 1028.
  • There is a server on port 1041.
  • There is a server on port 1204.
  • There is a server on port 2145.
  • There is a server on port 2348.
  • There is a server on port 3207.
  • There is a server on port 8888.

9
ServerSocket
  • public Socket accept()
  • Listens for a connection to be made to this
    socket and accepts it.
  • The method blocks until a connection is made.
  • A new Socket s is created and, if there is a
    security manager, the security manager's
    checkAccept method is called with
    s.getInetAddress().getHostAddress() and
    s.getPort() as its arguments to ensure the
    operation is allowed.
  • This could result in a SecurityException.
  • Returnsthe new Socket

10
ServerSocket
  • Example 11-2 (Daytime Server) on page 357
  • No new thread is created for incoming call
  • Current date is returned

11
ServerSocket Information
  • public int getLocalPort()
  • Returns the port on which this socket is
    listening.
  • public InetAddress getInetAddress()
  • Returns the local address of this server socket.
  • null if the socket is not yet connected.
  • Example 11-4 RandomPort on page 362

12
Closing the ServerSocket
  • public void close()
  • Closes this socket.
  • Example
  • for (int port 1 port lt 65535 port)
  • try
  • ServerSocket server new
    ServerSocket(port)
  • server.close()
  • catch (IOException e)
  • System.out.println(Server on port "
    port ".")
  • // end try
  • // end for

13
ServerSocket Options
  • Accept will wait for an incoming connection for
    SO_TIMEOUT milliseconds.
  • public void setSoTimeout(int timeout)
  • With this option set to a non-zero timeout, a
    call to accept() for this ServerSocket will block
    for only this amount of time.
  • If the timeout expires, a java.io.InterruptedIOExc
    eption is raised
  • The option must be enabled prior to entering the
    blocking operation to have effect.
  • A timeout of zero is interpreted as an infinite
    timeout.

14
ServerSocket Options
  • public int getSoTimeout()
  • Retrive setting for SO_TIMEOUT.
  • 0 returns implies that the option is disabled
    (i.e., timeout of infinity).
  • Examples in page 363

15
Client Test
  • Example 11-5 on page 365
  • InputThread reads data from IE
  • OuputThread sends data to IE

16
Single File Server
  • Page 370
  • InputThread and OutputThread are executed in
    parallel.
  • Can send a file to say IE or Netscape

17
Redirector
  • Redirector redirects users from one web site to
    another
  • See Example 11-7.
  • Try telnet 140.129.20.250 80
  • type GET / HTTP 1.0 (get index.html)
  • response HTTP 1.0 302 found ..
  • java Redirector http//140.129.20.250/
  • Redirect all http requests to 140.129.20.250

18
Redirector
  • Ask Browser to redirect the request to newSite
  • out.write("HTTP/1.0 302 FOUND\r\n")
  • -- tell client to expect to be redirected
  • out.write("Date " now "\r\n")
  • -- optional (give current time of
    server)
  • out.write("Server Redirector 1.0\r\n")
  • -- Optional (version of the server)
  • out.write("Location " newSite theFile
    "\r\n")
  • -- redirect to newSite

19
Redirector
  • Note that some browser does not support
    redirection (OK for IE 5.0 and up and Netscape
    4.7 and up)
  • Print the website is moved to newSite message.
  • See page 378 (ltHTMLgtltHEADgtltTITLEgtDocument
    movedlt/TITLEgtlt/HEADgt .)
  • You may telnet to request redirector service
  • Can be used to
  • firewall and
  • load balance control
  • Thread pool provides better performance

20
JHTTP
  • JHTTP Full-blown HTTP server
  • JHTTP can serve an entire document tree
  • Images
  • Applets
  • HTML and text files
  • JHTTP uses thread pool to serve request
  • Threads are created when the server is started
  • If there is no request, thread are put to sleep
    (wait)
  • A request from a client will wake up all threads
    in the thread pool for service (notifyAll)

21
JHTTP
  • When the thread is serving the request, it is
    just like a simple file transfer.
  • JHTTP thread check (parse) the request and send
    the response
  • JHTTP thread close the connection and go to
    sleep if there is no more request to be service

22
JHTTP
  • JHTTP is functional but still rather simple
  • A few features that can be added
  • A server administration interface
  • Support CGI programs and/or Java Servlet API
  • Support for other request methods such as POST,
    HEAD, and PUT
  • Server-side includes and/or Java server page

23
JHTTP
  • Performance Issues
  • Use Just-in-time compiler such as Hotspot
  • 2x 10x
  • Smart caching
  • Use Hashtable
  • Use low-priority thread to update the cache
Write a Comment
User Comments (0)
About PowerShow.com