SlideShare a Scribd company logo
1 of 15
Socket Programs using java.net package.
Java Network Programming
Lecture focuses on:
 TCP and UDP
 Network programming basics
 Identifying a machine
 Servers and Clients
 Ports and sockets
 Data transfer using sockets
The term network
programming refers to writing
programs that execute across
multiple devices (computers), in
which the devices are all connected
to each other using a network.
Client – Server Architecture
 programs running on client machines make
requests to a program (often called as server
program) running on a server machine. They
involve networking services provided by the
transport layer, which is part of the Internet
software stack, often called TCP/IP (Transport
Control Protocol/Internet Protocol) stack.
 TCP is a connection-oriented protocol that
provides a reliable flow of data between two
computers.
the Internet software stack
The transport layer comprises two
types of protocols, TCP and UDP.
TCP − TCP stands for Transmission
Control Protocol, which allows for
reliable communication between two
applications. TCP is typically used
over the Internet Protocol, which is
referred to as TCP/IP.
UDP − UDP stands for User
Datagram Protocol, a connection-less
protocol that allows for packets of
data to be transmitted between
applications.
The most widely used programming
interfaces for these protocols are
sockets.
java.net Package
 The classes in java.net: Java programs can use
TCP or UDP to communicate over the Internet.
The URL, URLConnection (for Web applications),
Socket, and ServerSocket (client-server
applications) classes all use TCP to communicate
over the network.
 The DatagramPacket, DatagramSocket, and
MulticastSocket classes are for use with UDP.
Sockets
Socket Programming in Java
What is a Socket?
 A socket is defined as an endpoint for
communication.
 It is a concatenation of IP address and port
 The socket 161.25.19.8:1625 refers to port
1625 on host 161.25.19.8
 Communication consists between a pair of
sockets.
 Considered a low-level form of communication
between distributed processes.
 Sockets allow only an unstructured stream of bytes
to be exchanged. It is the responsibility of the client
or server application to impose a structure on the
Server has
Server IP Address – by default localhost or 127.0.0.1
Port Number, use any integer >1024.
Programming for Sockets
Steps: For Server Side Programs
 Create a class by extending Thread
public class GreetingServer extends Thread {}
 Write a constructor to initialize value of ‘port’. In the
constructor create a Server Socket by using the port
number given by user. Wait for Client Request.
public GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port); // Opens a
Server Socket
serverSocket.setSoTimeout(20000); // Assigns wait
time
}
 When the request from client comes, create a new
Socket by accepting that request.
Socket server = serverSocket.accept();
 To Communicate with client create IO Streams. You
can read or write the data using DataInputStream and
DataOutputStream,
DataInputStream in = new
DataInputStream(server.getInputStream());
String str = in.readUTF();
DataOutputStream out = new
DataOutputStream(server.getOutputStream());
 In main(), create the Thread,
Thread t = new GreetingServer(port); //Start the
Steps for Client Side Program
 Create a Socket by giving ServerName and
Port number.
Socket client = new Socket(serverName,
port);
 Create Input and Output Streams.
OutputStream outToServer =
client.getOutputStream();
DataOutputStream out = new
DataOutputStream(outToServer);
InputStream inFromServer =
client.getInputStream();
DataInputStream in = new
DataInputStream(inFromServer);
 Read or Write Data using
Running the Programs
While Running the Programs
 Write Two Different Programs for Server and
Client.
 Compile both the programs by javac.
 Run Server Side Program. By using
java ProgName port number
 In another terminal / window run client program
java ProgName servername port number
Thank You

More Related Content

Similar to Java Network Programming.pptx

Similar to Java Network Programming.pptx (20)

Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
socket-programming.pptx
socket-programming.pptxsocket-programming.pptx
socket-programming.pptx
 
Lecture25
Lecture25Lecture25
Lecture25
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Chat server nitish nagar
Chat server nitish nagarChat server nitish nagar
Chat server nitish nagar
 
Networking
NetworkingNetworking
Networking
 
28 networking
28  networking28  networking
28 networking
 
Java networking
Java networkingJava networking
Java networking
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Client server project
Client server projectClient server project
Client server project
 
Multiuser chat application using java
Multiuser chat application using javaMultiuser chat application using java
Multiuser chat application using java
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
Socket
SocketSocket
Socket
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 

Java Network Programming.pptx

  • 1. Socket Programs using java.net package. Java Network Programming
  • 2. Lecture focuses on:  TCP and UDP  Network programming basics  Identifying a machine  Servers and Clients  Ports and sockets  Data transfer using sockets
  • 3. The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
  • 4. Client – Server Architecture  programs running on client machines make requests to a program (often called as server program) running on a server machine. They involve networking services provided by the transport layer, which is part of the Internet software stack, often called TCP/IP (Transport Control Protocol/Internet Protocol) stack.  TCP is a connection-oriented protocol that provides a reliable flow of data between two computers.
  • 5. the Internet software stack The transport layer comprises two types of protocols, TCP and UDP. TCP − TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP. UDP − UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications. The most widely used programming interfaces for these protocols are sockets.
  • 6. java.net Package  The classes in java.net: Java programs can use TCP or UDP to communicate over the Internet. The URL, URLConnection (for Web applications), Socket, and ServerSocket (client-server applications) classes all use TCP to communicate over the network.  The DatagramPacket, DatagramSocket, and MulticastSocket classes are for use with UDP.
  • 9. What is a Socket?  A socket is defined as an endpoint for communication.  It is a concatenation of IP address and port  The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8  Communication consists between a pair of sockets.  Considered a low-level form of communication between distributed processes.  Sockets allow only an unstructured stream of bytes to be exchanged. It is the responsibility of the client or server application to impose a structure on the
  • 10. Server has Server IP Address – by default localhost or 127.0.0.1 Port Number, use any integer >1024.
  • 11. Programming for Sockets Steps: For Server Side Programs  Create a class by extending Thread public class GreetingServer extends Thread {}  Write a constructor to initialize value of ‘port’. In the constructor create a Server Socket by using the port number given by user. Wait for Client Request. public GreetingServer(int port) throws IOException { serverSocket = new ServerSocket(port); // Opens a Server Socket serverSocket.setSoTimeout(20000); // Assigns wait time }
  • 12.  When the request from client comes, create a new Socket by accepting that request. Socket server = serverSocket.accept();  To Communicate with client create IO Streams. You can read or write the data using DataInputStream and DataOutputStream, DataInputStream in = new DataInputStream(server.getInputStream()); String str = in.readUTF(); DataOutputStream out = new DataOutputStream(server.getOutputStream());  In main(), create the Thread, Thread t = new GreetingServer(port); //Start the
  • 13. Steps for Client Side Program  Create a Socket by giving ServerName and Port number. Socket client = new Socket(serverName, port);  Create Input and Output Streams. OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); InputStream inFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer);  Read or Write Data using
  • 14. Running the Programs While Running the Programs  Write Two Different Programs for Server and Client.  Compile both the programs by javac.  Run Server Side Program. By using java ProgName port number  In another terminal / window run client program java ProgName servername port number