SlideShare a Scribd company logo
Network Programming 
with Java
public static void main(String[] args) throws 
UnknownHostException { 
InetAddress localAddress = InetAddress.getLocalHost(); 
System.out.println(localAddress.getHostAddress()); 
} 
public static void main(String[] args) throws 
UnknownHostException { 
InetAddress googleAddress = 
InetAddress.getByName("www.google.com"); 
System.out.println(googleAddress.getHostAddress()); 
}
TCP/IP Server: That(a program or a process) listens to a TCP 
port. 
Client: What (a program or a process) initiates the connection 
Server is represented by java.net.ServerSocket 
Client is represented by java.net.Socket 
int portNumber = 7896; // TCP port Number 
ServerSocket server = new ServerSocket(portNumber);// 
listen on port 
Socket socket = server.accept(); // Blocks until 
connection made 
String destIP = "172.17.20.123"; // Target Host IPv4 Address 
int portNumber = 7896; // TCP port Number 
Socket socket = new Socket(destIP, portNumber);// connect
Stay & while connected… 
Server Client 
Wait for 
connection 
Server’s Output/ Client’s Input 
Client’s Output/ Server’s Input
Java I/O 
Some important OutputStream child: 
 DataOutputStream 
FileOutputStream 
ByteArrayOutputStream
Java I/O 
Some important InputStream child: 
DataInputStream 
FileInputStream 
ByteArrayInputStream 
Read an input integer Example
File Handling 
if(false==dir.exists() || false==dir.isDirectory()){ 
dir.mkdirs(); 
}
File Handling 
5. Open an OutputStream to the file: 
6. Write the raw bytes to the OutputStream, flush and close it
Write to a File : Second approach 
DataOutputStream provides a wrapper over an OutputStream to 
write primitive data types, not just byte[]
Read from a File 
Read byte-by-byte 
Read Bytes chunk-wise : Faster approach

More Related Content

PDF
Asynchronous, Event-driven Network Application Development with Netty
Ersin Er
 
PPTX
Java 1
VidyaVarshini3
 
PPTX
Network programming using python
Ali Nezhad
 
PDF
Netty: asynchronous data transfer
Victor Cherkassky
 
DOCX
Client server part 12
fadlihulopi
 
PDF
Statying Alive - Online and OFfline
Erik Hellman
 
PDF
Netty from the trenches
Jordi Gerona
 
PPT
Pemrograman Jaringan
belajarkomputer
 
Asynchronous, Event-driven Network Application Development with Netty
Ersin Er
 
Network programming using python
Ali Nezhad
 
Netty: asynchronous data transfer
Victor Cherkassky
 
Client server part 12
fadlihulopi
 
Statying Alive - Online and OFfline
Erik Hellman
 
Netty from the trenches
Jordi Gerona
 
Pemrograman Jaringan
belajarkomputer
 

What's hot (20)

DOCX
Chatting dengan beberapa pc laptop
yayaria
 
PDF
Anaysing your logs with docker and elk
melvin louwerse
 
PDF
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 
DOCX
network programing lab file ,
AAlha PaiKra
 
PDF
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
DOCX
Assignment Server, Client Application
Shahzeb Pirzada
 
PPT
Web
googli
 
PDF
I know what your packet did last hop using packet histories to troubleshoot...
承達 蔡
 
PPTX
NodeJs
Orkhan Muradov
 
PDF
Monitoring infrastructure with prometheus
Shahnawaz Saifi
 
PPT
Java Socket Programming
Vipin Yadav
 
PDF
Altitude NY 2018: Programming the edge workshop
Fastly
 
PDF
Reactive server with netty
Dmitriy Dumanskiy
 
ZIP
iPhone and Rails integration
Paul Ardeleanu
 
PPTX
Java socket presentation
Zahidul Islam Razu
 
PDF
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 
PDF
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
Zabbix
 
PDF
Infrastructure & System Monitoring using Prometheus
Marco Pas
 
PPT
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 
KEY
Non blocking io with netty
Zauber
 
Chatting dengan beberapa pc laptop
yayaria
 
