24 bytes

  • HOME
  • Algorithms
    blue
  • Bubble Sort
  • Bucket Sort
  • Insertion Sort
  • Selection Sort
  • Merge Sort
  • Quick Sort
  • Heap Sort
  • Counting Sort
  • Stack
  • Queue
  • Double Linked List
  • Binary Search Tree
  • Towers OF Hanoi

  • Java Programs
    green bullet
  • Download RMI Calculator
  • Download file
  • Clock applet
  • File Upload
  • JDOM Parser
  • Client server
  • Udp Client server
  • Sudoku Solver

  • System Programming
  • Newtwork Sniffer
  • Good Links
    green bullet
  • Free Source Code
  • Top Coder
  • Code Project
  • Learn Today
  • Concept
  • Intresting Programs
    green bullet
  • Prime Number
  • GCD Euclids
  • Permutations


  • Google
     
    Web 24bytes.com



    Simple UDP Client Server Using Java
    
    SERVER
    package datagramtimeserver;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.util.Date;
    
    /**
     *
     * @author pcrao
     */
    public class TimeServer{
        static DatagramSocket socket ;
        /** Creates a new instance of Main */
        public TimeServer() {
            
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            try {
                socket = new DatagramSocket(9090);
                byte[] buf = new byte[256];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                while(true) {
                    System.out.println("Listening");
                    socket.receive(packet);          
                    
                    String toClient=new Date().toString();
                    buf = toClient.getBytes();
                    InetAddress address = packet.getAddress();
                    int port = packet.getPort();
                    packet = new DatagramPacket(buf, buf.length, address, port);
                    socket.send(packet);              
                    
                }
                
                
            } catch (SocketException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
    }
    
    
    
    CLIENT
    package datagramtimeserver;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    
    public class DataTimeClient {
        
        /** Creates a new instance of DataTimeClient */
        public DataTimeClient() {  
           }
        
        public static void main(String args[]) {
            byte[] buf = new byte[256];
            DatagramSocket socket;
            try {
                socket = new DatagramSocket();
                InetAddress address= InetAddress.getByName("localhost");
                DatagramPacket packet = new DatagramPacket(buf, buf.length,
                        address, 9090);
                socket.send(packet);
                packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                System.out.println(new String(packet.getData()));
            } catch (IOException ex) {
                ex.printStackTrace();
            }       
        }
    }
    
    Simple UDP Client Server Using Java
    
    request a code or suggestion


    Email:
    Category


    Google
     
    Web 24bytes.com
    . original template by Aran Down.