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
9. OpenLab 9
Classification of Streams
● 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 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
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
● They decided to create a STREAM/TCP Socket
● One way Communication
I/O Stream
Client Socket
MOHAN Server Socket
GANESH
`
17. OpenLab 17
Case-2
● They decided to create a STREAM/TCP Socket
● Two way Communication
I/O Stream
Client Socket
MOHAN Server Socket
GANESH
`
20. OpenLab 20
Case-1
● They decided to create a DATAGRAM/UDP
Socket
● One way Communication
Client Socket
MOHAN Server Socket
GANESH
Datagram Packet
21. 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