Teman-teman setelah minggu lalu kita sudah membuat program client server sederhana menggunakan ServerSocket dan Socket , pertemuan kali ini kita akan membuat sistem parkir sederhana dengan melakukan sedikit modifikasi pada program server dan client yang sudah ada.
ServerParkirSocket draft
import java.io.*; import java.net.*; import java.util.Scanner; public class ServerParkirSocket { private static ServerSocket serverSocket; public static LogKendaraan lKendaraan; private static final int PORT = 8080; public static void main(String[] args) throws IOException { lKendaraan=new LogKendaraan(); try { serverSocket = new ServerSocket(PORT); System.out.println("SERVER PARKIR PADA PORT:" + PORT); } catch (IOException ioEx) { System.out.println("\nTidak Dapat Membuka Port!"); System.exit(1); } do { System.out.println("Server Siap..."); Socket client = serverSocket.accept(); System.out.println("\nClient Baru ..masuk.." + client.getRemoteSocketAddress().toString()); ClientHandler handler = new ClientHandler(client); handler.start();//As usual, method calls run . }while (true); } } class ClientHandler extends Thread { private Socket client; private Scanner input; private PrintWriter output; public ClientHandler(Socket socket) { client = socket; try { input = new Scanner(client.getInputStream()); output = new PrintWriter( client.getOutputStream(),true); output.println("Selamat Datang di Server Parkir"); } catch(IOException ioEx){ ioEx.printStackTrace(); } } public void run() { String received=""; do { try{ received = input.nextLine(); System.out.println(received); String []data=received.split(";"); if(data!=null){ if(data[1].equalsIgnoreCase("IN")) ServerParkirSocket.lKendaraan.masuk(data[2]); } output.println("Data: " + ServerParkirSocket.lKendaraan.getInfo()); }catch(java.util.NoSuchElementException noex){} //ulangi sampai QUIT }while (!received.equalsIgnoreCase("QUIT")); try { if (client!=null) { System.out.println( "Menutup koneksi...sukses.."); client.close(); } else{ System.out.println( "Menutup koneksi...sukses.."); } } catch(IOException ioEx) { System.out.println("Gagal menutup koneksi!"); } } }
Tugas Bonus
SimpleChat Download
sesuai dengan kretifitas anda.
Thank you thank you for a very useful information.
thank.. very nice,,
http://obatherbalsipiliskronis33.wordpress.com/
Makasih info nya 🙂
Thank You
Thanks for share this was really helpful
affiliate marketing system review
ulasan anda tentang sistem parkir sederhana memang sangat sederhana namun kaya manfaat dan bisa langsung dipraktekkan karena ada panduannya.
makasih mas ilmunya