0% found this document useful (0 votes)
20 views

Info Server

The document contains code for a Java program that implements an information server. The server listens on port 50000 for client connections and can respond to TIME and QUIT commands by returning the current time or closing the connection.

Uploaded by

hachibei
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 TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Info Server

The document contains code for a Java program that implements an information server. The server listens on port 50000 for client connections and can respond to TIME and QUIT commands by returning the current time or closing the connection.

Uploaded by

hachibei
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 TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.net.*; import java.io.*; import java.util.

*; public class InfoServer { private final int INFO_PORT = 50000; private String datafromClient; public InfoServer(){ BufferedReader inFromClient; DataOutputStream outToClient; Socket serverSocket; try{ /*Bind port 50000 ke alamat lokal*/ ServerSocket infoServer = new ServerSocket(INFO_PORT); System.out.println("Server sudah siap..."); /*lakukan looping tanpa henti, sampai client memberikan perintah QUI T*/ while(true){ /*masuk ke mode listening, server siap menerima permintaan dari client*/ serverSocket = infoServer.accept(); System.out.println("Ada client yang terkoneksi!"); /*Siapkan input stream dari socket dan juga sekaligus konversi d ari byte * stream ke character stream (InputStreamReader) BufferedReader akan * memudahkan dalam pengolahan data karakter */ inFromClient = new BufferedReader(new InputStreamReader(serverSo cket. getInputStream())); /*Siapkan output stream ke socket*/ outToClient = new DataOutputStream(serverSocket.getOutputStream( )); /*Tulis welcome ke client*/ outToClient.writeBytes("Info server versi 2.0 \n" + "Silahkan be rikan perintah TIME | QUIT\n"); /*lakukan perulangan sampai client mengirimkan perintah QUIT*/ boolean isQuit = false; while (isQuit) { /*baca dari client*/ datafromClient = inFromClient.readLine(); if (datafromClient.startsWith("TIME")) { outToClient.writeBytes(new Date().toString() + "\n"); } else if (datafromClient.startsWith("QUIT")) { isQuit = true; } } outToClient.close(); inFromClient.close(); serverSocket.close(); System.out.println("Koneksi client tertutup..."); } }catch(IOException e){ System.out.println("Error " + e); } } public static void main(String[] args){ InfoServer infoServer = new InfoServer();

} }

You might also like