This document discusses using Java programming language to view information between an info server and info client. It provides the code for an InfoServer class that establishes a server socket on port 50000 and responds with time, network address or quit depending on the client request. It also provides the code for an InfoClient class that connects to the server, sends commands and displays the response. Running both programs results in the client connecting to the server and being able to request time, network or quit information from the server.
This document discusses using Java programming language to view information between an info server and info client. It provides the code for an InfoServer class that establishes a server socket on port 50000 and responds with time, network address or quit depending on the client request. It also provides the code for an InfoClient class that connects to the server, sends commands and displays the response. Running both programs results in the client connecting to the server and being able to request time, network or quit information from the server.
This document discusses using Java programming language to view information between an info server and info client. It provides the code for an InfoServer class that establishes a server socket on port 50000 and responds with time, network address or quit depending on the client request. It also provides the code for an InfoClient class that connects to the server, sends commands and displays the response. Running both programs results in the client connecting to the server and being able to request time, network or quit information from the server.
This document discusses using Java programming language to view information between an info server and info client. It provides the code for an InfoServer class that establishes a server socket on port 50000 and responds with time, network address or quit depending on the client request. It also provides the code for an InfoClient class that connects to the server, sends commands and displays the response. Running both programs results in the client connecting to the server and being able to request time, network or quit information from the server.
Oleh : M. Rizki Adji jaya (061130701306) 6CD JURUSAN TEKNIK KOMPUTER POLITEKNIK NEGERI SRIWIJAYA PALEMBANG Berikut adalah listing codenya dan eksekusi dari program yang telah dibuat. A) infoserver.java import java.io.*; import java.net.*; import java.util.*; public class infoserver { private final int INFO_PORT=50000; private String datafromClient; public infoserver() { BufferedReader inFromClient; DataOutputStream outToClient; Socket serverSocket; try { ServerSocket infoserver = new ServerSocket(INFO_PORT); System.out.println("Server siap..."); while (true){ serverSocket = infoserver.accept(); System.out.println("Ada client" + "yang terkoneksi!"); inFromClient = new BufferedReader( new InputStreamReader( serverSocket.getInputStream())); outToClient = new DataOutputStream( serverSocket.getOutputStream()); outToClient.writeBytes("InfoServer versi 0.1\n"+ "hanya untuk testing..\n"+ "Silahkan berikan perintah TIME|NET|QUIT\n"); boolean isQUIT = false; while (!isQUIT) { datafromClient = inFromClient.readLine(); if (datafromClient.startsWith("TIME")){ outToClient.writeBytes(new Date().toString() + "\n"); } else if (datafromClient.startsWith("NET")){ outToClient.writeBytes( InetAddress.getByName("localhost").toString()+ "\n"); } else if (datafromClient.startsWith("QUIT")) { isQUIT = true; } } outToClient.close(); inFromClient.close(); serverSocket.close(); System.out.println("Koneksi client sudah tertutup.."); } } catch (IOException ioe) { System.out.print("error:" + ioe); } catch (Exception e) { System.out.print("error:" + e); } } public static void main(String[]args) { new infoserver(); } } Jalankan program yang telah dibuat. Dengan cara klik ctrl+1 lalu ctrl+2, maka tampilan server akan seperti berikut: B) InfoClient.java import java.net.*; import java.io.*; import java.util.*; public class InfoClient { private final int INFO_PORT=50000; private final String TargetHost = "localhost"; private final String QUIT = "QUIT"; public InfoClient() { try { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket(TargetHost, INFO_PORT); DataOutputStream outToServer = new DataOutputStream( clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); System.out.println(inFromServer.readLine()); System.out.println(inFromServer.readLine()); System.out.println(inFromServer.readLine()); System.out.println(""); boolean isQuit = false; while (!isQuit) { System.out.print("Ketikkan Perintah Anda : "); String cmd = inFromUser.readLine(); cmd = cmd.toUpperCase(); if (cmd.equals(QUIT)) { isQuit = true; } outToServer.writeBytes(cmd + "\n"); String result = inFromServer.readLine(); System.out.println("Dari Server: " + result); } outToServer.close(); inFromServer.close(); clientSocket.close(); } catch (IOException ioe) { System.out.println("Error:" + ioe); } catch (Exception e) { System.out.println("Error:" + e); } } public static void main(String[]args) { new InfoClient(); } } Kemudian running juga program InfoClient.java dengan cara klik ctrl+1 lalu ctrl+2, maka tampilan akan seperti berikut: Lihat ke bagian jendela server. Ketika Client telah terkoneksi dengan server, maka tampilan akan seperti berikut: Lalu kembali ke jendela Client untuk memilih perintah. Masukkan perintah yang anda ingin, seperti ini untuk melihat waktu, dan jaringan yang terdapat di PC/Laptop anda.