Sockets for clients - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Sockets for clients

Description:

Sockets are used to setup this connection. Socket basics. Is a connection between ... Straight forward protocol based on TCP using port 79. Defined in RFC 1288 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 18
Provided by: v5o5jotqkg
Category:
Tags: clients | setup | sockets

less

Transcript and Presenter's Notes

Title: Sockets for clients


1
Sockets for clients
  • Lecture 6

2
  • Data Communication across Internet
  • Socket basics
  • Socket Class
  • Socket Exceptions
  • Socket Addresses
  • Examples

3
Data Communication across Internet
  • Data is transmitted in packets of fixed size
    called datagrams
  • Each datagram contains a header a payload
  • Header has the address port to which the data
    is going, from where it came other housekeeping
    info
  • Data can be carried by a number of datagrams
  • Pkts could be lost or could arrive in different
    order
  • Sockets are used to setup this connection

4
Socket basics
  • Is a connection between two hosts
  • Has seven basic opns
  • Connect to a remote machine (both client
    server)
  • Send data (both client server)
  • Receive data (both client server)
  • Close a connection (both client server)
  • Bind to a port (only server)
  • Listen for incoming data (only server)
  • Accept connections from remote machine on the
    bound port (only server)

5
Java socket class
  • Used by both clients servers
  • How it works
  • Pgm creates a new socket with a constructor
  • Socket attempts to connect to the remote host
  • Once connection is established, the local
    remote hosts get input and output streams from
    the socket and use those streams to send data to
    each other (Full-duplex)
  • When the transmission of data is complete, one or
    both sides close the connection.

6
Socket class cont..
  • The methods of the Socket class set up and tear
    down constructions and set various socket
    options.
  • TCP connections are reliable and uses the
    Streams interface
  • The constructors allow you to specify the host
    and the port you want to connect to

7
The constructors
  • Public Socket(String host, int port) throws
    UnknownHostException
  • try
  • Socket toUNB new Socket (www.unb.ca,80)
  • //send and receive data
  • catch (UnknownHostException ex)
  • System.err.println(ex)

8
Socket class constructors cont..
  • Public Socket(InetAddress host, int port) throws
    IOException
  • try
  • InetAddress UNB InetAddress.getByName
    (www.unb.ca)
  • Socket UNBSocket new Socket (unb,80)
  • //send and receive data
  • catch (UnknownHostException ex)
  • System.err.println(ex)
  • Catch (IOException ex)
  • System.err.println(ex)

9
Getting info about a socket
  • Find the host that the socket was connected to
  • Public InetAddress getinetAddress()
  • try(
  • Socket theSocket new Socket (UNB.ca, 80)
  • InetAddress host the Socket.getInetAddress()
  • System.out.println (Connected to remote host
    host)
  • // end try
  • catch (UnknownHostException ex)
  • System.err.println (ex)
  • catch (IOException ex)
  • System.err.println (ex)

10
Getting info about a socket cont..
  • Find the port the socket was connected to
  • Public InetAddress getinetAddress()
  • try(
  • Socket theSocket new Socket (UNB.ca, 80)
  • int port the Socket.getPort()
  • System.out.println (Connected to remote port
    port)
  • // end try
  • catch (UnknownHostException ex)
  • System.err.println (ex)
  • catch (IOException ex)
  • System.err.println (ex)
  • Similarly there are TWO more methods
  • Public int getLocalPort()
  • Public int getLocalAddress()
  • Examples http//www.cafeaulait.org/books/jnp3/exa
    mples/09/

11
Input and Output streams
  • Public inputStream getInputStream() throws
    Exception
  • Public outputStream getoutputStream() throws
    Exception
  • Example
  • A day time protocol client

12
Closing the socket
  • Public void close() throws IOException
  • Socket connection null
  • try
  • InetAddress UNB InetAddress.getByName
    (www.unb.ca)
  • Socket UNBSocket new Socket (unb,80)
  • //send and receive data
  • catch (UnknownHostException ex)
  • System.err.println(ex)
  • Catch (IOException ex)
  • System.err.println(ex)
  • finally
  • If (connection ! null) connection.close()

13
Setting socket options
  • TCP-NODELAY
  • SO_BINDADDR
  • SO_TIMEOUT
  • SO_LINGER
  • SO_SNDBUF
  • SO_RCVBUF
  • SO_KEEPALIVE
  • OOBINLINE

14
Class of service
  • Java lets you inspect and set the class of
    service for a socket
  • Public int getTrafficClass() throws
    SocketException
  • Public void setTrafficClass(int trafficClass)
    throws SocketException
  • 0x02 Low cost
  • 0x04 High reliability
  • 0x08 Maximum throughput
  • 0x10 Maximum delay
  • Examples Socket s new Socket (www.unb.ca,80)
  • s. setTrafficClass(0x02)
  • Socket s new Socket (www.unb.ca,80)
  • s.setTrafficClass (0x08 / 0x10)

15
Examples from book
  • http//www.cafeaulait.org/books/jnp3/examples/09/

16
Finger Protocol
  • Written by Les Earnest Earl Killiar Brian
    Harvey at MIT
  • Straight forward protocol based on TCP using port
    79
  • Defined in RFC 1288
  • Gives user info in the .plan file
  • Example http//www.cafeaulait.org/books/jnp3/exam
    ples/09/

17
Whois
  • Simple directory service protocol RFC 954
  • Example http//www.cafeaulait.org/books/jnp3/exam
    ples/09/
Write a Comment
User Comments (0)
About PowerShow.com