Senin, 02 Juli 2018

UDP CLIENTSERVER Without Thread Extends

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ClientServer2 {
    String IP_SERVER="192.168.1.17";
    private InetAddress serverAddress;
   
    static int portTX=8213;
    static int portRX=8212;
   
    private DatagramSocket udpRX;
    private DatagramSocket udpTX;
   
    public ClientServer2(int port) throws SocketException, IOException {
        this.portRX = port;
        this.udpRX = new DatagramSocket(portRX);
    }
 
   
    public static void main(String[] args) throws Exception {
    new Thread(){
        public void run(){
            System.out.println("hello run");
            try {
//                    long start = System.currentTimeMillis();
//                    sleep(2000);
//                    System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
                    System.out.println("Thread");
                    ClientServer2 client = new ClientServer2(portRX);
                    client.listen();
       
            } catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("world");
        }
    }.start();

    }
   
private void listen() throws Exception {
        System.out.println("-- Running Main Server at " + InetAddress.getLocalHost() + "--");
        String msg;
       
        while (true) {   
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            udpRX.receive(packet);
            msg = new String(packet.getData()).trim();
            System.out.println( "Message from " + packet.getAddress().getHostAddress() + ": " + msg);
           
                  if(msg.indexOf("WHAT IS")>=0){
                        ClientServer2 sender = null;
                        try {
                             sender = new ClientServer2(IP_SERVER, portTX);
                             sender.mulai();
                        } catch (IOException ex) {
                            Logger.getLogger(ClientServer2.class.getName()).log(Level.SEVERE, null, ex);
                        }
       
          }//if
                 
        }
    }

      private ClientServer2(String destinationAddr, int port) throws IOException {
        this.serverAddress = InetAddress.getByName(destinationAddr);
        this.portTX = port;
        udpTX = new DatagramSocket(this.portTX);
    }

    void mulai() throws IOException {
        String quote="Yg Running is......MP3 dll";
        byte[] buffer = quote.getBytes();
            DatagramPacket p = new DatagramPacket(quote.getBytes(), quote.getBytes().length, serverAddress, portTX);
            this.udpTX.send(p); 
            udpTX.close();
    }
   
}

Tidak ada komentar:

Posting Komentar