SlideShare a Scribd company logo
Module 07 – Java Networking
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Fundamental Java Programming
The Course Outline
Module 01 – Introduction to Java
Module 02 – Basic Java Programming
Module 03 – Control Flow and Exception Handling
Module 04 – Object Oriented in Java
Module 05 – Java Package and Access Control
Module 06 – Java File IO
Module 07 – Java Networking
Module 08 – Java Threading
Module 07 – Java Networking
• Basic Networking
• Network Port Numbers
• Java Socket
• Writing Server Socket
• Writing Client Socket
• Passing Object over Network
Basic Networking
TCP: The protocol for two applications which are want to communicate to
each other reliably, they establish a connection and send data back and
forth over that connection. This is analogous to making a telephone call.
UDP: The protocol that is not guaranteed between two applications on the
network. UDP is not connection-based like TCP. Rather, it sends
independent packets of data, called datagrams, from one application to
another. Sending datagrams is much like sending a letter through the
postal service: The order of delivery is not important and is not
guaranteed, and each message is independent of any other.
Network Port Numbers
IP Address is a unique number of computer network address for
both server and client.
Ports are identified by a 16-bit number (65536 ports), which TCP
and UDP use to deliver the data to the right application.
The first 1024 port numbers have been reserved by system root
user. The rest port numbers can be use by other system users.
Java Socket
A socket is one end-point of a two-way communication link
between two programs running on the network. Socket
classes are used to represent the connection between a
client program and a server program. The java.net package
provides two classes--Socket and ServerSocket--that
implement the client side of the connection and the server
side of the connection, respectively.
Simple Socket Server
package javanetworkingproject;
import java.net.*;
import java.io.*;
public class MyServer {
public static void main(String args[]) throws IOException {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1= s.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Send a string!
dos.writeUTF("Hi there สวัสดี");
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}
}
Simple Client Server
import java.net.*;
import java.io.*;
public class MyClient {
public static void main(String args[]) throws IOException {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("127.0.0.1",1234);
// Get result from server
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
Hi there สวัสดี
Object Passing over network
import java.io.Serializable;
public class Customer implements Serializable{
private String name;
private int collectedPoints;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCollectedPoints(int collectedPoints) {
this.collectedPoints = collectedPoints;
}
public int getCollectedPoints() {
return collectedPoints;
}
}
Server Socket Programming with Object Passing
package javanetworkingproject2;
import java.net.*;
import java.io.*;
public class MyServer2 {
public static void main(String[] args) throws Exception {
// Register service on port 1234
ServerSocket s = new ServerSocket(1234);
Socket s1 = s.accept(); // Wait and accept a connection
// Read input from client
ObjectInputStream objInputStream =
new ObjectInputStream(s1.getInputStream());
Customer cust1 = (Customer)objInputStream.readObject();
cust1.setCollectedPoints(3000);
// Write result to client
OutputStream s1out = s1.getOutputStream();
ObjectOutputStream objOutputStream = new ObjectOutputStream(s1out);
objOutputStream.writeObject(cust1);
// Close the connection, but not the server socket
objOutputStream.close();
s1out.close();
objInputStream.close();
s1.close();
}
}
Client Socket Programming with Object Passing
import java.net.*;
import java.io.*;
public class MyClient2 {
public static void main(String[] args) throws Exception {
// Open your connection to a server, at port 1234
Socket s1 = new Socket("127.0.0.1", 1234);
// Write object to the socket server
Customer cust1 = new Customer();
cust1.setName("Somchai");
ObjectOutputStream objOutputStream =
new ObjectOutputStream(s1.getOutputStream());
objOutputStream.writeObject(cust1);
// Get result from server
InputStream s1In = s1.getInputStream();
ObjectInputStream objInputStream = new ObjectInputStream(s1In);
Customer custResult = (Customer)objInputStream.readObject();
System.out.println(custResult.getName() + " has " +
custResult.getCollectedPoints() + " points");
// When done, just close the connection and exit
objInputStream.close();
s1In.close();
objOutputStream.close();
s1.close();
}
}
URL Reader
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL googleURL = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(
googleURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Danairat T.
Line ID: Danairat
FB: Danairat Thanabodithammachari
+668-1559-1446
Thank you

More Related Content

What's hot (20)

PPT
Network programming in Java
Tushar B Kute
 
PPT
java networking
Waheed Warraich
 
PPT
Networking Java Socket Programming
Mousmi Pawar
 
PDF
Java networking programs socket based
Mukesh Tekwani
 
PPTX
Java Networking
68SachinYadavSYCS
 
PPT
Java API: java.net.InetAddress
Sayak Sarkar
 
PPTX
Socket programming in Java (PPTX)
UC San Diego
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PDF
Networking in java, Advanced programming
Gera Paulos
 
PPT
Basic Networking in Java
suraj pandey
 
PPT
Network programming in Java
Tushar B Kute
 
PDF
Chap 1 Network Theory & Java Overview
Ministry of Higher Education
 
PPTX
Java 1
VidyaVarshini3
 
PDF
28 networking
Ravindra Rathore
 
PPT
Socket Programming
CEC Landran
 
PPTX
Java networking
ssuser3a47cb
 
PPT
Network Programming in Java
Tushar B Kute
 
PPT
Md13 networking
Rakesh Madugula
 
PPTX
Socket programming or network programming
Mmanan91
 
PPTX
Chapter 4
Ebisa Bekele
 
Network programming in Java
Tushar B Kute
 
java networking
Waheed Warraich
 
Networking Java Socket Programming
Mousmi Pawar
 
Java networking programs socket based
Mukesh Tekwani
 
Java Networking
68SachinYadavSYCS
 
Java API: java.net.InetAddress
Sayak Sarkar
 
Socket programming in Java (PPTX)
UC San Diego
 
Socket Programming - nitish nagar
Nitish Nagar
 
Networking in java, Advanced programming
Gera Paulos
 
Basic Networking in Java
suraj pandey
 
Network programming in Java
Tushar B Kute
 
Chap 1 Network Theory & Java Overview
Ministry of Higher Education
 
28 networking
Ravindra Rathore
 
Socket Programming
CEC Landran
 
Java networking
ssuser3a47cb
 
Network Programming in Java
Tushar B Kute
 
Md13 networking
Rakesh Madugula
 
Socket programming or network programming
Mmanan91
 
Chapter 4
Ebisa Bekele
 

Viewers also liked (18)

PPTX
Network programming in java - PPT
kamal kotecha
 
PPTX
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
PPT
Sockets
sivindia
 
PPTX
Stop guessing - make cashless payments a success
BSGAfrica
 
PDF
Jnp
hj43us
 
PPT
java packages
aptechsravan
 
PDF
Email Security Threats: IT Manager's Eyes Only
Topsec Technology
 
PDF
JEE Programming - 07 EJB Programming
Danairat Thanabodithammachari
 
PPTX
Geometric transformation 2d chapter 5
geethawilliam
 
PPT
Socket Programming Tutorial
Jignesh Patel
 
PPT
2 d transformations by amit kumar (maimt)
Amit Kapoor
 
PDF
Digital signatures
Ishwar Dayal
 
PPT
Lan chat system
Wipro
 
PPT
Digital signature
Hossain Md Shakhawat
 
PPT
Email
ARSD College
 
PPT
Digital Signature
saurav5884
 
PPT
Core java slides
Abhilash Nair
 
PPT
Electronic mail
Diwaker Pant
 
Network programming in java - PPT
kamal kotecha
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
Sockets
sivindia
 
Stop guessing - make cashless payments a success
BSGAfrica
 
Jnp
hj43us
 
java packages
aptechsravan
 
Email Security Threats: IT Manager's Eyes Only
Topsec Technology
 
JEE Programming - 07 EJB Programming
Danairat Thanabodithammachari
 
Geometric transformation 2d chapter 5
geethawilliam
 
Socket Programming Tutorial
Jignesh Patel
 
2 d transformations by amit kumar (maimt)
Amit Kapoor
 
Digital signatures
Ishwar Dayal
 
Lan chat system
Wipro
 
Digital signature
Hossain Md Shakhawat
 
Digital Signature
saurav5884
 
Core java slides
Abhilash Nair
 
Electronic mail
Diwaker Pant
 
Ad

Similar to Java Programming - 07 java networking (20)

PPT
Advanced Java Topics
Salahaddin University-Erbil
 
PPTX
Networking.pptx
Esubesisay
 
PPTX
5_6278455688045789623.pptx
EliasPetros
 
PPT
Unit 8 Java
arnold 7490
 
PPT
Udp Programming
leminhvuong
 
PPT
Udp Programming
phanleson
 
PPTX
Java
Subha Selvam
 
PDF
Lecture6
vantinhkhuc
 
PPTX
Java Network Programming.pptx
RoshniSundrani
 
PDF
17-Networking.pdf
sophia763824
 
PPTX
Socket & Server Socket
Hemant Chetwani
 
PPT
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
PPT
Java Socket Programming
Vipin Yadav
 
PDF
javanetworking
Arjun Shanka
 
PPTX
Java socket programming
Mohammed Abdalla Youssif
 
PPT
Chapter 4 slides
lara_ays
 
PDF
Networking Basics1ofjavaprogramming.pptx.pdf
omkarthombare4989
 
PPT
Socket Programming in Java.ppt yeh haii
inambscs4508
 
PDF
Ipc
deepakittude
 
PDF
Networking
Tuan Ngo
 
Advanced Java Topics
Salahaddin University-Erbil
 
Networking.pptx
Esubesisay
 
5_6278455688045789623.pptx
EliasPetros
 
Unit 8 Java
arnold 7490
 
Udp Programming
leminhvuong
 
Udp Programming
phanleson
 
Lecture6
vantinhkhuc
 
Java Network Programming.pptx
RoshniSundrani
 
17-Networking.pdf
sophia763824
 
Socket & Server Socket
Hemant Chetwani
 
Networking.ppt(client/server, socket) uses in program
govindjha339843
 
Java Socket Programming
Vipin Yadav
 
javanetworking
Arjun Shanka
 
Java socket programming
Mohammed Abdalla Youssif
 
Chapter 4 slides
lara_ays
 
Networking Basics1ofjavaprogramming.pptx.pdf
omkarthombare4989
 
Socket Programming in Java.ppt yeh haii
inambscs4508
 
Networking
Tuan Ngo
 
Ad

More from Danairat Thanabodithammachari (20)

PDF
Thailand State Enterprise - Business Architecture and SE-AM
Danairat Thanabodithammachari
 
PDF
Agile Management
Danairat Thanabodithammachari
 
PDF
Agile Organization and Enterprise Architecture v1129 Danairat
Danairat Thanabodithammachari
 
PDF
Blockchain for Management
Danairat Thanabodithammachari
 
PDF
Enterprise Architecture and Agile Organization Management v1076 Danairat
Danairat Thanabodithammachari
 
PDF
Agile Enterprise Architecture - Danairat
Danairat Thanabodithammachari
 
PDF
Digital Transformation, Enterprise Architecture, Big Data by Danairat
Danairat Thanabodithammachari
 
PDF
Big data Hadoop Analytic and Data warehouse comparison guide
Danairat Thanabodithammachari
 
PDF
Big data hadooop analytic and data warehouse comparison guide
Danairat Thanabodithammachari
 
PDF
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
PDF
Perl Programming - 04 Programming Database
Danairat Thanabodithammachari
 
PDF
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
PDF
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
PDF
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
PDF
Setting up Hadoop YARN Clustering
Danairat Thanabodithammachari
 
PDF
JEE Programming - 03 Model View Controller
Danairat Thanabodithammachari
 
PDF
JEE Programming - 05 JSP
Danairat Thanabodithammachari
 
PDF
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
PDF
JEE Programming - 08 Enterprise Application Deployment
Danairat Thanabodithammachari
 
PDF
JEE Programming - 06 Web Application Deployment
Danairat Thanabodithammachari
 
Thailand State Enterprise - Business Architecture and SE-AM
Danairat Thanabodithammachari
 
Agile Organization and Enterprise Architecture v1129 Danairat
Danairat Thanabodithammachari
 
Blockchain for Management
Danairat Thanabodithammachari
 
Enterprise Architecture and Agile Organization Management v1076 Danairat
Danairat Thanabodithammachari
 
Agile Enterprise Architecture - Danairat
Danairat Thanabodithammachari
 
Digital Transformation, Enterprise Architecture, Big Data by Danairat
Danairat Thanabodithammachari
 
Big data Hadoop Analytic and Data warehouse comparison guide
Danairat Thanabodithammachari
 
Big data hadooop analytic and data warehouse comparison guide
Danairat Thanabodithammachari
 
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
Perl Programming - 04 Programming Database
Danairat Thanabodithammachari
 
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
Perl Programming - 02 Regular Expression
Danairat Thanabodithammachari
 
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
Setting up Hadoop YARN Clustering
Danairat Thanabodithammachari
 
JEE Programming - 03 Model View Controller
Danairat Thanabodithammachari
 
JEE Programming - 05 JSP
Danairat Thanabodithammachari
 
JEE Programming - 04 Java Servlets
Danairat Thanabodithammachari
 
JEE Programming - 08 Enterprise Application Deployment
Danairat Thanabodithammachari
 
JEE Programming - 06 Web Application Deployment
Danairat Thanabodithammachari
 

Recently uploaded (20)

PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
NEW-Viral>Wondershare Filmora 14.5.18.12900 Crack Free
sherryg1122g
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 

Java Programming - 07 java networking

  • 1. Module 07 – Java Networking Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446
  • 2. Fundamental Java Programming The Course Outline Module 01 – Introduction to Java Module 02 – Basic Java Programming Module 03 – Control Flow and Exception Handling Module 04 – Object Oriented in Java Module 05 – Java Package and Access Control Module 06 – Java File IO Module 07 – Java Networking Module 08 – Java Threading
  • 3. Module 07 – Java Networking • Basic Networking • Network Port Numbers • Java Socket • Writing Server Socket • Writing Client Socket • Passing Object over Network
  • 4. Basic Networking TCP: The protocol for two applications which are want to communicate to each other reliably, they establish a connection and send data back and forth over that connection. This is analogous to making a telephone call. UDP: The protocol that is not guaranteed between two applications on the network. UDP is not connection-based like TCP. Rather, it sends independent packets of data, called datagrams, from one application to another. Sending datagrams is much like sending a letter through the postal service: The order of delivery is not important and is not guaranteed, and each message is independent of any other.
  • 5. Network Port Numbers IP Address is a unique number of computer network address for both server and client. Ports are identified by a 16-bit number (65536 ports), which TCP and UDP use to deliver the data to the right application. The first 1024 port numbers have been reserved by system root user. The rest port numbers can be use by other system users.
  • 6. Java Socket A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.
  • 7. Simple Socket Server package javanetworkingproject; import java.net.*; import java.io.*; public class MyServer { public static void main(String args[]) throws IOException { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); Socket s1= s.accept(); // Wait and accept a connection // Get a communication stream associated with the socket OutputStream s1out = s1.getOutputStream(); DataOutputStream dos = new DataOutputStream (s1out); // Send a string! dos.writeUTF("Hi there สวัสดี"); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } }
  • 8. Simple Client Server import java.net.*; import java.io.*; public class MyClient { public static void main(String args[]) throws IOException { // Open your connection to a server, at port 1234 Socket s1 = new Socket("127.0.0.1",1234); // Get result from server InputStream s1In = s1.getInputStream(); DataInputStream dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } } Hi there สวัสดี
  • 9. Object Passing over network import java.io.Serializable; public class Customer implements Serializable{ private String name; private int collectedPoints; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setCollectedPoints(int collectedPoints) { this.collectedPoints = collectedPoints; } public int getCollectedPoints() { return collectedPoints; } }
  • 10. Server Socket Programming with Object Passing package javanetworkingproject2; import java.net.*; import java.io.*; public class MyServer2 { public static void main(String[] args) throws Exception { // Register service on port 1234 ServerSocket s = new ServerSocket(1234); Socket s1 = s.accept(); // Wait and accept a connection // Read input from client ObjectInputStream objInputStream = new ObjectInputStream(s1.getInputStream()); Customer cust1 = (Customer)objInputStream.readObject(); cust1.setCollectedPoints(3000); // Write result to client OutputStream s1out = s1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(s1out); objOutputStream.writeObject(cust1); // Close the connection, but not the server socket objOutputStream.close(); s1out.close(); objInputStream.close(); s1.close(); } }
  • 11. Client Socket Programming with Object Passing import java.net.*; import java.io.*; public class MyClient2 { public static void main(String[] args) throws Exception { // Open your connection to a server, at port 1234 Socket s1 = new Socket("127.0.0.1", 1234); // Write object to the socket server Customer cust1 = new Customer(); cust1.setName("Somchai"); ObjectOutputStream objOutputStream = new ObjectOutputStream(s1.getOutputStream()); objOutputStream.writeObject(cust1); // Get result from server InputStream s1In = s1.getInputStream(); ObjectInputStream objInputStream = new ObjectInputStream(s1In); Customer custResult = (Customer)objInputStream.readObject(); System.out.println(custResult.getName() + " has " + custResult.getCollectedPoints() + " points"); // When done, just close the connection and exit objInputStream.close(); s1In.close(); objOutputStream.close(); s1.close(); } }
  • 12. URL Reader import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL googleURL = new URL("http://www.google.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( googleURL.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }
  • 13. Danairat T. Line ID: Danairat FB: Danairat Thanabodithammachari +668-1559-1446 Thank you