OpenLab 1 Socket Programming openlabworld@gmail.com
OpenLab 2 Socket Programming
OpenLab 3 TCP/IP Beginning
OpenLab 4 TCP/IP ModelEncapsulation Decapsulation
OpenLab 5 Socket Description
OpenLab 6 Sockets ● Defination - An interface between an application process and transport layer – The application process can send/receive messages to/from another application process (local or remote)via a socket ● Types of Sockets – TCP/Stream Socket – UDP/Datagram Socket – Raw Socket ● No Sockets – No communication
OpenLab 7 Socket Programming using JAVA ● I/O Stream ● Reader/Writer ● Create Socket (Class) – TCP Socket ● Server ServerSocket ● Client Socket – UDP Socket ● DatagramSocket
OpenLab 8 I/O Stream *https://docs.oracle.com/javase/tutorial/essential/io/streams.html OutputStream InputStream Write Read - Input Source - Output Destination A stream can represent many different kinds of sources & destinations - - Files - Devices - Programs - Memory arrays
OpenLab 9 Classification of Streams ● Byte Stream – Data in the form of individual bytes ● Text Strem – Data as character of each 2 bytes
OpenLab 10 Byte Strem ● Byte Stream – Data in the form of individual bytes ● InputStrem – DataInputStream – FileInputStream – BufferedInputStream – . . . ● OutputStream – DataOutputStream – FileOutputStream – BufferedOutputStream – . . .
OpenLab 11 Text Stream ● Text Stream – Data as character of each 2 bytes ● Reader – FileReader – BufferedReader ● Writer – FileWriter – BufferedWriter
OpenLab 12 Steps to create Socket(Server/Client) ● TCP Server Socket – Step-1 Create ServerSocket Object – Step-2 Wait for the client request – Step-3 Create I/O streams for communication to the client. – Step-4 Perform communication with client – Step-5 Close Socket ● TCP Client Socket – Step-1 Create Socket Object – Step-2 Create I/O streams for communication with the server – Step-3 Perform I/O or communication with the server – Step-4 Close Socket
OpenLab 13 Create TCP Socket (Connection Oriented) socket() connect() bind() accept() send() recv() listen() socket() send() recv() Server Client close() close()
OpenLab 14 Working of TCP Socket
OpenLab 15 Scenario 2 ● Two BEST friends , they want to chat to each using their computer and their fathers uninstalled messanger from their computers Ganesh Mohan
OpenLab 16 Case-1 ● They decided to create a STREAM/TCP Socket ● One way Communication I/O Stream Client Socket MOHAN Server Socket GANESH `
OpenLab 17 Case-2 ● They decided to create a STREAM/TCP Socket ● Two way Communication I/O Stream Client Socket MOHAN Server Socket GANESH `
OpenLab 18 Steps to Create UDP Socket ● UDP Server/Client – Step-1 Create DatagramSocket object – Step-2 Create buffer for tmp storage – Step-3 Store/Reterive data in/from buffer – Step-4 Create DatagramPacket – Step-5 send/receive DatagramPacket
OpenLab 19 Create UDP Socket [Connection less] socket() bind() recvfrom( ) sendto() socket() recvfrom( ) sendto() ClientServer close() close()
OpenLab 20 Case-1 ● They decided to create a DATAGRAM/UDP Socket ● One way Communication Client Socket MOHAN Server Socket GANESH Datagram Packet
OpenLab 21 Case-2 ● They decided to create a DATAGRAM/UDP Socket ● Two way Communication Client Socket MOHAN Server Socket GANESH Datagram Packet Datagram Packet
OpenLab 22 To be continued...

Socket Programming using Java

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
    OpenLab 6 Sockets ● Defination- An interface between an application process and transport layer – The application process can send/receive messages to/from another application process (local or remote)via a socket ● Types of Sockets – TCP/Stream Socket – UDP/Datagram Socket – Raw Socket ● No Sockets – No communication
  • 7.
    OpenLab 7 Socket Programming usingJAVA ● I/O Stream ● Reader/Writer ● Create Socket (Class) – TCP Socket ● Server ServerSocket ● Client Socket – UDP Socket ● DatagramSocket
  • 8.
    OpenLab 8 I/O Stream *https://docs.oracle.com/javase/tutorial/essential/io/streams.html OutputStream InputStream Write Read -Input Source - Output Destination A stream can represent many different kinds of sources & destinations - - Files - Devices - Programs - Memory arrays
  • 9.
    OpenLab 9 Classification ofStreams ● Byte Stream – Data in the form of individual bytes ● Text Strem – Data as character of each 2 bytes
  • 10.
    OpenLab 10 Byte Strem ●Byte Stream – Data in the form of individual bytes ● InputStrem – DataInputStream – FileInputStream – BufferedInputStream – . . . ● OutputStream – DataOutputStream – FileOutputStream – BufferedOutputStream – . . .
  • 11.
    OpenLab 11 Text Stream ●Text Stream – Data as character of each 2 bytes ● Reader – FileReader – BufferedReader ● Writer – FileWriter – BufferedWriter
  • 12.
    OpenLab 12 Steps tocreate Socket(Server/Client) ● TCP Server Socket – Step-1 Create ServerSocket Object – Step-2 Wait for the client request – Step-3 Create I/O streams for communication to the client. – Step-4 Perform communication with client – Step-5 Close Socket ● TCP Client Socket – Step-1 Create Socket Object – Step-2 Create I/O streams for communication with the server – Step-3 Perform I/O or communication with the server – Step-4 Close Socket
  • 13.
    OpenLab 13 Create TCPSocket (Connection Oriented) socket() connect() bind() accept() send() recv() listen() socket() send() recv() Server Client close() close()
  • 14.
  • 15.
    OpenLab 15 Scenario 2 ●Two BEST friends , they want to chat to each using their computer and their fathers uninstalled messanger from their computers Ganesh Mohan
  • 16.
    OpenLab 16 Case-1 ● Theydecided to create a STREAM/TCP Socket ● One way Communication I/O Stream Client Socket MOHAN Server Socket GANESH `
  • 17.
    OpenLab 17 Case-2 ● Theydecided to create a STREAM/TCP Socket ● Two way Communication I/O Stream Client Socket MOHAN Server Socket GANESH `
  • 18.
    OpenLab 18 Steps toCreate UDP Socket ● UDP Server/Client – Step-1 Create DatagramSocket object – Step-2 Create buffer for tmp storage – Step-3 Store/Reterive data in/from buffer – Step-4 Create DatagramPacket – Step-5 send/receive DatagramPacket
  • 19.
    OpenLab 19 Create UDPSocket [Connection less] socket() bind() recvfrom( ) sendto() socket() recvfrom( ) sendto() ClientServer close() close()
  • 20.
    OpenLab 20 Case-1 ● Theydecided to create a DATAGRAM/UDP Socket ● One way Communication Client Socket MOHAN Server Socket GANESH Datagram Packet
  • 21.
    OpenLab 21 Case-2 ● Theydecided to create a DATAGRAM/UDP Socket ● Two way Communication Client Socket MOHAN Server Socket GANESH Datagram Packet Datagram Packet
  • 22.
    OpenLab 22 To becontinued...