Praktikum 3: Pemrograman UDP Socket Dan Pengantar Pemrograman Jaringan GUI
Praktikum 3: Pemrograman UDP Socket Dan Pengantar Pemrograman Jaringan GUI
-1-
clientPort); //step 7. dgramSocket.send(outPacket); //step 8. } while (true); } catch (IOException e) { e.printStackTrace(); } finally { // Jika eksepsi dilempar, tutup koneksi System.out.println("\n* Menutup koneksi... *"); dgramSocket.close(); //step 9. } } }
public static void main(String[] args) { try { host = InetAddress.getLocalHost(); } catch (UnknownHostException e) { System.out.println("Host tidak ditemukan"); System.exit(1); } run(); } private static void run() { try { dgramSocket = new DatagramSocket(); //step 1. //mengatur aliran data untuk masukan keyboard BufferedReader userEntry = new BufferedReader( new InputStreamReader(System.in)); String pesan = "", respon = ""; do { System.out.print("Masukan pesan: "); pesan = userEntry.readLine(); if (!pesan.equals("***TUTUP***")) { outPacket = new DatagramPacket( pesan.getBytes(), pesan.length(), host, PORT); //step 2. dgramSocket.send(outPacket); //step 3.
-2-
buffer = new byte[256]; //step 4. inPacket = new DatagramPacket(buffer, buffer.length); //step 5. dgramSocket.receive(inPacket); //step 6. respon = new String(inPacket.getData(), 0, inPacket.getLength()); //step 7. System.out.println("\nSERVER> "+ respon); } } while (!pesan.equals("***TUTUP***")); } catch (IOException e) { e.printStackTrace(); } finally { System.out.println("\n* Menutup koneksi...*"); dgramSocket.close(); } } }
3.2. Pemrograman Jaringan dengan GUI Program 3.4. Server program DaytimeServer
import java.net.*; import java.io.*; import java.util.Date; public class DaytimeServer { public static void main(String[] args){ ServerSocket server; final int DAYTIME_PORT = 13; Socket socket; try { server = new ServerSocket(DAYTIME_PORT); do { socket = server.accept(); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); Date date = new Date(); out.println(date); //Mengeksekusi metode toString socket.close(); } while (true); } catch (IOException e) { System.out.println(e); } } }
-3-
public class GetRemoteTime extends JFrame implements ActionListener { private private private private private private JTextField hostInput; JTextArea display; JButton timeButton; JButton exitButton; JPanel buttonPanel; static Socket socket = null;
public static void main(String[] args) { GetRemoteTime app = new GetRemoteTime(); app.setSize(400, 300); app.setVisible(true); app.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e){ //cek apakah ada port yang buka if (socket != null) try { socket.close(); } catch (IOException ioEx){ System.out.println("\n*** Tidak dapat menutup link!***\n"); } System.exit(0); } }); } public GetRemoteTime(){ Container pane = getContentPane(); hostInput = new JTextField(20); pane.add(hostInput, BorderLayout.NORTH); display = new JTextArea(10, 15); //dua baris berikut memastikan bahwa penyatuan kata // terjadi di dalam JTextArea display.setWrapStyleWord(true); display.setLineWrap(true);
-4-
pane.add(new JScrollPane(display), BorderLayout.CENTER); buttonPanel = new JPanel(); timeButton = new JButton("Get Date and Time"); timeButton.addActionListener(this); buttonPanel.add(timeButton); exitButton = new JButton("exit"); exitButton.addActionListener(this); buttonPanel.add(exitButton); pane.add(buttonPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent event){ if (event.getSource() == exitButton) System.exit(0); String theTime; //Menerima nama host dari user String host = hostInput.getText(); final int DAYTIME_PORT = 13; try { //Membuat obyek Socket untuk membuat koneksi dengan //host yang diinginkan pada port yang sesuai socket = new Socket(host,DAYTIME_PORT); //Membuat aliran input data untuk socket dan //tambahkan string-reading secara fungsional BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); //Menerima respon host dari aliran input di atas theTime = input.readLine(); //Tambahkan respon host pada JTextArea display.append("The date/time at " + host + " is " + theTime + "\n"); hostInput.setText(""); } catch (IOException ex) { display.append("No such host!\n"); } finally{ try { if (socket != null) socket.close(); //close connection to host } catch (IOException e) {
-5-
3.3. Tugas Praktikum Eksekusi semua keempat program server dan client dan buat laporan praktikum.
-6-