0% found this document useful (0 votes)
44 views6 pages

Praktikum 3: Pemrograman UDP Socket Dan Pengantar Pemrograman Jaringan GUI

The document describes programs for UDP socket programming and GUI network programming in Java. It includes the code for a UDP echo server and client that exchange messages. It also includes a daytime server that returns the current date/time and a client that requests the time from the server and displays it. The practical assignment is to execute all four server and client programs and create a report on the practical work.

Uploaded by

Pemain Lama
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

Praktikum 3: Pemrograman UDP Socket Dan Pengantar Pemrograman Jaringan GUI

The document describes programs for UDP socket programming and GUI network programming in Java. It includes the code for a UDP echo server and client that exchange messages. It also includes a daytime server that returns the current date/time and a client that requests the time from the server and displays it. The practical assignment is to execute all four server and client programs and create a report on the practical work.

Uploaded by

Pemain Lama
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

PRAKTIKUM 3

Pemrograman UDP Socket dan Pengantar Pemrograman Jaringan GUI

3.1. Pemrograman UDP Socket Program 3.1. Server program UDPEchoServer


import java.io.*; import java.net.*; public class UDPEchoServer { private static final int PORT = 1234; private static DatagramSocket dgramSocket; private static DatagramPacket inPacket, outPacket; private static byte[] buffer; public static void main(String[] args) { System.out.println("Membuka port...\n"); try { dgramSocket = new DatagramSocket(PORT); //step 1. } catch (SocketException e) { System.out.println("Tidak dapat membuka port!"); System.exit(1); } run(); } private static void run() { try { String pesanIn, pesanOut; int numPesan = 0; do { buffer = new byte[256]; //step 2. inPacket = new DatagramPacket(buffer, buffer.length); //step 3. dgramSocket.receive(inPacket); //step 4. InetAddress clientAddress = inPacket.getAddress(); //step 5. int clientPort = inPacket.getPort(); //step 5. pesanIn = new String(inPacket.getData(), 0, inPacket.getLength()); //step 6. System.out.println("Pesan diterima."); numPesan++; pesanOut = ("Pesan "+ numPesan + ": "+ pesanIn); outPacket = new DatagramPacket(pesanOut.getBytes(), pesanOut.length(), clientAddress,

-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. } } }

Program 3.2. Client program UDPEchoClient


import java.io.*; import java.net.*; public class UDPEchoClient { private private private private private static static static static static InetAddress host; final int PORT = 1234; DatagramSocket dgramSocket; DatagramPacket inPacket,outPacket; byte[] buffer;

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-

Program 3.4. Client program GetRemoteTime


import import import import import java.awt.*; java.awt.event.*; javax.swing.*; java.net.*; java.io.*;

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-

System.out.println("Unable to disconnect!"); System.exit(1); } } } }

3.3. Tugas Praktikum Eksekusi semua keempat program server dan client dan buat laporan praktikum.

-6-

You might also like