Uncategorized

Tugas Rancang 2 – Sistem Terdistribusi (Subject: Socket Programming – UDP)


TOPIK TUGAS RANCANG 2 : Aplikasi Billing Warnet (Client – Server)

MATERI PENDUKUNG:

Berikut ini adalah Contoh Program Chatting Multi-Client:


Kelas ChatEngine.java (Server-nya):

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package latihanudp;

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

/**
 *
 * @author e250-05
 */
public class ChatEngine extends Thread {
    private DatagramSocket socket=null;
    private DatagramPacket terima=null;

    public void run(){
        try {
            socket=new DatagramSocket(5000);
            System.out.println("Server Start");
        } catch (SocketException e) {
            Logger.getLogger(ChatEngine.class.getName()).log(Level.SEVERE,null,e);
        }

        while(true){
            try {
                byte[] data = new byte[200];
                terima = new DatagramPacket(data, data.length);
                socket.receive(terima);
                //konversi dari byte ke string
                System.out.println(new String(terima.getData(), 0, terima.getLength()));
            } catch (IOException ex) {
                Logger.getLogger(ChatEngine.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}

Kelas ServerUDP.java (Thread)

public class ServerUDP {
    public static void main(String[] args) {
        Thread t=new ChatEngine();
        t.start();
    }
}

Kelas MultiChat.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package latihanudp;

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

/**
 *
 * @author e250-05
 */
public class MultiChat {

    private static DatagramPacket kirim = null;
    private static Scanner sc = null;
    private static DatagramSocket socket = null;

    public static void main(String[] args) throws IOException {
        try {
            socket = new DatagramSocket();
        } catch (SocketException ex) {
            Logger.getLogger(MultiChat.class.getName()).log(Level.SEVERE, null, ex);
        }

        sc = new Scanner(System.in);
        String msg = "";
        String msgsend = "";
        String nick = "";
        System.out.println("Input NICK: ");
        nick = sc.next();
        sc = new Scanner(System.in);
        while (!msg.equals("exit")) {
            System.out.println(nick + " : ");
            msg = sc.nextLine();
            if (!msg.equals("exit")) {
                try {
                    msgsend = nick + " : " + msg;
                    byte[] data = msgsend.getBytes();
                    kirim = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5000);
                    socket.send(kirim);
                } catch (UnknownHostException ex) {
                    Logger.getLogger(MultiChat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            msg = nick + " left from the conversation";
            byte data[] = msg.getBytes();

            try {
                kirim = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 5000);
                socket.send(kirim);
            } catch (IOException ex) {
                Logger.getLogger(MultiChat.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        socket.close();
        Runtime.getRuntime().gc();
        Runtime.getRuntime().exit(0);
    }
}

Dari program di atas, yang dijalankan sebagai Server adalah ServerUDP.java dan Client (dapat di-run beberapa/multi client) adalah MultiChat.java

 

Berikut ini adalah contoh Program Client-Server UDP dengan OBJEK yang dikirimkan:

Kelas Objek.java



import java.io.Serializable;

public class objek implements Serializable{

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public String getNim() {
        return nim;
    }

    public void setNim(String nim) {
        this.nim = nim;
    }
    private String nim="", nama="";
    @Override
    public String toString(){
        StringBuffer buffer = new StringBuffer();
        buffer.append(nim);
        buffer.append(nama);
        return buffer.toString();
    }
}

Kelas Protocol.java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class protocol {

    private DatagramSocket socketInduk;
    private DatagramPacket packetInduk;

    public objek receive() {
        objek datas = null;
        try {
            System.out.println("Binding port 4000.");
            socketInduk = new DatagramSocket(4000);
            packetInduk = new DatagramPacket(new byte[256], 256);
            System.out.println("Waiting packet...");
            socketInduk.receive(packetInduk);
            InetAddress remote_addr = packetInduk.getAddress();
            System.out.println("Sent by : " + remote_addr.getHostAddress());
            System.out.println("Sent from: " + packetInduk.getPort());
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(packetInduk.getData()));
            try {
                datas = (objek) ois.readObject();
                System.out.println("nim "+datas.getNim());
                System.out.println("nama "+datas.getNama());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            socketInduk.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datas;
    }

    public void send(objek dC){
        String hostname = "localhost";
        try {
            DatagramSocket socket = new DatagramSocket();
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutput oo = new ObjectOutputStream(bout);
            oo.writeObject((Object)dC);
            oo.close();
            byte[] barray = bout.toByteArray();
            DatagramPacket packet = new DatagramPacket(barray, barray.length);
            System.out.println("Looking up hostname " + hostname);
            InetAddress remote_addr = InetAddress.getByName(hostname);
            System.out.println("Hostname resolved as " + remote_addr.getHostAddress());
            packet.setAddress(remote_addr);
            packet.setPort(4000);
            socket.send(packet);
            System.out.println("Packet sent!");
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

Kelas receiver.java


public class receiver {
    public static void main(String[] args) {
        protocol rec=new protocol();
        rec.receive();
    }
}

Kelas sender.java


public class sender {

    public static void main(String[] args) {
        protocol send = new protocol();
        objek dC = new objek();
        dC.setNim("672007005");
        dC.setNama("arie");
        send.send(dC);
    }
}

TUGAS RANCANG 2:

Socket UDP dikerjakan kelompok 2 orang tiap kelompok.
Buat sebuah perangkat lunak/Sw untuk billing warnet menggunakan UDP.
Client mengirimkan :

  • waktu mulai (Ketika KETIK START)
  • waktu berhenti (Ketika KETIK STOP)
  • username
  • Server melakukan perhitungan tiap menit misalnya Rp. 3000
  • Pada saat selesai server mengirimkan harga ke Client.
  • Dikerjakan dalam Console Application (tanpa GUI).
  • Pekerjaan yang sama akan didiskon, dikumpulkan jadi satu, diresponsi bareng-bareng.
  • Diskon 20% per mahasiswa.

Salam Sayang,

-y2-

Advertisements

1 thought on “Tugas Rancang 2 – Sistem Terdistribusi (Subject: Socket Programming – UDP)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s