0% found this document useful (0 votes)
5 views18 pages

Socket Programming in Java 15.3.24

The document provides an overview of socket programming in Java, focusing on interprocess communication (IPC) and the Socket API. It explains the types of sockets, including connectionless (UDP) and connection-oriented (TCP) sockets, and details the classes used for datagram and stream-mode sockets. Additionally, it includes code examples for implementing server and client programs that communicate using these socket types.

Uploaded by

Patrick Kilonzo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Socket Programming in Java 15.3.24

The document provides an overview of socket programming in Java, focusing on interprocess communication (IPC) and the Socket API. It explains the types of sockets, including connectionless (UDP) and connection-oriented (TCP) sockets, and details the classes used for datagram and stream-mode sockets. Additionally, it includes code examples for implementing server and client programs that communicate using these socket types.

Uploaded by

Patrick Kilonzo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Socket Programming in Java

1
Introduction
• Interprocess communication (IPC) is the
backbone of distributed computing. Processes
are runtime representations of a program.
• Two or more processes engage in a protocol –
a set of rules to be observed by the
participants for data communication.
• A process can be a sender at some instant of
the communication and can be a receiver of
the data at another instant of the
communication.
• When data is sent from one process to
another single process, the communication is
said to be unicast.
• When data is sent from one process to more
than one process at the same time, the
communication is said to be multicast

2
• An IPC application programming interface • A process wishing to communicate with
(API) abstracts the details and intricacies another process must create an instance
of the system-level facilities and allows or instantiate a socket.
the programmer to concentrate on the • Two processes wishing to communicate
application logic. can instantiate sockets and then issue
• The Socket API is a low-level operations provided by the API to send
programming facility for implementing and receive data.
IPC. The upper-layer facilities are built on • A socket is uniquely identified by the IP
top of the operations provided by the address of the machine and the port
Socket API. number at which the socket is opened
• The Socket API was originally provided as (i.e. bound to)
part of the Berkeley UNIX OS, but has
been later ported to all operating systems
including Sun Solaris and Windows
systems.

3
Types of Sockets
• The User Datagram Protocol • For example, if process 1 on host
(UDP) transports packets in a A sends datagrams m1 and m2
connectionless manner . successively to process 2 on host
• In a connectionless B, the datagrams may be m P1
communication, each data P2 m P1 P3 m P2 P4 m
packet (also called datagram) is transported on the network
addressed and routed through different routes and
individually and may arrive at may arrive at the destination in
the receiver in any order. any of the two orders: m1, m2 or
m2, m1.

4
• The Transmission Control Protocol • A socket programming construct can
(TCP) is connection-oriented and use either UDP or TCP transport
transports a stream of data over a protocols.
logical connection established • Sockets that use UDP for transport of
between the sender and the receiver. packets are called “datagram” sockets
• As a result, data sent from a sender to and sockets that use TCP for transport
a receiver is guaranteed to be are called “stream” sockets.
received in the order they were sent.
• In the above example, messages m1
and m2 are delivered to process 2 on
host B in the same order they were
sent from process 1 on host A.

5
Connectionless Datagram Socket
• In Java, two classes are provided
for the datagram socket API:
➢ The DatagramSocket class for the
sockets
➢The DatagramPacket class for the
packets exchanged.
• A process wishing to send or
receive data using the datagram
socket API must instantiate a
DatagramSocket object, which is
bound to a UDP port of the
machine and local to the process.

6
7
8
datagram receiver datagram sender
([Link]) program ([Link]) program

import [Link].*;
Import [Link].*;

9
Connection-Oriented (Stream-Mode) Socket
• Data is inserted into the stream by a
sender process usually called the
server and is extracted from the
stream by the receiver process usually
called the client.
• The server process establishes a
connection socket and then listens for
connection requests from other
processes.
• Connection requests are accepted one
at a time. When the connection
request is accepted, a data socket is
created using which the server
process can write or read from/to the
data stream

10
11
Code for the Connection Server Program to Respond to a Connecting Client

12
Code for the Connection Client Program to Connect to a Server Program
import [Link].*;

13
Program to Send Objects of User-defined Classes using Stream-mode Sockets

• An important requirement of classes whose objects needs to be


transmitted across sockets is that these classes should implement the
Serializable interface defined in the [Link]. package.

14
Code for the Employee Class, Implementing the Serializable Interface

Import [Link].*;

15
Client Program to Send an Object of User-defined Class across Stream-mode Socket

16
Server Program to Receive an Object of User-defined Class across a Stream-mode Socket

17
Screenshots of the Client-Server Program to Send and Receive
Object of User defined Class across Stream-mode Sockets1818

18

You might also like