Tuesday, November 7, 2017

LAB 6 - DATAGRAM (UDP) - Simple


Receiver.java
import java.net.*; 
public class Receiver{ 
  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("Receiver is Waiting for the message:");
    System.out.println("Message received  : " +str); 
    ds.close(); 
  } 
}   


Sender.java
import java.net.*; 
public class Sender{ 
  public static void main(String[] args) throws Exception { 
    DatagramSocket ds = new DatagramSocket(); 
    String str = "Welcome to Network Programming"; 
    InetAddress ip = InetAddress.getByName("127.0.0.1"); 
    
    DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000); 
    System.out.println("Sender is sending the message :");
    ds.send(dp); 
    ds.close(); 
  } 


SOURCE CODE
RECEIVER
SENDER 

OUTPUT

SENDER 
 
RECEIVER 
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.