Simple Sending File
Yang butuh bantuan cara pengiriman file pake Socket Programming untuk tugas Praktikum Sistem Operasi Tahun Akademik 2010/2011 bisa mempelajari source code di bawah ini.
Server.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /** * @(#)Server.java * * * @author * @version 1.00 2010/12/3 */ import javax.swing.*; import java.io.*; import java.net.*; public class Server { public static void main (String[] args) { try { ServerSocket ss = new ServerSocket(2889); System.out.println("Server start and waiting client..."); Socket socket = ss.accept(); ObjectOutput out = new ObjectOutputStream(socket.getOutputStream()); ObjectInput in = new ObjectInputStream(socket.getInputStream()); byte[] dataReceive = (byte[])in.readObject(); JFileChooser choose = new JFileChooser(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(choose); } catch(Exception e) { } int result = choose.showSaveDialog(null); if(result==JFileChooser.APPROVE_OPTION) { String filePath = choose.getSelectedFile().toString(); File file = new File(filePath); FileOutputStream fos = new FileOutputStream(file); fos.write(dataReceive); fos.close(); JOptionPane.showMessageDialog(null, "File telah selesai disimpan di "+filePath+"!"); } } catch(IOException ioe) { } catch(ClassNotFoundException cnfe) { } } } |
Client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | /** * @(#)Client.java * * * @author * @version 1.00 2010/12/3 */ import javax.swing.*; import java.io.*; import java.net.*; public class Client { public static void main (String[] args) { try { Socket socket = new Socket("localhost",2889); ObjectOutput out = new ObjectOutputStream(socket.getOutputStream()); ObjectInput in = new ObjectInputStream(socket.getInputStream()); JFileChooser choose = new JFileChooser(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(choose); } catch(Exception e) { } int result = choose.showOpenDialog(null); if(result==JFileChooser.APPROVE_OPTION) { String filePath = choose.getSelectedFile().toString(); File file = new File(filePath); FileInputStream fis = new FileInputStream(file); int size = fis.available(); byte[] dataSend = new byte[size]; int length = fis.read(dataSend); out.writeObject(dataSend); out.flush(); } } catch(IOException ioe) { } } } |
Categories: Praktikum Sistem Operasi