Cup 10: Networking - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Cup 10: Networking

Description:

The following ststic methods return a new InetAddress object. InetAddress. ... HTTP, FTP, GOPHER, FILE, NEWS. Host. dotted quad or domain name format. Port ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: timma87
Category:
Tags: cup | gopher | networking

less

Transcript and Presenter's Notes

Title: Cup 10: Networking


1
Cup 10 Networking
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
java.net
  • Contains classes related to networking
  • IP Address
  • Uniform Resource Locator (URL)
  • Sockets
  • Server and regular sockets
  • Transmission Protocols
  • TCP/IP
  • UDP

3
IP Addresses
  • 130.101.5.4
  • dotted quad format
  • kira.cc.uakron.edu
  • domain name format
  • The following ststic methods return a new
    InetAddress object
  • InetAddress.getByName(String)
  • InetAddress.getLocalHost()

4
InetAddress Class
  • getAllByName(String)
  • returns an array of InetAddress objects
  • UnknownHostException
  • getHostAddress()
  • returns the IP address as a String in dotted quad
    format
  • getHostName()
  • returns the domain name

5
Uniform Resource Locator
  • Protocol
  • HTTP, FTP, GOPHER, FILE, NEWS
  • Host
  • dotted quad or domain name format
  • Port
  • 0-1024 are more or less reserved
  • File
  • A String representing the resource

6
URL Class
  • URL(String, String, int, String)
  • URL(String, String, String)
  • port number is implied by the protocol
  • URL(String)
  • "http//www.cs.uakron.edu/index.html"
  • URL(URL, String)
  • creates a relative URL, parsing the String within
    the context of the given URL

7
Establishing a Connection with a URL
  • URLConnection openConnection()
  • Instantiates a connection Object for the the URL
  • connection settings are manipulated before the
    actual connection is established
  • Method connect() establishes the connection using
    the appropriate protocol handler and settings of
    the URLConnection object

8
Using a Connection with a URL
  • Object getContent()
  • InputStream getInputStream()
  • A URL's openStream() method is a shortcut for the
    two-step process URL.openConnection().getInputStr
    eam()
  • OutputStream getOutputStream()
  • String getVariousHeaderInfo()

9
Sockets
  • Stream - supports active communications between
    two computers
  • a route is established
  • data is streamed in packets
  • receipt is verified before the next packet is sent
  • Datagram - supports connectionless communication
    (UDP)
  • message is broken into packets and sent to a
    router
  • packets bounce between routers until they arrive
    at their destination
  • packets must be reassembled to retrieve the
    message

10
Client Sockets
  • Socket cs new Socket(addr, port)
  • applt-gtclientlt------gtserverlt-gtapp
  • InputSream and OutputStream objects are
    constructed from the socket to support transfer
    of data
  • The remote socket is usually created by a server
    to form the other end of the connection

11
Boilerplate Server
  • ServerSocket server new ServerSocket(1234)
  • Socket sock server.accept()
  • Thread is blocked at this statement until a
    connection request occurs which causes a new
    Socket to be created to form the other end of the
    communications stream
  • InputStream in sock.getInputStream()
  • OutputStream out sock.getOutputStream()

12
Datagram Client/Server
  • The server and client simply create a Datagram
    socket
  • Datagram socket new DatagramSocket(port)
  • The system will assign a port number if you do
    not request one
  • The server should specify a port number so the
    client knows where to send the datagrams

13
DatagramPacket
  • new DatagramPacket(msg, len, addr, port)
  • This creates a packet to be sent
  • msg is a byte array
  • len is the size of the msg (in bytes)
  • addr is the InetAddress the package is going to
  • port is the port number on the receiving end

14
Sending and Receiving Datagrams
  • socket.send(aPacket)
  • Packet is sent out on the Internet and is routed
    toward the destination
  • socket.receive(anotherPacket)
  • This is a blocking statement that waits for a
    datagram to be received.
  • anotherPacket was created with no address info,
    just a byte array and length

15
Decoding a Received DatagramPacket
  • pack.getAddress()
  • pack.getPort()
  • pack.getLength()
  • pack.getData()
  • set methods
Write a Comment
User Comments (0)
About PowerShow.com