Minggu, 01 Juli 2018

UDP GET POST UNLIMITED

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 GETPOST extends Thread{
    int lop=1;
    String IPDESTINATION="192.168.1.3";
    private InetAddress serverAddress;
    private static int portTX=8217;

    static int PORT=8218;
    private DatagramSocket udpSocket;
    private DatagramSocket udpSocketTX;
   
    public GETPOST(int port) throws SocketException, IOException {
        this.PORT = port;
        this.udpSocket = new DatagramSocket(PORT);
    }
 
   
    public static void main(String[] args) throws Exception {
       GETPOST client = new GETPOST(PORT);
       client.start();
    }
   
    public void run(){  //wait
        //System.out.println("I'm Wait " + InetAddress.getLocalHost() + "!");
        String msg;
        int ada=0;
        while (ada==0) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            try {
                udpSocket.receive(packet);
            } catch (IOException ex) {
                Logger.getLogger(GETPOST.class.getName()).log(Level.SEVERE, null, ex);
            }
            msg = new String(packet.getData()).trim();
            System.out.println("Message From Server/" + packet.getAddress().getHostAddress() + ": " + msg);
          if(msg.indexOf("WHAT IS")>=0){
            GETPOST sender = null;
            try {
                sender = new GETPOST(IPDESTINATION, portTX);
                 sender.mulai();
            } catch (IOException ex) {
                Logger.getLogger(GETPOST.class.getName()).log(Level.SEVERE, null, ex);
            }
       
          }//if
       
        }
         
 
  } 
   
   
      private GETPOST(String destinationAddr, int port) throws IOException {
        this.serverAddress = InetAddress.getByName(destinationAddr);
        this.portTX = port;
        udpSocketTX = 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.udpSocketTX.send(p); 
            udpSocketTX.close();
    }
   
}

Tidak ada komentar:

Posting Komentar