Professional Documents
Culture Documents
Network Programming Section 4
Network Programming Section 4
Section 4: Socket
Part II: socket for servers
Firstly, lets discuss the layers of the TCP/IP protocol stack and their corresponding
Java modules. Then explain socket server with an example for TCP/IP protocol.
Application Layer: This layer includes protocols like HTTP, FTP, SMTP, etc.,
which are used by applications to communicate over the network. In Java, you
interact with application layer protocols using various classes like
HttpURLConnection for HTTP, FTPClient for FTP, etc.
Transport Layer: The transport layer provides end-to-end communication between
the communicating hosts and ensures data delivery. TCP (Transmission Control
Protocol) and UDP (User Datagram Protocol) are the most common protocols at this
layer. In Java, you use Socket and ServerSocket classes for TCP communication and
DatagramSocket and DatagramPacket classes for UDP communication.
Internet Layer: This layer is responsible for routing packets across different
networks. IP (Internet Protocol) is the primary protocol used at this layer. In Java,
you don't typically interact directly with this layer, as lower-level networking
operations are abstracted away by higher-level APIs.
Link Layer: The link layer deals with the physical connection between devices
and the transmission of data frames. It includes protocols like Ethernet, Wi-Fi, etc.
Java doesn't provide direct APIs for interacting with the link layer, as it's usually
handled by the operating system and network drivers.
Prepared by: Eng/ Basma Elshoky
ServerSocket
ServerSocket class is used to create a server-side socket that listens for incoming
client connections. Constructors & Methods:
Constructor Description
ServerSocket(int port) Creates a server socket bound to the specified port.
ServerSocket(int port, int Creates a server socket bound to the specified port, with a
backlog) specified backlog. The backlog is the maximum number
of pending connections the queue will hold.
ServerSocket(int port, int backlog, Creates a server socket bound to the specified port, with a
InetAddress bindAddr) specified backlog and network interface.
Prepared by: Eng/ Basma Elshoky
Method Description
bind(SocketAddress endpoint) Binds the server socket to a specific address (IP address
and port number).
close() Closes the server socket. Any pending client connections
will be terminated.
getInetAddress() Returns the address to which the socket is bound.
getLocalPort() Returns the port number on which this socket is listening.
setSoTimeout(int timeout) Sets the timeout in milliseconds for how long the accept()
method will block. If a connection is not established
within the timeout period, a SocketTimeoutException is
thrown.
getSoTimeout() Returns the timeout in milliseconds for how long the
accept() method will block.
setReuseAddress(boolean on) Sets the SO_REUSEADDR socket option. This allows
the socket to be bound even if there is a previous
connection in the TIME_WAIT state.
getReuseAddress() Returns whether the SO_REUSEADDR socket option is
enabled.
getChannel() Returns the server socket's ServerSocketChannel if
available.
setPerformancePreferences(int Sets performance preferences for connection time,
connectionTime, int latency, int latency, and bandwidth. This can influence the system's
bandwidth): choice of underlying implementation.
Prepared by: Eng/ Basma Elshoky
Additional classes:
InputStream and OutputStream: These classes represent streams for reading and
writing binary data. They are the fundamental building blocks for network streams.
Reader and Writer: These classes represent streams for reading and writing
character-based data. They are built on top of InputStream and OutputStream and
provide support for character encoding.
BufferedInputStream: This class also has a protected byte array named buf that
serves as a buffer. It does not declare any new methods of its own. It only overrides
methods from InputStream.
Example 1:
Checks for ports on the local machine by attempting to create ServerSocket objects
on them and seeing on which ports that fails. If you’re using Unix and are not
running as root, this program works only for ports 1,024 and above.
Prepared by: Eng/ Basma Elshoky
create a server socket on port 130 that would hold up to 100 incoming
connection requests in the queue, you would write
Homework Task:
Then apply CH02_VID07_Build Server Client Application in Java .”code with screen of
result from your PC”