Minggu, 01 Juli 2018

THREAD AND UDP

class MultithreadingDemo extends Thread{  
  public void run(){  
    System.out.println("My thread is in running state.");  
  }   
  public static void main(String args[]){  
     MultithreadingDemo obj=new MultithreadingDemo();   
     obj.start();  
  }  
}
IMPLEMENTASI:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class GETPOST extends Thread{
    static int PORT=8212;
    private DatagramSocket udpSocket;
    private int port;

    public GETPOST(int port) throws SocketException, IOException {
        this.port = port;
        this.udpSocket = new DatagramSocket(this.port);
    }
 
   
    public static void main(String[] args) throws Exception {
        GETPOST client = new GETPOST(PORT);
        //client.listen();
        client.start();
    }

   
    public void run(){  //wait
        System.out.println("My thread is in running state."); 
        try{
        System.out.println("I'm Wait " + InetAddress.getLocalHost() + "!");
        String msg;
       
        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            udpSocket.receive(packet);
            msg = new String(packet.getData()).trim();
            System.out.println("Message From Server/" + packet.getAddress().getHostAddress() + ": " + msg);
        }
        }
        catch(Exception ee){}
  } 
   
   
   
     private void listen() throws Exception {
        System.out.println("I'm Wait " + InetAddress.getLocalHost() + "!");
        String msg;
       
        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            udpSocket.receive(packet);
            msg = new String(packet.getData()).trim();
            System.out.println("Message From Server/" + packet.getAddress().getHostAddress() + ": " + msg);
        }
    }
}

+++++++++++++++++++++++++++++++++++++++++



My thread is in running state.
I'm Wait DESKTOP-DBJ9C1E/169.254.19.56!
Message From Server/192.168.1.3: halo apakabar

IMPLEMENTASI




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


public class GETPOST extends Thread{
    private DatagramSocket udpSocketTX;
    private InetAddress serverAddress;
    private static int portTX=8217;

    static int PORT=8218;
    private DatagramSocket udpSocket;
    
    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
        try{
        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);
            udpSocket.receive(packet);
            msg = new String(packet.getData()).trim();
            System.out.println("Message From Server/" + packet.getAddress().getHostAddress() + ": " + msg);
          
                GETPOST sender = new GETPOST("192.168.1.3", portTX);
                sender.mulai();
         
        }
        }
        catch(Exception ee){}
  }   
    
    
      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);   
    }
    
}

Tidak ada komentar:

Posting Komentar