0% found this document useful (0 votes)
16 views7 pages

Java Networking and Socket Programming

Java networking is facilitated through the java.net package, allowing developers to create networked applications using sockets, URLs, and protocols like TCP/IP and HTTP. Key concepts include IP addresses, port numbers, and various classes such as InetAddress, URL, Socket, and ServerSocket for managing connections. Client/server programming involves establishing connections, exchanging data, and closing connections, with specific classes used for both connection-oriented and connection-less communication.

Uploaded by

Shaad Tanwar
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)
16 views7 pages

Java Networking and Socket Programming

Java networking is facilitated through the java.net package, allowing developers to create networked applications using sockets, URLs, and protocols like TCP/IP and HTTP. Key concepts include IP addresses, port numbers, and various classes such as InetAddress, URL, Socket, and ServerSocket for managing connections. Client/server programming involves establishing connections, exchanging data, and closing connections, with specific classes used for both connection-oriented and connection-less communication.

Uploaded by

Shaad Tanwar
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

Java Networking

Java provides extensive support for networking through its [Link] package,
which allows developers to create and manage networked applications with
ease. Java networking enables communication between machines, facilitating
data sharing and distributed application development.

Dr. Niky Jain, ISTAR College 1


1. Introduction to Networking
Networking refers to the practice of connecting multiple devices (computers,
servers, or nodes) to share resources and data. In Java, networking capabilities
are implemented using sockets, URLs, and protocols such as TCP/IP and HTTP.
Key Concepts in Networking:
 IP Address: A unique identifier for a device on a network.
 Port Number: A number that specifies a particular process or service on a
device.
 Protocol: A set of rules for communication (e.g., TCP, UDP, HTTP).
Java Networking Classes:

 InetAddress: Represents an IP address.


 URL: Represents a Uniform Resource Locator for accessing web resources.
 URLConnection: Handles communication with the resource pointed to by a
URL.
 Socket: Used for TCP communication between a client and a server.
 ServerSocket: Listens for incoming TCP connections from clients.
 DatagramSocket: UsedDr.
for UDP
Niky communication.
Jain, ISTAR College 2
2. Introduction to Client/Server Programming
In client/server programming, two main components interact:
 Client: Sends requests to the server and processes responses.
 Server: Listens for client requests and provides responses.

Key Steps in Client/Server Communication:


1) Establish a connection:
 The server listens on a specific port.
 The client connects to the server using its IP address and port.
2) Exchange data:
 Use input/output streams to send and receive data.
3) Close the connection:
 Both client and server close their sockets after communication is
complete.

Dr. Niky Jain, ISTAR College 3


3. Java Socket Programming
 Java Socket programming is used for communication between the
applications running on different JRE.
 Java Socket programming can be connection-oriented or connection-less.
 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:

1) IP Address of Server, and


2) Port number.

Dr. Niky Jain, ISTAR College 4


Here, we are discussing of one-way client and server communication. In this
application, client sends a message to the server, server reads the message and
prints it. Here, two classes are being used: Socket and ServerSocket. The
Socket class is used to communicate client and server. Through this class, we
can read and write message. The ServerSocket class is used at server-side. The
accept() method of ServerSocket class blocks the console until the client is
connected. After the successful connection of client, it returns the instance of
Socket at server-side

Dr. Niky Jain, ISTAR College 5


Dr. Niky Jain, ISTAR College 6
Socket class
A socket is simply an endpoint for communications between the machines. The
Socket class can be used to create a socket.
Important methods

Method Description

returns the InputStream attached with this


1) public InputStream getInputStream()
socket.
returns the OutputStream attached with
2) public OutputStream getOutputStream()
this socket.
3) public synchronized void close() closes this socket

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

Method Description

returns the socket and establish a


1) public Socket accept()
connection between server and client.
2) public synchronized void close() closes the server socket.
Dr. Niky Jain, ISTAR College 7

You might also like