Aim: -
Write an application to send “hello” message from server to client using
UDP.
Tools:
- NetBeans IDE, jdk
Program:
-
Server.java
package
srvr;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.net.DatagramPacket;
import
java.net.DatagramSocket;
import
java.net.InetAddress;
import
java.net.UnknownHostException;
public
class Srvr {
public static void main(String[] args)
throws Exception {
System.out.println("Server is
Ready...");
byte data[]=new byte[100];
int cprt=777,sprt=888;
InetAddress xn=
InetAddress.getLocalHost();
DatagramPacket dp=new
DatagramPacket(data, data.length, xn, cprt);
DatagramSocket ds=new
DatagramSocket(sprt);
ds.receive(dp);
String s=new
String(dp.getData(),0,dp.getLength());
System.out.print("String From Client : ");
System.out.println(s);
}
}
Client.java
package
srvr;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
import
java.net.DatagramPacket;
import
java.net.DatagramSocket;
import
java.net.InetAddress;
public
class clnt {
public static void main(String[] args)
throws Exception {
byte data[]=new byte[100];
int cprt=777,sprt=888;
System.out.print("Enter String :
");
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in) );
InetAddress xn=
InetAddress.getLocalHost();
String s=br.readLine();
data=s.getBytes();
DatagramPacket dp=new
DatagramPacket(data, data.length, xn, sprt);
DatagramSocket ds=new
DatagramSocket(cprt);
ds.send(dp);
System.out.println("String Sended
To Server...");
}
}
Output
0 Comments