Chapter 14 (Networking)
Chapter 14 (Networking)
©https://blogs.ashrithgn.com/
Reference Books
2. Java Server Programming (J2EE 1.4 Edition) Black Book (2006 Print) by
Steven Holzner et al.
Chapter (1) & (7) Introduction to J2EE and Web Containers
Chapter (8) Understanding Servlet Programming
Chapter (9) Understanding Servlet Sessions
Chapter (10) Understanding of Java Server Pages and JSTL
Chapter (6) JDBC and Database Programming
©https://blogs.ashrithgn.com/
Basic of Networking
linking multiple devices so that they can readily share information and software
resources.
Three elements in networking
Client – sends request for services
Server – sends response
Network – media of communication
What is protocol?
Protocol is the special set of rules that end points in a telecommunication
connection use when they communicate.
clipartmax.com
Ref: https://microchipdeveloper.com/tcpip:tcp-vs-udp
8 Faculty of Computer Science ©https://blogs.ashrithgn.com/
Port
User level process/services generally use port number >=1024 (port number
<1024 are reserved for special services.)
Some well known ports:
ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
http 80/tcp
https://www.oracle.com/java/technologies/javase/training-support.html
Protocol Domain
File path
represents an IP address
provides methods to get the IP of any host name and vice versa
two types of address:
Unicast - sends IP packets to a single recipient on a network
Multicast - sends IP packets to a group of hosts on a network
import java.net.*;
public class InetAddressEg {
public static void main(String[] args) {
try {
System.out.println(InetAddress.getLocalHost());
String name="www.google.com";
System.out.println(InetAddress.getByName(name));
InetAddress[] addresses=InetAddress.getAllByName(name);
for(InetAddress address:addresses){
System.out.println(address); 130-42VN/172.20.10.5
} www.google.com/74.125.68.103
} catch (UnknownHostException e) { www.google.com/74.125.68.103
e.printStackTrace();} www.google.com/74.125.68.99
} www.google.com/74.125.68.105
} www.google.com/74.125.68.106
www.google.com/74.125.68.104
www.google.com/74.125.68.147
Some Methods
Method Description
1) public InputStream getInputStream() returns the InputStream attached
with this socket.
2) public OutputStream getOutputStream() returns the OutputStream attached
with this socket.
3) public void close() closes this socket
Some Methods
Method Description
1)public Socket accept() returns the socket and establish a connection
between server and client.
2) public void close() closes the server socket.
Creating Server
need to create the instance of ServerSocket class
ServerSocket ss=new ServerSocket(port number);
Creating Client
need to create the instance of Socket class
Socket s=new Socket(“Server Name”, PortNumber);
Server
- Receives a text
- Prints it
Client
- Sends a text
Server
- acts as a calculator
- accepts command and inputs, and calculates them
- returns result
Client
- interacts with user to get inputs
- request the server giving command with data
- receive the result
import java.io.*;
import java.net.*;
public class CalculatorServer {
String cmd = "";
int num1 = 0;
int num2 = 0;
int result = 0;
public CalculatorServer() throws IOException {
ServerSocket svrSocket = new ServerSocket(1025);
System.out.println("Server is ready***");
Socket connSocket = svrSocket.accept();
System.out.println("Server and client is connected");
32 Faculty of Computer Science ©https://blogs.ashrithgn.com/
Example2: Server Program(Cont’d)
while (true)
{
// create inputStream object to read data form client
DataInputStream in = new DataInputStream(connSocket.getInputStream());
cmd = in.readUTF();
num1 = in.readInt();
num2 = in.readInt();
// method call associate with command from user
if (cmd.equalsIgnoreCase("ADD")) {
result =calculateSum(num1, num2);
} else if (cmd.equalsIgnoreCase("SUB")) {
result =calculateSub(num1, num2);
}
else if (cmd.equalsIgnoreCase("MULT")) {
result = calculateMultiply(num1, num2);
} else {
result = calculateDivide(num1, num2);
}
DataOutputStream out = new DataOutputStream(
connSocket.getOutputStream());
out.writeInt(result);
svrSocket.close();
}
}
// calculate methods
private int calculateSum(int n1, int n2) {
return (n1 + n2);
34 } Faculty of Computer Science ©https://blogs.ashrithgn.com/
Example2: Server Program(Cont’d)
new CalculatorServer();
}
}
35 Faculty of Computer Science ©https://blogs.ashrithgn.com/
Example2: Client Program
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class CalculatorClient {
char ch;
public CalculatorClient() throws IOException,UnknownHostException{
Socket cliSocket = new Socket("localhost", 1025);
do {
// create inputStream object
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
36 Faculty of Computer Science ©https://blogs.ashrithgn.com/
Example2: Client Program(Cont’d)
cliSocket.close();
}
public static void main(String[] args) throws IOException {
new CalculatorClient();
}
}
38 Faculty of Computer Science ©https://blogs.ashrithgn.com/
Exercise
Write a client server program to calculate the Area and Perimeter of a Square.
Client accepts the length and sends it to the server. The server calculates and returns
the results. Client should display these results.