Anaysing your logs with docker and elk
melvin louwerse
 
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 
network programing lab file ,
AAlha PaiKra
 
C# Advanced L08-Networking+WCF
Mohammad Shaker
 
Assignment Server, Client Application
Shahzeb Pirzada
 
Web
googli
 
I know what your packet did last hop using packet histories to troubleshoot...
承達 蔡
 
Monitoring infrastructure with prometheus
Shahnawaz Saifi
 
Java Socket Programming
Vipin Yadav
 
Altitude NY 2018: Programming the edge workshop
Fastly
 
Reactive server with netty
Dmitriy Dumanskiy
 
iPhone and Rails integration
Paul Ardeleanu
 
Java socket presentation
Zahidul Islam Razu
 
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
Zabbix
 
Infrastructure & System Monitoring using Prometheus
Marco Pas
 
Monitoring using Prometheus and Grafana
Arvind Kumar G.S
 
Non blocking io with netty
Zauber
 
Ad

Viewers also liked (6)

PPSX
Xmpp and java
Soham Sengupta
 
PPS
Sohamsg ajax
Soham Sengupta
 
PDF
Gordillo rrhh capitulo xiii
Integridad Política
 
PDF
Jabber/XMPP
Florian Holzhauer
 
ODP
XMPP - Real Time Communication
Dominic Lüchinger
 
PPT
Kundenservice 2.0 updated
Heike Simmet
 
Xmpp and java
Soham Sengupta
 
Sohamsg ajax
Soham Sengupta
 
Gordillo rrhh capitulo xiii
Integridad Política
 
Jabber/XMPP
Florian Holzhauer
 
XMPP - Real Time Communication
Dominic Lüchinger
 
Kundenservice 2.0 updated
Heike Simmet
 
Ad

Similar to Network programming1 (20)

PPT
Socket Programming
Sivadon Chaisiri
 
PPTX
Advance Java-Network Programming
ashok hirpara
 
PPT
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
PPT
Networking & Socket Programming In Java
Ankur Agrawal
 
PPTX
Network programming in java - PPT
kamal kotecha
 
DOCX
Lab manual cn-2012-13
Sasi Kala
 
PPT
Socket Programming it-slideshares.blogspot.com
phanleson
 
PDF
java sockets
Enam Ahmed Shahaz
 
PDF
Lecture6
vantinhkhuc
 
PDF
Laporan multiclient chatting client server
trilestari08
 
PPT
Network
phanleson
 
PDF
ikh331-06-distributed-programming
Anung Ariwibowo
 
PPT
Java Networking
Ankit Desai
 
PPTX
Advanced Java Programming: Introduction and Overview of Java Networking 1. In...
KuntalVasoya
 
PPTX
3160707_AJava_GTU_Study_Material_Presentations_Unit-1_16032021121225PM.pptx
vasishtharishi07
 
DOCX
Winform
quocphu199
 
ODP
Networking and Data Access with Eqela
jobandesther
 
PPT
Unit 8 Java
arnold 7490
 
PDF
Ipc
deepakittude
 
PPT
Ppt of socket
Amandeep Kaur
 
Socket Programming
Sivadon Chaisiri
 
Advance Java-Network Programming
ashok hirpara
 
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
Networking & Socket Programming In Java
Ankur Agrawal
 
Network programming in java - PPT
kamal kotecha
 
Lab manual cn-2012-13
Sasi Kala
 
Socket Programming it-slideshares.blogspot.com
phanleson
 
java sockets
Enam Ahmed Shahaz
 
Lecture6
vantinhkhuc
 
Laporan multiclient chatting client server
trilestari08
 
Network
phanleson
 
ikh331-06-distributed-programming
Anung Ariwibowo
 
Java Networking
Ankit Desai
 
Advanced Java Programming: Introduction and Overview of Java Networking 1. In...
KuntalVasoya
 
3160707_AJava_GTU_Study_Material_Presentations_Unit-1_16032021121225PM.pptx
vasishtharishi07
 
Winform
quocphu199
 
Networking and Data Access with Eqela
jobandesther
 
Unit 8 Java
arnold 7490
 
Ppt of socket
Amandeep Kaur
 

