Disini saya akan memberikan contoh source code JAVA untuk aplikasi pengiriman data atau FILE.....
server.java
server.java
import java.net.*;
import java.io.*;
public class FileServer {
public static void main (String [] args ) throws IOException {
// Mengaktifkan Socket Dengan Port 13267
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Menunggu Sambungan...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// Mengirimkan Sebuah File
File myFile = new File ("data.pdf");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Mengirimkan Data...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
}
}
Client.java
import java.net.*; import java.io.*; public class FileClient{ public static void main (String [] args ) throws IOException { int filesize=6022386; // filesize temporary hardcoded long start = System.currentTimeMillis(); int bytesRead; int current = 0; // Membuka dan mengetest socket port 13267 pada Localhost 127.0.0.1 Socket sock = new Socket("127.0.0.1",13267); System.out.println("Connecting..."); // Menerima File byte [] mybytearray = new byte [filesize]; InputStream is = sock.getInputStream(); FileOutputStream fos = new FileOutputStream("source-copy.pdf"); BufferedOutputStream bos = new BufferedOutputStream(fos); bytesRead = is.read(mybytearray,0,mybytearray.length); current = bytesRead; do { bytesRead = is.read(mybytearray, current, (mybytearray.length-current)); if(bytesRead >= 0) current += bytesRead; } while(bytesRead > -1); bos.write(mybytearray, 0 , current); bos.flush(); long end = System.currentTimeMillis(); System.out.println(end-start); bos.close(); sock.close(); } }
Semoga Bermanfaat
Khoirul Fatah (Fox-Z)
mas,,,ada contoh implementasi dengan form gk???
BalasHapus