DatagramSocket send1() Example

Sends a datagram packet from this socket.


DatagramSocketclass send1() method example. This example shows you how to use send1() method.This method Sends a datagram packet from this socket.

Here is the code:-
//PROGRAM FOR SERVER SIDE
/**
 * @Program that Receives a datagram packet from this socket.
 * Receive1.java 
 * Author:-RoseIndia Team
 * Date:-20-jun-2008
 */
import java.io.*;

import java.net.*;
import java.util.*;


public class Receive1 {

    public static void main(String args[])throws Exception {
        DatagramSocket socket = null;

        socket = new DatagramSocket(1313);
        for (int i = 0; i < 10; i++) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);

            socket.receive(packet);

      buf = new Date().toString().getBytes();
      InetAddress address = packet.getAddress();
      int port = packet.getPort();
      packet = new DatagramPacket(buf, buf.length, address, port);
      socket.send(packet);

  socket.close();

        }
    }
/*
 
//PROGRAM FOR CLIENT SIDE
/**
 * @Program that Sends a datagram packet from this socket.
 * send1.java 
 * Author:-RoseIndia Team
 * Date:-20-jun-2008
 */
/*
import java.io.*;
import java.net.*;

public class send1 {

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

        DatagramSocket socket = new DatagramSocket();
        for (int i = 0; i < 10; i++) {
            byte[] buf = new byte[256];
            InetAddress address = InetAddress.getByName("localhost");

            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 1313);

            socket.send(packet);

            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            String received = new String(packet.getData());
            System.out.println("Current server time: " + received);
            Thread.sleep(1500);
        }

        socket.close();



    }
}

 */

Output of the program:-
Current server time: Mon Jun 23 12:27:19 IST 2008

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US