Sie sind auf Seite 1von 18

Datagram sockets

UDP
Datagram packets are used to implement a connectionless packet delivery service
supported by the UDP

Each message is transferred from source machine to destination based on


information contained within that packet.

That means, each packet needs to have destination address and each packet
might be routed differently, and might arrive in any order.

Packet delivery is not guaranteed.


Message format

Java supports datagram communication through the following classes:

DatagramPacket

DatagramSocket
commonly used Constructors of DatagramPacket class

DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used

to receive the packets.

DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram

packet. This constructor is used to send the packets.


Commonly used Constructors of DatagramSocket class
DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with

the available Port Number on the localhost machine.

DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds

it with the given Port Number.

DatagramSocket(int port, InetAddress address) throws SocketException: it creates a

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:

void send(DatagramPacket p) ---- Sends a datagram packet from this socket.

void receive(DatagramPacket p) ---- Receives a datagram packet from this socket.


sender
i//DSender.java

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();
}
}
Receiver
//DReceiver.java
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();
}
}
Client server program
java InetAddress class represents an IP address.

The java.net.InetAddress class provides methods to get the IP


of any host name for example www.google.com, www.facebook.com etc.

The InetAddress class has no visible constructors.

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

static InetAddress getByName(String hostName) throws UnknownHostException

static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException


import java.net.*;

class InetAddressTest

public static void main(String args[]) throws UnknownHostException {

InetAddress Address = InetAddress.getLocalHost();

System.out.println(Address);

Address = InetAddress.getByName("starwave.com");

System.out.println(Address);

InetAddress SW[] = InetAddress.getAllByName("www.nba.com");

for (int i=0; i<SW.length; i++)

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.

A URL contains many information:


1. Protocol: In this case, http is the protocol.

2. Server name or IP Address: In this case, www.javatpoint.com is the server name.

3. Port Number: It is an optional attribute.

4. File Name or directory name: In this case, index.jsp is the file name.
public class URLDemo{

public static void main(String[] args){

try{

URL url=new URL("http://www.javatpoint.com/java-tutorial");

System.out.println("Protocol: "+url.getProtocol());

System.out.println("Host Name: "+url.getHost());

System.out.println("PortNumber: "+url.getPort());

System.out.println("File Name: "+url.getFile());

}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.

The openConnection() method of URL class returns the object of


URLConnection class.
import java.io.*; import java.net.*;
public class URLConnectionExample {
public static void main(String[] args){
try{
URL url=new URL("http://www.javatpoint.com/java-tutorial");
URLConnection urlcon=url.openConnection();
InputStream stream=urlcon.getInputStream();
int i;
while((i=stream.read())!=-1){
System.out.print((char)i);
}
}catch(Exception e){System.out.println(e);}
}
}
Java HttpURLConnection class

The Java HttpURLConnection class is http specific URLConnection.

It works for HTTP protocol only.

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);}
}
}

Das könnte Ihnen auch gefallen