Advanced Programming-NETWORK CH-04
Advanced Programming-NETWORK CH-04
By: Mule Ta – 2
CH04: Network Programming
What is Network ?
By: Mule Ta – 3
CH04: Network Programming
What is Network ?
an Interconnection between computing machine.
Modern-day applications 7→ Internet based or Web-Based!
↓ java handles via
• Network Programming!
java.net package provides
stream-based communications: Enable applications to view networking as
streams of data
packet-based communications: for transmitting individual packets of information
Java provides two mechanisms for distributed computing
Socket-based communication
• Enable applications to view networking as if it were file I/O
↓ achieved via
By: Mule Ta – 3
CH04: Network Programming
What is Network ?
an Interconnection between computing machine.
Modern-day applications 7→ Internet based or Web-Based!
↓ java handles via
• Network Programming!
java.net package provides
stream-based communications: Enable applications to view networking as
streams of data
packet-based communications: for transmitting individual packets of information
Java provides two mechanisms for distributed computing
Socket-based communication
• Enable applications to view networking as if it were file I/O
↓ achieved via
Socket Programming!
By: Mule Ta – 3
CH04: Network Programming
What is Network ?
an Interconnection between computing machine.
Modern-day applications 7→ Internet based or Web-Based!
↓ java handles via
• Network Programming!
java.net package provides
stream-based communications: Enable applications to view networking as
streams of data
packet-based communications: for transmitting individual packets of information
Java provides two mechanisms for distributed computing
Socket-based communication
• Enable applications to view networking as if it were file I/O
↓ achieved via
Socket Programming!
Remote method invocation (RMI)
• N’Z scope of the chapter ....
By: Mule Ta – 3
CH04: Network Programming
By: Mule Ta – 4
CH04: Network Programming
By: Mule Ta – 4
CH04: Network Programming
By: Mule Ta – 5
CH04: Network Programming
By: Mule Ta – 6
CH04: Network Programming
By: Mule Ta – 6
CH04: Network Programming
By: Mule Ta – 6
CH04: Network Programming
4. The constructor of the Socket class attempts to connect the client to the
specified server and port number
By: Mule Ta – 6
CH04: Network Programming
4. The constructor of the Socket class attempts to connect the client to the
specified server and port number
5. On the server side, the accept method returns a reference to a new socket
on the server that is connected to the client’s socket
↓
if (connection is successful)
communication can occur using I/O streams
By: Mule Ta – 6
CH04: Network Programming
By: Mule Ta – 7
CH04: Network Programming
By: Mule Ta – 8
CH04: Network Programming
By: Mule Ta – 9
CH04: Network Programming
2 public ServerSocket(int port, int backlog) throws IOException the backlog parameter specifies how many incoming
clients to store in a wait queue
3 public ServerSocket(int port, int backlog, InetAddress InetAddress parameter specifies the local IP address
address) throws IOException to bind to.
4 public ServerSocket throws IOException Creates an unbound server socket. When using this
constructor, use the bind method when you are ready
to bind the server socket
By: Mule Ta – 10
CH04: Network Programming
2 public ServerSocket(int port, int backlog) throws IOException the backlog parameter specifies how many incoming
clients to store in a wait queue
3 public ServerSocket(int port, int backlog, InetAddress InetAddress parameter specifies the local IP address
address) throws IOException to bind to.
4 public ServerSocket throws IOException Creates an unbound server socket. When using this
constructor, use the bind method when you are ready
to bind the server socket
has Methods
public int getLocalPort()
Returns the port that the server socket is listening on.
public Socket accept() throws IOException
Waits for an incoming client
blocks until either a client connects to the server on the specified port or the
socket times out
By: Mule Ta – 10
CH04: Network Programming
represents an IP address
provides methods to get the IP of any host name
1. static InetAddress getByAddress(byte[] addr)
Returns an InetAddress object given the raw IP address .
2. static InetAddress getByName(String host)
Determines the IP address of a host, given the host’s name.
3. String getHostAddress()
Returns the IP address string in textual presentation.
4. String getHostName()
Gets the host name for this IP address.
By: Mule Ta – 11
CH04: Network Programming
import java.io.*;
import java.io.*; import java.net.*;
import java.net.*; public class IntAddressTestDemo{
public class IntAddressTestDemo{ public static void main(String [] agrs){
public static void main(String [] agrs){ byte address[]={142,250,201,142};
try{ try{
InetAddress ip=InetAddress.getByName("www.aastu.edu.et"); InetAddress ip=InetAddress.getByAddress(address);
System.out.println("Host Name: "+ip.getHostName()); System.out.println("IP Address: "+ip.getHostAddress());
System.out.println("IP Address: "+ip.getHostAddress()); System.out.println("Host Name: "+ip.getHostName());
}
catch(Exception e){ }
System.out.println(e); catch(Exception e){
} System.out.println(e);
} }
} }
Output: }
Host Name: www.aastu.edu.et Output:
IP Address: 197.156.73.161 IP Address: 142.250.201.142
Host Name: www.google.com
By: Mule Ta – 12
CH04: Network Programming
What is UDP ?
stands for User datagram protocol.
Connectionless Transport layer protocol
primarily used to establish low-latency and loss-tolerating connections between
applications on the internet.
beneficial in time-sensitive communications.
VoIP, DNS, multimedia data
How Java Implements UDP then?
Two main classes
• DatagramPacket : is a data container
• DatagramSocket : is a mechanism to send and receive DatagramPackets.
↓
data transferred is encapsulated in a unit 7→ DATAGRAM
↙
independent, self-contained message sent over the network whose
arrival, arrival time, and content are not guaranteed
By: Mule Ta – 13
CH04: Network Programming
By: Mule Ta – 14
CH04: Network Programming
Any Methods ?
send(DatagramPacket p): sends a datagram packet.
receive(DatagramPacket p): receives a datagram packet.
setSoTimeout(int timeout):
↓
sets timeout in milliseconds, limiting the waiting time when receiving data.
↓
• If the timeout expires, a SocketTimeoutException is raised.
close(): closes the socket.
Exceptions raised:
IOException, PortUnreachableException, SocketTimeoutException
By: Mule Ta – 15
CH04: Network Programming
No Methods Description
1 public InetAddress getAddress( ) returns an InetAddress object containing the address
of the remote host
3 public byte[ ] getData() returns a byte array containing the data from the
datagram
4 public int getLength() returns the number of bytes of data in the datagram
No Methods Description
1 public InetAddress getAddress( ) changes the payload of the UDP datagram
2 public void setData(byte[] data, int offset, int length) provides an alternative approach to sending a large
quantity of data..
3 public void setAddress(InetAddress remote) changes the address a datagram packet is sent to
4 public void setAddress(SocketAddress remote) rchanges the address and port a datagram packet is
By: Mule Ta – sent to 16
CH04: Network Programming
By: Mule Ta – 17
CH04: Network Programming
By: Mule Ta – 18
CH04: Network Programming
By: Mule Ta – 19
CH04: Network Programming
By: Mule Ta – 20
CH04: Network Programming
By: Mule Ta – 20
Done!
Question!
By: Mule Ta – 21