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

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 TheCourse 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: Theprotocol 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 IPAddress 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 socketis 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 packagejavanetworkingproject; 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 importjava.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 overnetwork 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 Programmingwith 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 Programmingwith 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.*; importjava.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