Sockets for Servers - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Sockets for Servers

Description:

Keeps listening for clients & accept connections. Server socket runs on the server and ... A full-fledged HTTP server. must respond to requests for files ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 19
Provided by: v5o5jotqkg
Category:

less

Transcript and Presenter's Notes

Title: Sockets for Servers


1
Sockets for Servers
  • Lecture 7 (Chapter 10)

2
(No Transcript)
3
What server sockets do??
  • Keeps listening for clients accept connections
  • Server socket runs on the server and listens for
    incoming TCP connections.
  • Each server socket listens on a particular port
    on the server machine
  • When a client on a remote host attempts to
    connect to that port, the server wakes up,
    negotiates the connection between the client and
    the server, and runs a regular socket object
    representing the socket between the two hosts.
  • Server sockets wait for connections while client
    sockets initiate connections.

4
server socket class
5
Sever Socket class
  • Contains everything needed to write servers in
    Java
  • - constructors that create new ServerSocket
    objects
  • - methods that listen for connections on a
    specified port
  • - methods that configure the various server
    socket option
  • - other miscellaneous methods

6
Life cycle of a server program
  • 1. A new ServerSocket is created on a particular
    port using ServerSocket() constructor
  • 2. ServerSocket listens for incoming connection
    attempts on that port using its accept() method
  • 3. Depending on the type of server, the Sockets
    getInputStream() or getOutputStream() or both are
    called to get I/O streams to communicate with
    client
  • 4. Server-client interact according to an
    agreed-upon protocol until it is time to close
    the connection
  • 5. The server, the client or both close the
    connection
  • 6. The server returns to step 2 and keeps
    listening for the next connection

7
ServerSocket constructors
  • Four
  • Public ServerSocket(int port) throws
    BindException, IOException
  • Public ServerSocket(int port, int queueLength)
    throws BindException, IOException
  • Public ServerSocket(int port, int queueLength,
    InetAddress bindAddress) throws BindException,
    IOException
  • Public ServerSocket() throws IOException //Java
    1.4

8
Public ServerSocket(int port) throws
BindException, IOException
  • try
  • ServerSocket httpd new ServerSocket (80)
  • catch (IOException ex)
  • System.err.println(ex)

9
Public ServerSocket(int port, int queueLength)
throws BindException, IOException
  • try
  • ServerSocket httpd new ServerSocket (2336, 10)
  • catch (IOException ex)
  • System.err.println(ex)

10
Public ServerSocket(int port, int queueLength,
InetAddress bindAddress) throws BindException,
IOException
  • try
  • ServerSocket httpd new ServerSocket (2336, 10,
    InetAddress.getByName(183.143.12.12))
  • catch (IOException ex)
  • System.err.println(ex)

11
Accepting closing connections
  • Public Sockets accept() throws IOException
  • Public void close() throws IOException
  • Ex Day time server

12
The get methods
  • ServerSocket class provides two getter methods
  • Local address
  • Port occupied by the server socket
  • Public InetAddress getInetAddress()
  • ServerSocket httpd new ServerSocket (80)
  • InetAddress ia httpd.getInetAddress()
  • Public int getLocalPort()
  • Ex A random port

13
Socket Options
  • SO_TIMEOUT
  • SO_REUSEADDR
  • SO_RCVBUF
  • Java 1.5
  • Public void setPerformancePreferences (int
    connectionTime, int latency, int bandwidth)

14
Some useful servers
  • Client Tester
  • HTTP servers (3 types)

15
Client Tester
  • Runs on a port specified on the command line,
    shows all data sent by the client, and allows you
    to send a response to the client by typing it on
    the command line.
  • Uses two threads one to handle input from the
    client and another to send output from the server
  • From book client tester

16
HTTP Servers
  • Single file server
  • A redirector
  • A full-fledged HTTP server
  • must respond to requests for files
  • Convert urls into filenames on local system
  • Respond to POST GET requests
  • Handle requests for files that dont exist
  • Interpret MIME types
  • Much more
  • We can custom make servers

17
Custom servers
  • Useful for small sites
  • High traffic sites like yahoo! Can also custom
    made since they do only one thing
  • Much faster than general purpose servers such as
    Apache Microsoft IIS
  • Easy to optimize for a special purpose
  • Java is not a bad language for this!
  • Web servers is one area where Java is competitive
    with C.

18
HTTP Server examples
  • Server examples
Write a Comment
User Comments (0)
About PowerShow.com