Beruflich Dokumente
Kultur Dokumente
UDP
Datagram packets are used to implement a connectionless packet delivery service
supported by the UDP
That means, each packet needs to have destination address and each packet
might be routed differently, and might arrive in any order.
DatagramPacket
DatagramSocket
commonly used Constructors of DatagramPacket class
DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used
DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram
datagram socket and binds it with the specified port number and host address.
The class DatagramSocket supports various methods that can be used for transmitting or receiving data a
datagram over the network. The two key methods are:
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");
To create an InetAddress object, you have to use one of the available factory methods.
Factory methods are merely a convention whereby static methods in a class return an instance of that
class.
In the case of InetAddress, the three methods getLocalHost(), getByName(), and getAllByName() can
be used to create instances of InetAddress.
Methods of InetAddress
static InetAddress getLocalHost( ) throws UnknownHostException
class InetAddressTest
System.out.println(Address);
Address = InetAddress.getByName("starwave.com");
System.out.println(Address);
System.out.println(SW[i]); } }
default/206.148.209.138
starwave.com/204.202.129.90
www.nba.com/204.202.130.223
Java URL
The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It
points to a resource on the World Wide Web.
4. File Name or directory name: In this case, index.jsp is the file name.
public class URLDemo{
try{
System.out.println("Protocol: "+url.getProtocol());
System.out.println("PortNumber: "+url.getPort());
}catch(Exception e){System.out.println(e);}
} }
Java URLConnection class
The Java URLConnection class represents a communication link between the URL and the
application.
This class can be used to read and write data to the specified resource referred by the URL.
By the help of HttpURLConnection class, you can information of any HTTP URL such as
header information, status code, response code etc.
The openConnection() method of URL class returns the object of URLConnection class.
import java.io.*;
import java.net.*;
public class HttpURLConnectionDemo{
public static void main(String[] args){
try{
URL url=new URL("http://www.javatpoint.com/java-tutorial");
HttpURLConnection huc=(HttpURLConnection)url.openConnection();
for(int i=1;i<=8;i++){
System.out.println(huc.getHeaderFieldKey(i)+" = "+huc.getHeaderField(i));
}
huc.disconnect();
}catch(Exception e){System.out.println(e);}
}
}