Socket Programming in Java - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Socket Programming in Java

Description:

Represents datagram socket for sending and receiving packets. ... Programming steps for receiving data packet. Create DatagramSocket for listening ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 11
Provided by: orc52
Category:

less

Transcript and Presenter's Notes

Title: Socket Programming in Java


1
Socket Programming in Java
  • Prepared by Dr. Tony K. C. Chan.

2
Learning Objectives
  • Basic skill of socket programming in Java
  • Sending packet using datagram socket
  • Receiving packet using datagram socket

3
1. Introduction
  • Socket the interface between the application
    layer and the transport layer within a host.

4
1. Introduction
  • Stream communication
  • TCP (Transmission Control Protocol)
  • Reliable and in-order delivery
  • e.g., WWW, e-mail, Secure Shell.
  • Datagram communication
  • UDP (User Datagram Protocol)
  • Datagram may arrive out of order or go missing
    without notice.
  • Faster and more efficient for lightweight or
    time-sensitive purposes.

5
2. Processing Address
  • IP Address Identifies the interface that a host
    is connected.
  • Port number Identify the receiving process.

6
2. Processing Address
  • The InetAddress class
  • Contains methods for handling the IP Address.
  • Some useful methods.
  • static InetAddress getLocalHost() Returns the
    local host.
  • static InetAddress getByName(String host)
    Determines the IP address of the given host name.
  • String getHostAddress() Returns the IP address
    in String.
  • Example Get local host IP address in String
  • InetAddress.getLocalHost().getHostAddress()

7
3. Socket Programming Using Datagram
  • DatagramPacket
  • Represents datagram packet.
  • Implements connectionless packet delivery
    service.
  • DatagramSocket
  • Represents datagram socket for sending and
    receiving packets.
  • The sending and receiving point for a packet
    delivery service.

8
3. Socket Programming Using Datagram
  • Programming steps for receiving data packet
  • Create DatagramSocket for listeningDatagramSocket
    socket new DatagramSocket(2000)
  • Create a byte array to hold the received
    databyte buf new byte256
  • Create DatagramPacket for holding the received
    packetDatagramPacket packet new
    DatagramPacket(buf, buf.length)
  • Wait for the packetsocket.receive(packet)
  • Get data from the received packetString message
    new String(packet.getData()).trim()

9
3. Socket Programming Using Datagram
  • Programming steps for sending data packet
  • Create DatagramSocket for sending
    packetDatagramSocket socket new
    DatagramSocket()
  • Create a byte array that holds the data to be
    sentbyte buf message.getBytes()
  • Create DatagramPacket with the target IP
    portDatagramPacket packet new
    DatagramPacket(buf, buf.length, targetIP, 2000)
  • Send the packetsocket.send(packet)
  • Close the socketsocket.close()

10
THE END
Write a Comment
User Comments (0)
About PowerShow.com