Java Networking PDF
Java Networking PDF
1. Networking basics
2. Sockets, port
3. Proxy servers
4. Internet addressing URL
5. java.net – Networking classes and Interfaces
6. Implementing TCP/IP based Server and Client
7. Datagrams – Datagram packet, Datagram server and client
8. URL connections
Networking basics:
Networking is a concept of connecting two or more computing
devices together so that we can share resources.
A socket is bound to a port number so that the TCP layer can identify
the application that data is destined to be sent to
• The client sends a message to the server via a Socket, which the server
immediately stores when it unblocks.
• The server wakes up because it got a message (unblocks), and the Socket
connection is established.
• The server reads the message from the client. (If you skip this step, the
client typically gets confused). This is done via the Socket accepted.
• The server generates a response to the client and sends it, through a write
stream of the Socket accepted.
• The server closes the output stream(of the Socket accepted) to indicate end
of communication. (This is required).
Socket() To create a socket
}catch(Exception e){System.out.println(e);}
}
OUTPUTTTTTTTTTTTTTTT
} Protocol: https
Host Name: www.lagrandee.edu.np
Port Number: 10
File Name: /faculty-and-staff/
Java URL connection class:
• URL – The URL class in Java is the entry point to any available
sources on the internet. A Class URL describes a Uniform Resource
Locator, which is a signal to a “resource” on the World Wide Web. A
source can denote a simple file or directory, or it can indicate a more
difficult object, such as a query to a database or a search engine.
Interfaces in Java.net
• CookiePolicy – The CookiePolicy interface in the java.net package
provides the classes for implementing various networking applications. It
decides which cookies should be accepted and which should be rejected.
In CookiePolicy, there are three pre-defined policy implementations,
namely ACCEPT_ALL, ACCEPT_NONE, and
ACCEPT_ORIGINAL_SERVER.
Datagrams
• Datagrams are collection of information sent from one device to
another device via the established network. When the datagram is
sent to the targeted device, there is no assurance that it will reach to
the target device safely and completely. It may get damaged or lost
in between. Likewise, the receiving device also never know if the
datagram received is damaged or not. The UDP protocol is used to
implement the datagrams in Java.
• DatagramPacket is a message that can be sent or received. It is a
data container. If you send multiple packet, it may arrive in any
order. Additionally, packet delivery is not guaranteed.
import java.net.*;
public class DSender{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Welcome java";
InetAddress ip = InetAddress.getByName("127.0.0.1");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip,3000);
ds.send(dp);
ds.close();
}
}
// DataGram Receiver File Code
import java.net.*;
public class DReceiver{
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}