Kumpulan Praktikum Tugas Sistem Terdistribusi
1. Buatlah program yang membaca input text dari keyboard, Inputan berupa satu kalimat atau paragraph program akan keluar ketika kita mengetikkan kata “keluar”.
Source Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class nomorsatu {
public static void main(String[] args) {
int data;
System.out.println("Masukkan angka dan akhiri dengan \"888\"");
do {
System.out.print("Masukkan angka : ");
BufferedReader bfr = new BufferedReader
(new InputStreamReader(System.in));
String valueInput = null;
try {
valueInput = bfr.readLine();
} catch (IOException e) {
e.printStackTrace();
}
data= Integer.valueOf(valueInput).intValue();
//Data = String.valueOf(valueInput).toString();
//keluar = String.valueOf(valueInput).toString();
}
while (data!=888);
System.out.println("Selesai");
}
}
Hasilnya:
2. Buatlah program untuk mengkopi isi sebuah file ke file lain.
Source Code:
package soal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class no2 {
public static void main(String[] args)
{
FileInputStream instream = null;
FileOutputStream outstream = null;
try{
File infile1 =new File("D:/inifile.txt");
File outfile1 =new File("D:/buku/pindahfile.txt");
instream = new FileInputStream(infile1);
outstream = new FileOutputStream(outfile1);
byte[] buffer = new byte[1024];
int length;
while ((length = instream.read(buffer)) > 0){
outstream.write(buffer, 0, length);
}
instream.close();
outstream.close();
System.out.println("File copied successfully!!");
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Hasilnya:
3. Buatlah program untuk menggabungkan file. Baca sebuah file lalu simpan isi tersebut ke variable dan membaca file kedua lalu menyimpannya pada variable kedua kemudian menggambungkan kedua file tersebut dalam satu file yang disimpan di direktori d misalnya.
Source Code:
package soal;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class no3 {
public static void main(String[] args) throws IOException
{
FileInputStream fileInputStream1 = null;
FileInputStream fileInputStream2 = null;
FileInputStream fileInputStream3 = null;
FileOutputStream fileOutputStream = null;
SequenceInputStream sequenceInputStream = null;
try
{
fileInputStream1 = new FileInputStream("D:/file1.txt");
fileInputStream2 = new FileInputStream("D:/file2.txt");
fileInputStream3 = new FileInputStream("D:/file3.txt");
fileOutputStream = new FileOutputStream("D:/buku/myoutputfile.txt");
Vector<FileInputStream> vector = new Vector<FileInputStream>();
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
Enumeration<FileInputStream> enumeration = vector.elements();
sequenceInputStream = new SequenceInputStream(enumeration);
int i;
while ((i = sequenceInputStream.read()) != -1)
{
fileOutputStream.write(i);
}
System.out.println("Successfully read from 3 files and written to one file.");
}
finally
{
if (fileInputStream1 != null)
{
fileInputStream1.close();
}
if (fileInputStream2 != null)
{
fileInputStream2.close();
}
if (fileInputStream3 != null)
{
fileInputStream3.close();
}
if (fileOutputStream != null)
{
fileOutputStream.close();
}
if (sequenceInputStream != null)
{
sequenceInputStream.close();
}
}
}
}
Hasilnya:
4. Buat program untuk merename file dengan inputan keyboard dan membuat folder baru dengan inputan keyboard.
Source Code:
package soal;
import java.io.File;
public class no4 {
public static void main (String[] args){
File newFile = new File("D:/buku.txt");
File renFile = new File("D:/buku/kutu.txt");
if(newFile.renameTo(renFile)){
System.out.println(newFile.getName()+" diubah menjadi "+ renFile.getName() );
System.out.println("Sukses !!");
}
else{
System.out.println("Operation Failed !!");
}
}
}
Hasilnya:
5. Buat program untuk pengecekan keberadaan folder. Misalnya dalam direktori “d” terdapat folder buku, kemudian dengan menjalankan program dan mengetikkan “buku” maka program akan mencari difolder tersebut jika ada maka “true” jika tidak ada maka “false”.
Source Code:
import java.io.File;
public class nomorlima {
public static void main(String[]args){
String FOLDER_FILE = "D:/buku";
File file = new File(FOLDER_FILE);
if(file.exists())
System.out.println("true");
else
System.out.println("false");
}
}
Hasilnya:
package java_thread;public class kirim extends Thread {int threadNumber;public kirim (int num) {threadNumber = num;}public void run() {for(int g=1;g<=10;g+=2){if (g%2!=0)System.out.println("bilangan ganjil= "+g);}try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}}public static void main(String[] args) {Thread t1= new kirim(1);Thread t2= new kirim(2);Thread t3= new kirim(3);Thread t4= new kirim(4);Thread t5= new kirim(5);Thread t6= new kirim(6);Thread t7= new kirim(7);Thread t8= new kirim(8);Thread t9= new kirim(9);Thread t10= new kirim(10);//mulait1.start();t2.start();t3.start();t4.start();t5.start();t6.start();t7.start();t8.start();t9.start();t10.start();}}
package java_thread;public class terima extends Thread {int threadNumber;public terima (int num) {threadNumber = num;}public void run() {for(int g=1;g<=10;g++){if (g%2==0)System.out.println("bilangan genap= "+g);}try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}}public static void main(String[] args) {Thread t1= new terima(1);Thread t2= new terima(2);Thread t3= new terima(3);Thread t4= new terima(4);Thread t5= new terima(5);Thread t6= new terima(6);Thread t7= new terima(7);Thread t8= new terima(8);Thread t9= new terima(9);Thread t10= new terima(10);//mulait1.start();t2.start();t3.start();t4.start();t5.start();t6.start();t7.start();t8.start();t9.start();t10.start();}}
package java_thread;public class ThreadUtama extends Thread {int threadNumber;public ThreadUtama (int num) {threadNumber = num;}public void run() {for(int g=1;g<=10;g++){if (g%2==0)System.out.println("bilangan genap= "+g);}try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}}public static void main(String[] args) {Thread t1= new terima(1);Thread t2= new terima(2);Thread t3= new terima(3);Thread t4= new terima(4);Thread t5= new terima(5);Thread t6= new terima(6);Thread t7= new terima(7);Thread t8= new terima(8);Thread t9= new terima(9);Thread t10= new terima(10);Thread t11= new kirim(1);Thread t12= new kirim(2);Thread t13= new kirim(3);Thread t14= new kirim(4);Thread t15= new kirim(5);Thread t16= new kirim(6);Thread t17= new kirim(7);Thread t18= new kirim(8);Thread t19= new kirim(9);Thread t20= new kirim(10);//mulait1.start();t2.start();t3.start();t4.start();t5.start();t6.start();t7.start();t8.start();t9.start();t10.start();t11.start();t12.start();t13.start();t14.start();t15.start();t16.start();t17.start();t18.start();t19.start();t20.start();}}
package java_thread;public class RunnableThread implements Runnable{public void run() {for(int g=1;g<=10;g+=2){if (g%2!=0)System.out.println("bilangan ganjil= "+g);}try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println("Selesai");}}public static void main(String[] args) {System.out.println("Create runnable oject");Runnable run= (Runnable) new RunnableThread();System.out.println("Create thread pertama");Thread t1= new Thread(run);System.out.println("Mulai kedua thread");t1.start();}}
package java_thread;public class hitungarray extends Thread {int threadNumber;public hitungarray (int num) {threadNumber = num;}public void run() {int[] array1 = {6, 9, 1, 2, 3, 5};int sum1 = 0;for( int num1 : array1) {sum1 = sum1+num1;}System.out.println("Thread 1: 6+9+1+2+3+5= "+ sum1);int[] array2 = {7, 11, 6, 4, 3, 1};int sum2 = 0;for( int num2 : array2) {sum2 = sum2+num2;}System.out.println("Thread 2: 7+11+6+4+3+1= "+ sum2);int[] array3 = {5, 4, 3, 2, 1, 12};int sum3 = 0;for( int num3 : array3) {sum3 = sum3+num3;}System.out.println("Thread 3: 5+4+3+2+1+12= "+ sum3);try {Thread.sleep(3000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}int total;total= sum1+sum2+sum3;System.out.println("Total ="+total);}public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("Helmy Kurniawan");Thread t1= new hitungarray(1);//mulait1.start();}}
package java_thread;public class cerita extends Thread {int threadNumber;public cerita (int num) {threadNumber = num;}public void run() {System.out.println("Dokter 1 menangani pasien-"+ threadNumber);try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}for (int i=1; i<=5;i++);{//System.out.println("perawat2");}try {Thread.sleep(1000);} catch (InterruptedException ex) {System.out.println(threadNumber + "Selesai");}System.out.println("Pasien ke-"+threadNumber+" membayar dikasir 1");}public static void main(String[] args) {System.out.println("Cerita; ");Thread t1= new cerita(1);Thread t2= new cerita(2);Thread t3= new cerita(3);Thread t4= new cerita(4);Thread t5= new cerita(5);Thread t6= new cerita(6);Thread t7= new cerita(7);Thread t8= new cerita(8);Thread t9= new cerita(9);Thread t10= new cerita(10);Thread t11= new ceritaperawat(1);Thread t12= new ceritaperawat(2);Thread t13= new ceritaperawat(3);Thread t14= new ceritaperawat(4);Thread t15= new ceritaperawat(5);Thread t16= new ceritaperawat(6);Thread t17= new ceritaperawat(7);Thread t18= new ceritaperawat(8);Thread t19= new ceritaperawat(9);Thread t20= new ceritaperawat(10);//mulait1.start();t2.start();t3.start();t4.start();t5.start();t6.start();t7.start();t8.start();t9.start();t10.start();t11.start();t12.start();t13.start();t14.start();t15.start();t16.start();t17.start();t18.start();t19.start();t20.start();}}
package java_thread;public class ceritaperawat extends Thread {int threadNumber;public ceritaperawat (int num) {threadNumber = num;}public void run() {System.out.println("Perawat 1 menangani pasien-"+ threadNumber);System.out.println("Perawat 2 menangani pasien-"+ threadNumber);}public static void main(String[] args) {System.out.println("Cerita; ");Thread t1= new ceritaperawat(1);Thread t2= new ceritaperawat(2);Thread t3= new ceritaperawat(3);Thread t4= new ceritaperawat(4);Thread t5= new ceritaperawat(5);Thread t6= new ceritaperawat(6);Thread t7= new ceritaperawat(7);Thread t8= new ceritaperawat(8);Thread t9= new ceritaperawat(9);Thread t10= new ceritaperawat(10);//mulait1.start();t2.start();t3.start();t4.start();t5.start();t6.start();t7.start();t8.start();t9.start();t10.start();}}
package JavaApplication1;import java.net.InetAddress;import java.net.UnknownHostException;public class JavaApplication1 {public static void main(String[] args) throws UnknownHostException {InetAddress ip;InetAddress remoteAddress = InetAddress.getByName("www.google.com");String hostname;String remoteHostName = remoteAddress.getHostName();String remoteHostAddress = remoteAddress.getHostAddress();try {ip = InetAddress.getLocalHost();hostname = ip.getHostName();System.out.println("your current IP address ; " + ip);System.out.println("your current Hostname ; " + hostname);System.out.println("remoteHostName ; "+remoteHostName);System.out.println("remoteHostAddress ; "+ remoteHostAddress);} catch (UnknownHostException e ){e.printStackTrace();}}}
package JavaApplication1;import java.net.UnknownHostException;import java.net.InetAddress;public class JavaApplication1 {public static void main(String[] args) throws UnknownHostException {// print localhost ip addressSystem.out.println(InetAddress.getLocalHost().getHostAddress());//print all ip address for a websiteInetAddress[] InetAddresses = InetAddress.getAllByName("www.google.com");for(InetAddress inet : InetAddresses){System.out.println(inet);}}}
import java.io.ObjectInputStream;import java.net.*;public class serverhelm {private static int SRV_PORT=5000;private static ObjectInputStream is=null;public static void main(String[] args) throws Exception{ServerSocket SocketServer= new ServerSocket(SRV_PORT);Socket SocketClient= (SocketServer.accept());is= new ObjectInputStream(SocketClient.getInputStream());helmykurniawan helm= (helmykurniawan) is.readObject();System.out.println ("Server menerima data Mahasiwa");helm.print();}}
import java.net.ServerSocket;import java.net.Socket;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;public class clienthelm {private static int SRV_PORT= 5000;private static ObjectOutputStream os=null;public static void main(String[] args) throws Exception{try {Socket SocketClient= new Socket("127.0.0.1", SRV_PORT);os= new ObjectOutputStream(SocketClient.getOutputStream());helmykurniawan helm= new helmykurniawan("Helmy Kurniawan", "19.1.03.02.0145");os.writeObject(helm);System.out.println("Client kirim data Mahasiwa: ");helm.print();}catch (Exception e) { e.printStackTrace();}}}
import java.io.Serializable;public class helmykurniawan implements Serializable {String nama;String npm;public helmykurniawan (String nama, String npm){this.nama=nama; this.npm=npm;}public static void main(String[] args) {// TODO Auto-generated method stub}public void print() {System.out.println("Data Mahasiwa: ");System.out.println("Nama: "+ nama);System.out.println("NPM: "+ npm);}}
jelaskan hasilnya!
import java.io.IOException;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.logging.Level;import java.util.logging.Logger;/**** @author Helmykediri*/public class Server_Basic {private static ServerSocket ss;public static void main (String args[]) throws IOException {ss = new ServerSocket(1236);boolean keadaan = true;int urut = 1;while (keadaan) {new server(ss.accept(), urut).start();System.out.println("Client ke- " + urut + " Masuk ");urut++;}}}class server extends Thread {static Socket sc= null;int angka = 0;server (Socket a, int angka) {this.angka = angka;server.sc = a;}@Overridepublic void run() {System.out.println ( "Client Connect " +sc.getInetAddress()+ "OnPort"+ sc.getPort());try {PrintWriter out = new PrintWriter(sc.getOutputStream(), true);out.println("Selamat Datang Client ke-" +angka);} catch (IOException ex) {Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);}}}
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.Socket;/**** @author Helmykediri*/public class Client_Basic {private static Socket sk;public static void main(String args[]) {try {sk = new Socket("127.0.0.1",1236);BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));String put="";while (true){if((put=in.readLine())!=null){System.out.println(put);}}} catch(IOException e){e.printStackTrace();}}}
import java.io.*;import java.net.*;import java.util.Scanner;/**** @author TOSHIBA*/public class clienthelm {private static InetAddress host;private static final int PORT = 1234;private static Scanner networkInput;private static Scanner userEntry;public static void main(String[] args) {try {host = InetAddress.getLocalHost();} catch (UnknownHostException uhEx) {System.out.println("\nHost ID tidak ditemukan!\n");System.exit(1);}sendMessages();}private static void sendMessages() {Socket socket = null;try {socket = new Socket(host, PORT);networkInput = new Scanner(socket.getInputStream());PrintWriter networkOutput = new PrintWriter(socket.getOutputStream(), true);userEntry = new Scanner(System.in);String message, response;do {System.out.print("Masukkan pesan ('QUIT' untuk keluar): ");message = userEntry.nextLine();//Kirim pesan (message)ke server pada output stream socket...//Terima respon dari server pada stream input socket...networkOutput.println(message);response = networkInput.nextLine();//Tampilkan respon dari server...System.out.println("\nSERVER> " + response);} while (!message.equals("QUIT"));} catch (IOException ioEx) {//ioEx.printStackTrace();} finally {try {System.out.println("\nMenutup koneksi...");socket.close();} catch (IOException ioEx) {System.out.println("Gagal menutup koneksi!");System.exit(1);}}}}
import java.io.*;import java.net.*;import java.util.Scanner;/**** @author TOSHIBA*/public class serverhelm {private static ServerSocket serverSocket;private static final int PORT = 1234;public static void main(String[] args) throws IOException {try {serverSocket = new ServerSocket(PORT);System.out.println("Server berjalan, menunggu client...");} catch (IOException ioEx) {System.out.println("\nTidak dapat mensetup port!");System.exit(1);}do {//Menunggu koneksi dari client...Socket client = serverSocket.accept();System.out.println("\nClient baru diterima.\n");//Buat thread untuk menangani komunikasi dengan client ini//lewatkan socket yang relevan ke contructor dari thread iniClientHandler handler = new ClientHandler(client);handler.start(); //menjalankan thread yang telah dibuat} while (true);}}class ClientHandler extends Thread {private Socket client;private Scanner input;private PrintWriter output;public ClientHandler(Socket socket) {//Set up referensi ke socket yang beraosiasi...client = socket;try {input = new Scanner(client.getInputStream());output = new PrintWriter(client.getOutputStream(), true);} catch (IOException ioEx) {//ioEx.printStackTrace();}}public void run() {String received;do {//Terima pesan dari client pada input stream socket...received = input.nextLine();System.out.println(received);//Echo-kan pesan kembali ke client pada stream output socket...output.println("[" + received + "]");//Ulangi sampai client mengirimkan pesan 'QUIT'...} while (!received.equals("QUIT"));try {if (client != null) {System.out.println("Menutup koneksi...");client.close();}} catch (IOException ioEx) {System.out.println("Penutupan koneksi gagal!");}}}
import java.io.*;import java.net.Socket;import java.net.UnknownHostException;import java.util.logging.Level;import java.util.logging.Logger;/**** @author TOSHIBA*/public class clienthelm {public static void main(String[] args) {try {Socket s = new Socket("localhost", 5136);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();String pesan = "Siapa anda ?? \r\n";os.write(pesan.getBytes());int c;while(true){c=is.read();System.out.print((char)c);if((char)c=='\n')break;}is.close();os.close();s.close();} catch (UnknownHostException ex) {Logger.getLogger(clienthelm.class.getName()).log(Level.SEVERE, null, ex);} catch (IOException ex) {Logger.getLogger(clienthelm.class.getName()).log(Level.SEVERE, null, ex);}}}
import java.io.*;import java.net.ServerSocket;import java.net.Socket;import java.util.logging.Level;import java.util.logging.Logger;/**/**** @author TOSHIBA*/public class serverhelm {private static ServerSocket ss;public static void main(String[] args) {try {ss = new ServerSocket(5136);Socket s = ss.accept();InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();String respon = "Saya Nurul Mahpiroh\r\n";int c;while(true){c=is.read();System.out.print((char)c);if((char)c=='\n')break;}os.write(respon.getBytes());os.flush();os.close();is.close();s.close();} catch (IOException ex) {Logger.getLogger(serverhelm.class.getName()).log(Level.SEVERE, null, ex);}}}
Posting Komentar untuk "Kumpulan Praktikum Tugas Sistem Terdistribusi"
Posting Komentar
Artikel di blog ini bersumber dari pengalaman pribadi penulis, tulisan orang lain sebagai posting tamu maupun bayaran oleh sebab itu segala hak cipta baik kutipan dan gambar milik setiap orang yang merasa memilikinya