More from Soham Sengupta (20)

PPTX
Spring method-level-secuirty
Soham Sengupta
 
PPTX
Spring security mvc-1
Soham Sengupta
 
PDF
JavaScript event handling assignment
Soham Sengupta
 
PDF
Networking assignment 2
Soham Sengupta
 
PDF
Networking assignment 1
Soham Sengupta
 
PPT
Sohams cryptography basics
Soham Sengupta
 
PPT
JSR-82 Bluetooth tutorial
Soham Sengupta
 
PPT
Core java day2
Soham Sengupta
 
PPT
Core java day1
Soham Sengupta
 
PPT
Core java day4
Soham Sengupta
 
PPT
Core java day5
Soham Sengupta
 
PPT
Exceptions
Soham Sengupta
 
PPSX
Java.lang.object
Soham Sengupta
 
PPTX
Soham web security
Soham Sengupta
 
PPTX
Html tables and_javascript
Soham Sengupta
 
PPT
Html javascript
Soham Sengupta
 
PPT
Java script
Soham Sengupta
 
PPT
Dhtml
Soham Sengupta
 
PPSX
Cookies and session
Soham Sengupta
 
Spring method-level-secuirty
Soham Sengupta
 
Spring security mvc-1
Soham Sengupta
 
JavaScript event handling assignment
Soham Sengupta
 
Networking assignment 2
Soham Sengupta
 
Networking assignment 1
Soham Sengupta
 
Sohams cryptography basics
Soham Sengupta
 
JSR-82 Bluetooth tutorial
Soham Sengupta
 
Core java day2
Soham Sengupta
 
Core java day1
Soham Sengupta
 
Core java day4
Soham Sengupta
 
Core java day5
Soham Sengupta
 
Exceptions
Soham Sengupta
 
Java.lang.object
Soham Sengupta
 
Soham web security
Soham Sengupta
 
Html tables and_javascript
Soham Sengupta
 
Html javascript
Soham Sengupta
 
Java script
Soham Sengupta
 
Cookies and session
Soham Sengupta
 

Recently uploaded (20)

DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
PDF
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
This slide provides an overview Technology
mineshkharadi333
 
The Power of IoT Sensor Integration in Smart Infrastructure and Automation.pptx
Rejig Digital
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
Software Development Methodologies in 2025
KodekX
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
AVTRON Technologies LLC
 
NewMind AI Monthly Chronicles - July 2025
NewMind AI
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
CIFDAQ
 
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
AbdullahSani29
 

Network programming1

  • 2. public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost(); System.out.println(localAddress.getHostAddress()); } public static void main(String[] args) throws UnknownHostException { InetAddress googleAddress = InetAddress.getByName("www.google.com"); System.out.println(googleAddress.getHostAddress()); }
  • 3. TCP/IP Server: That(a program or a process) listens to a TCP port. Client: What (a program or a process) initiates the connection Server is represented by java.net.ServerSocket Client is represented by java.net.Socket int portNumber = 7896; // TCP port Number ServerSocket server = new ServerSocket(portNumber);// listen on port Socket socket = server.accept(); // Blocks until connection made String destIP = "172.17.20.123"; // Target Host IPv4 Address int portNumber = 7896; // TCP port Number Socket socket = new Socket(destIP, portNumber);// connect
  • 4. Stay & while connected… Server Client Wait for connection Server’s Output/ Client’s Input Client’s Output/ Server’s Input
  • 5. Java I/O Some important OutputStream child:  DataOutputStream FileOutputStream ByteArrayOutputStream
  • 6. Java I/O Some important InputStream child: DataInputStream FileInputStream ByteArrayInputStream Read an input integer Example
  • 7. File Handling if(false==dir.exists() || false==dir.isDirectory()){ dir.mkdirs(); }
  • 8. File Handling 5. Open an OutputStream to the file: 6. Write the raw bytes to the OutputStream, flush and close it
  • 9. Write to a File : Second approach DataOutputStream provides a wrapper over an OutputStream to write primitive data types, not just byte[]
  • 10. Read from a File Read byte-by-byte Read Bytes chunk-wise : Faster approach