Teman-teman untuk pertemuan kali ini silakan:
- Membentuk kelompok yang terdiri dari 3 s/d 4 mahasiswa
- Setiap kelompok menyiapkan jaringan komputer
- Salah satu komputer difungsikan sebagai server dan komputer yang lain sebagai client
- Lakukan percobaan fungsi server dan client
Server
import java.io.*; import java.net.*; import java.util.*; public class TCPEchoServer { private static ServerSocket serverSocket; private static final int PORT = 1234; public static void main(String[] args) { System.out.println("Membuka server pada PORT: "+ PORT +"...\n"); try { serverSocket = new ServerSocket(PORT); //Step 1. } catch(IOException ioEx) { System.out.println( "Unable to attach to port!"); System.exit(1); } do { handleClient(); }while (true); } private static void handleClient() { Socket link = null; //Step 2. try { link = serverSocket.accept(); //Step 2. Scanner input = new Scanner(link.getInputStream()); //Step 3. PrintWriter output = new PrintWriter( link.getOutputStream(),true); //Step 3. int numMessages = 0; String message = input.nextLine(); //Step 4. while (!message.equalsIgnoreCase("***tutup***")) { System.out.println("Pesan diterima."); numMessages++; output.println("Pesan " + numMessages + ": " + message); //Step 4. message = input.nextLine(); } output.println(numMessages + " pesan diterima server."); //Step 4. } catch(IOException ioEx) { ioEx.printStackTrace(); } finally { try { System.out.println( "\n* Koneksi ditutup... *"); link.close(); //Step 5. } catch(IOException ioEx) { System.out.println( "Gagal memutuskan koneksi!"); System.exit(1); } } } }
Client
import java.io.*; import java.net.*; import java.util.*; public class TCPEchoClient { private static InetAddress host; private static final int PORT = 1234; public static void main(String[] args) { try { host = InetAddress.getLocalHost(); } catch (UnknownHostException uhEx) { System.out.println("Host ID tidak ditemukan!"); System.exit(1); } accessServer(); } private static void accessServer() { Socket link = null; //Step 1. try { link = new Socket(host,PORT); //Step 1.membuka input stream Scanner input = new Scanner(link.getInputStream()); //Step 2.membuka output stream PrintWriter output = new PrintWriter( link.getOutputStream(),true); //Step 2. //Set up stream untuk masukan dari keyboard Scanner userEntry = new Scanner(System.in); String message, response; do { System.out.print("Masukan Pesan : "); message = userEntry.nextLine(); output.println(message); //Step 3. response = input.nextLine(); //Step 3. System.out.println("\n"+new Date() +"SERVER> "+response); }while (!message.equals("***tutup***")); } catch(IOException ioEx) { ioEx.printStackTrace(); } finally { try { System.out.println( "\n* Closing connection... *"); link.close(); //Step 4. } catch(IOException ioEx) { System.out.println( "Unable to disconnect!"); System.exit(1); } } } }
Server Mutli Client
import java.io.*; import java.net.*; import java.util.Scanner; public class MultiEchoServer { private static ServerSocket serverSocket; private static final int PORT = 1234; public static void main(String[] args) throws IOException { try { serverSocket = new ServerSocket(PORT); System.out.println("MULTI CLIENT SERVER pada PORT:" + PORT); } catch (IOException ioEx) { System.out.println("\nTidak Dapat Membuka Port!"); System.exit(1); } do { //Tunggu Koneksi System.out.println("Server Siap..."); Socket client = serverSocket.accept(); System.out.println("\nClient Baru ..masuk..\n"); //Create a thread to handle communication with //this client and pass the constructor for this //thread a reference to the relevant socket... 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) { //Set up reference to associated socket... client = socket; try { input = new Scanner(client.getInputStream()); output = new PrintWriter( client.getOutputStream(),true); } catch(IOException ioEx){ ioEx.printStackTrace(); } } public void run() { String received; do { //menerima pesan dari client //melalui socket input stream.. received = input.nextLine(); //Menampilkan kembali pesan ke client //melalui socket output stream... output.println("ECHO: " + received); //ulangi sampai QUIT }while (!received.equalsIgnoreCase("QUIT")); try { if (client!=null) { System.out.println( "Menutup koneksi...sukses.."); client.close(); } } catch(IOException ioEx) { System.out.println("Gagal menutup koneksi!"); } } }
Client Multi
import java.io.*; import java.net.*; import java.util.*; public class MultiEchoClient { private static InetAddress host; private static final int PORT = 1234; public static void main(String[] args) { try { host = InetAddress.getLocalHost(); } catch(UnknownHostException uhEx) { System.out.println("\nHost ID tidak ditemukan!\n"); System.exit(1); } sendMessages(); } private static void sendMessages() { Socket socket = null; try { socket = new Socket(host,PORT); Scanner networkInput = new Scanner(socket.getInputStream()); PrintWriter networkOutput = new PrintWriter( socket.getOutputStream(),true); //Set up stream for keyboard entry... Scanner userEntry = new Scanner(System.in); String message, response; do { System.out.print( "Tuliskan PESAN ('QUIT' untuk keluar program): "); message = userEntry.nextLine(); networkOutput.println(message); response = networkInput.nextLine(); System.out.println( "\nSERVER> " + response); }while (!message.equalsIgnoreCase("QUIT")); } catch(IOException ioEx) { ioEx.printStackTrace(); } finally { try { System.out.println( "\nPenutupan Koneksi..."); socket.close(); } catch(IOException ioEx) { System.out.println( "Unable to disconnect!"); System.exit(1); } } } }