Client
------------------------------------------------------------------------------------------------------------
package practical8;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Client8 {
public static void main (String args[])
{
try
{
// Listening to port 3000
DatagramSocket dsoc=new DatagramSocket(3000);
// Creating buffer to store 1024 bytes
byte buff[] = new byte[1024];
// Creating packet that stores the received data
DatagramPacket dpack = new DatagramPacket(buff,buff.length);
// Receives the packet
dsoc.receive(dpack);
// Printing what server has sent
System.out.println(new String(dpack.getData()));
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
Server
------------------------------------------------------------------------------------------------------------
package practical8;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server8 {
public static void main(String args[])
{
try
{
// Listening to the port 5217 (Opening connection)
DatagramSocket dsoc = new DatagramSocket(5217);
// Receiver's address
InetAddress host = InetAddress.getLocalHost();
// What message server wants to send
String str = "Hi, Amit ... ";
// Creating packet to store the message
DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(),host,3000);
// Send the packet
dsoc.send(dp);
// Close the connection
dsoc.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}
Comments
Post a Comment