Chapter 6 Networking in Java
Chapter 6 Networking in Java
Chapter 6 Networking in Java
Bedasa Wayessa
Hi TCP connection
request
Hi
TCP connection
Got the reply
time? GET http://www.google.com
2:00
<file>
time
6
Internet Architecture Model
7
Networking Basics
Applications Layer
Standard apps
. HTTP
FTP TCP/IP Stack
Telnet The java.net
package provides Application
User apps
support for the (http,ftp,telnet,…)
Transport Layer two common
TCP network Transport
UDP protocols (TCP, UDP,..)
Programming Interface:
Sockets
Network
Network Layer
(IP,..)
IP Link
Link Layer (device driver,..)
Device drivers
Ping
}catch(Exception e){
System.out.println(e);
}
}
}
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){
System.out.println(e);
}
}
}
CS@AmboU Advanced Programming - Comp 511 18
URL class
The URL class represents a URL.
URL is an acronym for Uniform Resource Locator.
It points to a resource on the World Wide Web.
– For example: http://www.javatpoint.com/sonoojaiswal/index.jsp
A URL contains many information’s:
Protocol: In this case, http is the protocol.
Server name or IP Address:
In this case, www.javatpoint.com is the server name.
Port Number: It is an optional attribute.
If we write http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port
number.
File Name or directory name:
In this case, index.jsp is the file name.
CS@AmboU Advanced Programming - Comp 511 19
URL class
Examples of protocols include HTTP, HTTPS, FTP, and File.
The path is also referred to as the filename, and the host is also called
the authority.
Commonly used methods of URL class:
public String getProtocol(): it returns the protocol of the URL.
public String getHost(): it returns the host name of the URL.
public String getPort(): it returns the Port Number of the URL.
public String getFile(): it returns the file name of the URL.
InetAddress ip=InetAddress.getByName("www.javatpoint.com");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e){
System.out.println(e);}
}
}
}
//DSender.java
import java.net.*;
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(),
str.length(), ip, 3000);
ds.send(dp);
ds.close();
}
}
ds.close();
}
}
import java.rmi.*;
public interface MyRemoteObject extends Remote{
public double sum(double num1, double num2) throws
RemoteException;
public double product(double num1, double num2) throws
RemoteException;
}
CS@AmboU Advanced Programming - Comp 511 44
RMI
The UnicastRemoteObject class:
The actual implementation of a remote object (not the interface we
discussed previously) will usually extend
java.rmi.server.UnicastRemoteObject.
This is the RMI equivalent to the familiar Object class.
When a subclass of UnicastRemoteObject is constructed, the RMI
runtime system automatically "exports" it to start listening for network
connections from remote interfaces (stubs) for the object.
Here's a remote object class that implements the RemoteObject
interface.
import java.rmi.*;
public interface Addition extends Remote{
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.*;
import java.io.*;
public class Server{
Naming.rebind("rmi://localhost/Addition",server);
import java.rmi.*;
public class Client{
public static void main(String args[]){
try{
Addition addition =
(Addition)Naming.lookup("rmi://localhost/Addition");
System.out.println("The sum is: " + addition.add(90,80));
}catch(ConnectException e){
System.out.println("Unable to connect to server");
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
https://docs.oracle.com/en/java/
49