Introduction to Sockets - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Introduction to Sockets

Description:

A socket is bound to a port number so that the TCP layer can identify the ... Goodbye.'; } catch (IOException e) { dString = 'IOException occurred in server. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 19
Provided by: russell5
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Sockets


1
Introduction to Sockets
  • A socket is one endpoint of a two-way
    communication link between two programs running
    on the network. A socket is bound to a port
    number so that the TCP layer can identify the
    application to which data is destined to be sent.

2
Sockets
  • Interface between application and protocol
    software
  • de facto standard
  • Usually, part of OS itself
  • Integrated with system I/O. Like file I/O.
  • open-read-write-close paradigm
  • Referenced by integer socket descriptor

3
(No Transcript)
4
Networking Review
  • TCP/IP Protocol Suite
  • Application programming interface to TCP/IP (API)
  • BSD Unix
  • Java.net.Socket
  • Platform Independence

5
(No Transcript)
6
Ports
  • TCP and UDP use ports to map incoming data to a
    particular process running on the computer
  • Ports are 16 bit numbers
  • Need IP Address (32-bits) and port.
  • 0-1023 Restricted out of 65,535.

7
Networking Classes in Java
  • TCP URL, URLConnection, Socket, ServerSocket
  • UDP DatagramPacket, DatagramSocket,
    MulticastSocket
  • Socket and ServerSocket form two ends of
    communicaiton link.

8
(No Transcript)
9
Procedure
  • Open a socket
  • Open an input and output stream to the socket
  • Read from and write to the stream according to
    the servers protocol
  • close streams
  • close socket

10
Client/Server
  • Request - Response
  • A server application waits passively for contact.
  • A client application initiates communication
    actively.

11
Clients
  • Client temporarily when remote access needed.
    Does other computation locally.
  • Runs locally
  • Invoked by user.
  • Initiates contact
  • Can access multiple services as needed
  • No special h/w or OS

12
Servers
  • Special purpose to provide one service
  • Invoked automatically at boot
  • Runs on server (shared computer)
  • Waits on connections passively
  • Accept connections from arbitrary clients
  • Requires powerful h/w and special purpose OS

13
Datagrams
  • An independent self-contained message
  • Not Reliable
  • DatagramPacket and DatagramSocket
  • DatagramSocket Specify port or not
  • DatagramPacket Contains addressing information
  • MulticastSocket To broadcast

14
import java.io. import java.net. import
java.util. public class QuoteMain
public static void main(String args) throws
IOException BufferedReader in null
boolean moreQuotes true
DatagramSocket socket new DatagramSocket(4445)
try in new
BufferedReader(new FileReader("one-liners.txt"))
catch (FileNotFoundException e)
System.err.println("Could not open quote
file. Serving time instead.")
15
while (moreQuotes) try
byte buf new byte256
// receive request
DatagramPacket packet new DatagramPacket(buf,
buf.length) socket.receive(packet
) // figure out response
String dString null
if (in null) dString
new Date().toString() else
try if ((dString
in.readLine()) null)
in.close() moreQuotes false
dString "No more quotes. Goodbye."
catch (IOException e)
dString "IOException occurred
in server."
16
buf dString.getBytes() // send the
response to the client at "address" and "port"
InetAddress address
packet.getAddress() int port
packet.getPort() packet new
DatagramPacket(buf, buf.length, address, port)
socket.send(packet)
catch (IOException e)
e.printStackTrace() moreQuotes false
socket.close()
17
import java.io. import java.net. import
java.util. public class QuoteClient
public static void main(String args) throws
IOException if (args.length ! 1)
System.out.println("Usage java
QuoteClient lthostnamegt") return
// get a datagram socket
DatagramSocket socket new DatagramSocket()
// send request byte buf
new byte256 InetAddress address
InetAddress.getByName(args0)
DatagramPacket packet new DatagramPacket(buf,
buf.length, address, 4445)
socket.send(packet)
18
// get response packet new
DatagramPacket(buf, buf.length)
socket.receive(packet) // display
response String received new
String(packet.getData())
System.out.println("Quote of the Moment "
received) socket.close()
Write a Comment
User Comments (0)
About PowerShow.com