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

What's hot

Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
yayaria
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 

What's hot (20)

Chatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptopChatting dengan beberapa pc laptop
Chatting dengan beberapa pc laptop
 
Anaysing your logs with docker and elk
Anaysing your logs with docker and elkAnaysing your logs with docker and elk
Anaysing your logs with docker and elk
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
C# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCFC# Advanced L08-Networking+WCF
C# Advanced L08-Networking+WCF
 
Assignment Server, Client Application
Assignment Server, Client ApplicationAssignment Server, Client Application
Assignment Server, Client Application
 
Web
WebWeb
Web
 
I know what your packet did last hop using packet histories to troubleshoot...
I know what your packet did last hop  using  packet histories to troubleshoot...I know what your packet did last hop  using  packet histories to troubleshoot...
I know what your packet did last hop using packet histories to troubleshoot...
 
NodeJs
NodeJsNodeJs
NodeJs
 
Monitoring infrastructure with prometheus
Monitoring infrastructure with prometheusMonitoring infrastructure with prometheus
Monitoring infrastructure with prometheus
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Altitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshopAltitude NY 2018: Programming the edge workshop
Altitude NY 2018: Programming the edge workshop
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 
iPhone and Rails integration
iPhone and Rails integrationiPhone and Rails integration
iPhone and Rails integration
 
Java socket presentation
Java socket presentation Java socket presentation
Java socket presentation
 
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, EverAltitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
 
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
Rihards Olups - Encrypting Daemon Traffic With Zabbix 3.0
 
Infrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using PrometheusInfrastructure & System Monitoring using Prometheus
Infrastructure & System Monitoring using Prometheus
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
 
Non blocking io with netty
Non blocking io with nettyNon blocking io with netty
Non blocking io with netty
 

Viewers also liked (6)

Xmpp and java
Xmpp and javaXmpp and java
Xmpp and java
 
Sohamsg ajax
Sohamsg ajaxSohamsg ajax
Sohamsg ajax
 
Gordillo rrhh capitulo xiii
Gordillo rrhh capitulo xiiiGordillo rrhh capitulo xiii
Gordillo rrhh capitulo xiii
 
Jabber/XMPP
Jabber/XMPPJabber/XMPP
Jabber/XMPP
 
XMPP - Real Time Communication
XMPP - Real Time CommunicationXMPP - Real Time Communication
XMPP - Real Time Communication
 
Kundenservice 2.0 updated
Kundenservice 2.0 updatedKundenservice 2.0 updated
Kundenservice 2.0 updated
 

Similar to Network programming1

Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
Sasi Kala
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client server
trilestari08
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
Anung Ariwibowo
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
jobandesther
 

Similar to Network programming1 (20)

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
 
Networking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in programNetworking.ppt(client/server, socket) uses in program
Networking.ppt(client/server, socket) uses in program
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
java sockets
 java sockets java sockets
java sockets
 
Lecture6
Lecture6Lecture6
Lecture6
 
Laporan multiclient chatting client server
Laporan multiclient chatting client serverLaporan multiclient chatting client server
Laporan multiclient chatting client server
 
Network
NetworkNetwork
Network
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Winform
WinformWinform
Winform
 
Networking and Data Access with Eqela
Networking and Data Access with EqelaNetworking and Data Access with Eqela
Networking and Data Access with Eqela
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Ipc
IpcIpc
Ipc
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
nw-lab_dns-server.pdf
nw-lab_dns-server.pdfnw-lab_dns-server.pdf
nw-lab_dns-server.pdf
 

More from Soham Sengupta

More from Soham Sengupta (20)

Spring method-level-secuirty
Spring method-level-secuirtySpring method-level-secuirty
Spring method-level-secuirty
 
Spring security mvc-1
Spring security mvc-1Spring security mvc-1
Spring security mvc-1
 
JavaScript event handling assignment
JavaScript  event handling assignment JavaScript  event handling assignment
JavaScript event handling assignment
 
Networking assignment 2
Networking assignment 2Networking assignment 2
Networking assignment 2
 
Networking assignment 1
Networking assignment 1Networking assignment 1
Networking assignment 1
 
Sohams cryptography basics
Sohams cryptography basicsSohams cryptography basics
Sohams cryptography basics
 
JSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorialJSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorial
 
Core java day2
Core java day2Core java day2
Core java day2
 
Core java day1
Core java day1Core java day1
Core java day1
 
Core java day4
Core java day4Core java day4
Core java day4
 
Core java day5
Core java day5Core java day5
Core java day5
 
Exceptions
ExceptionsExceptions
Exceptions
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
Jsp1
Jsp1Jsp1
Jsp1
 
Soham web security
Soham web securitySoham web security
Soham web security
 
Html tables and_javascript
Html tables and_javascriptHtml tables and_javascript
Html tables and_javascript
 
Html javascript
Html javascriptHtml javascript
Html javascript
 
Java script
Java scriptJava script
Java script
 
Dhtml
DhtmlDhtml
Dhtml
 
Cookies and session
Cookies and sessionCookies and session
Cookies and session
 

Recently uploaded

Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
The architecture of Generative AI for enterprises.pdf
The architecture of Generative AI for enterprises.pdfThe architecture of Generative AI for enterprises.pdf
The architecture of Generative AI for enterprises.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

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