COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

Only one application at a time can be bound to a specific port on the server. ... 00 = US is on Standard time. 50 = US is on Daylight Savings Time. ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 26
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 19-Feb-08
  • Lecture Set 11
  • Networking

2
Networking
  • Fundamental networking capabilities come from
    package java.net.
  • Offers
  • Stream-based communications
  • Allow applications to view networking as streams
    of data
  • Packet-based communications
  • Allows for transmission of individual packets of
    information
  • Often used for streaming audio or video

3
Socket-based communications
  • Allow an application to view networking as if it
    were just file I/O
  • Programs can read from and write to a socket just
    like reading/writing a file
  • Two types of sockets Stream and Datagram

4
Stream and Datagram sockets
  • Stream sockets Connection-oriented
  • Process establishes connection to another
    process
  • While connection is in place, data flows in
    continuous stream
  • Uses TCP (transmission control protocol)
  • Datagram sockets Connectionless
  • Individual packets of information are
    transmitted
  • Does not guarantee delivery, or that packets
    arrive in correct order
  • Uses UDP (user datagram protocol)
  • Less overhead than TCP (thus, commonly used for
    steaming broadcast data like video or audio)

5
A simple server using Stream Sockets
  • Requires 5 steps
  • 1 Create a Server-Socket object
  • ServerSocket s new ServerSocket(port,queuelngth
    )
  • Specifies TCP port number to use and number of
    clients that can wait to connect to the server
  • Port number is often called handshake point.
    Used by the client to locate the server at the ip
    address.
  • The above constructor binds the server to the
    port. Only one application at a time can be
    bound to a specific port on the server.
  • If queue is full, new client connections are
    denied.
  • Port number can be between 0 and 65,535. Note
    that ports below 1024 are ephemeral ports
    (reserved by the operating system in most cases)
    and should not be used when creating your server
    application unless there is a good reason to do
    so.

6
A simple server using Stream Sockets
  • 2 Tell server to listen for client connection
    attempts
  • Socket connection s.accept()
  • This causes the server to listen indefinitely
    (called blocking) for an attempt by a client to
    connect.
  • After connection attempt on listening port, the
    connection is pushed off onto another port
    (allowing server to accept a new connection on
    the listening port)

7
A simple server using Stream Sockets
  • 3 Get the OutputStream and InputStream objects
    that enable the server to communication with the
    client
  • At this point, we have two options byte
    oriented or character oriented network i/o
    streams.

8
Byte-Oriented I/O Streaming
  • ObjectInputStream input new ObjectInputStream
    (connection.getInputStream())
  • ObjectOutputStream output new
    ObjectOutputStream
  • (connection.getOutputStream())

9
Character Oriented I/O Streaming
  • This looks and acts just like the file I/O that
    we talked about before
  • BufferedReader is new BufferedReader(new
    InputStreamReader(connection.getInputStream()))
  • PrintStream os new PrintStream(
    connection.getOutputStream())

10
A simple server using Stream Sockets
  • 4 Processing phase
  • Server and client communicate via the input and
    output streams
  • Keep in mind that this is usually done according
    to a defined protocol
  • 5 End
  • When transmission is complete, server should
    close connection by invoking the close method on
    the streams and on the socket.
  • This is extremely important! A multi-threaded
    network application server must close all streams
    and sockets in order to avoid locking up system
    resources.

11
Server example
  • See ChatServer.java

12
A simple client using Stream Sockets
  • Clients are a 4 step process
  • 1 Create socket to connect to the server
  • Socket connection new Socket(serverAddress,port)

  • Can throw an IOException
  • Common exception is UnknownHostException (when
    client is unable to resolve server address to
    corresponding IP address).

13
A simple client using Stream Sockets
  • 2 Get the OutputStream and InputStream objects
    that enable the client to connunicate with the
    server
  • This is done the same as when creating the server
    IO streams
  • 3 Processing phase
  • 4 End

14
Client Example
  • See ChatClient.java

15
Class InetAddress
  • Java Class that represents an Internet Protocol
    (IP) address
  • Doesnt have any public constructors
  • Pass the hostname or string format of the dotted
    quad address to the static InetAddress.getByName()
    method
  • Contains many useful methods
  • getLocalHost()
  • Returns the local host name
  • getHostName()
  • Returns the hose name for this IP address
  • getHostAddress()
  • Determines the IP address of a host, given the
    hosts name
  • See API for others
  • http//java.sun.com/javase/6/docs/api/java/net/Ine
    tAddress.html

16
InetAddress Example
  • See InetAddressExample.java

17
Connectionless Client/Server interaction with
Datagrams
  • Uses classes DatagramSocket and DatagramPacket
  • See DatagramSend.java and DatagramReceive.java

18
A simple socket port scanner (local host)
  • This example shows you how to determine which
    ports are open and which are closed on the local
    machine
  • See PortScanExample.java

19
Connecting to a REAL server
  • Lets say we want to connect to an existing
    server.
  • how do we communicate?
  • All protocols are documented in RFCs (request
    for comments)

20
Various RFCs
  • Daytime protocol (RFC-867) http//www.ietf.org/r
    fc/rfc0867.txt
  • Time protocol (RFC-868) http//www.ietf.org/rfc/
    rfc0868.txt
  • NTP protocol (RFC-1305) http//www.ietf.org/rfc/
    rfc1305.txt
  • SMTP protocol (RFC-821) http//www.ietf.org/rfc/
    rfc0821.txt

21
Communicating with a NIST Daytime Server
  • NIST (National Institute of Standards and
    Technology) runs many time servers (daytime,
    time, NTP).
  • The servers are publicly accessible.
  • See http//www.time.gov/ and
    http//tf.nist.gov/tf-cgi/servers.cgi

22
NIST Daytime
  • The Daytime protocol is widely used by small
    computers running MS-DOS and similar operating
    systems.
  • The server listens on port 13, and responds to
    requests in either tcp/ip or udp/ip formats.
  • The standard does not specify an exact format for
    the Daytime Protocol, but requires that the time
    is sent using standard ASCII characters.
  • NIST chose a time code format similar to the one
    used by its dial-up Automated Computer Time
    Service (ACTS)

23
NIST Daytime Format
  • JJJJJ YR-MO-DA HHMMSS TT L H msADV UTC(NIST)
    OTM
  • JJJJ is Modified Julian Date (starting point of
    Nov. 17, 1858).
  • YR-MO-DA is the date. Last two digits of year,
    month, and current day of month.
  • HHMMSS is time in hours, minutes, seconds in
    UTC.
  • Apply offset for local time (-5 for EST)
  • TT is 00 to 99.
  • 00 US is on Standard time.
  • 50 US is on Daylight Savings Time.
  • In month of DST change, number changes to days
    till change and will decrement every day until
    change occurs.
  • L is code indicating leap second addition. (??)
  • H is health of server.
  • 0 healthy, 1 possible time error up to 5 sec,
    2 time error 5 sec, 3 server h/w or s/w
    failure, 4 maintenance mode
  • msADV millisecond advance on time to compensate
    for network delays
  • UTC(NIST) label to indicate where it came from
  • OTM on time marker (asterisk). Means that time
    is current when asterisk is printed.

24
Simple DayTime example
  • See DaytimeExample.java

25
Assignment 4
  • Posted see assignments page
Write a Comment
User Comments (0)
About PowerShow.com