Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Socket Programming

Transport layer comprises of two types of protocols: TCP and UDP


TCP is connection-oriented protocol that provides reliable flow of data. Example
applications are HTTP, FTP and Telnet
UDP is a protocol that sends independent packets of data called datagrams. No
guarantee of arrival.

TCP and UDP protocols use ports to map incoming data to a particular process. Port is
represented by a positive (16 bit) integer value. (Logical access number – port)
Port no > =1024
What is socket?
A socket is one endpoint of a two-way communication link between two programs
running on the network.
Classes which are available in java.net package
Socket - The Socket class can be used to create a socket
ServerSocket - The ServerSocket class can be used to create a server socket. This
object is used to establish communication with the clients.

DatagramSocket - Reads text from a character-input stream, buffering characters so


as to provide for the efficient reading of characters, arrays, and lines.
InetAddress - The InetAddress class provides methods to resolve host names to their
IP addresses and vice versa.
DatagramPacket - to implement a connectionless packet delivery service. Each
message is routed from one machine to another based solely on information
contained within that packet. Multiple packets sent from one machine to another
might be routed differently,
Other classes which are used in socket programming
DataInputStream - . A data input stream lets an application read primitive Java data
types from an underlying input stream in a machine-independent way.
DataOutputStream - A data output stream lets an application write primitive Java
data types to an output stream in a portable way.
BufferedReader - Reads text from a character-input stream, buffering characters so as
to provide for the efficient reading of characters, arrays, and lines.

Socket and ServerSocket classes are used for connection-oriented socket


programming and DatagramSocket and DatagramPacket classes are used for
connection-less socket programming.
The client in socket programming must know two information:
IP Address of Server, and
Port number.

Socket class
A socket is simply an endpoint for communications between the machines. The
Socket class can be used to create a socket.

ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to
establish communication with the clients.

You might also like