Info Client dan Info Server
Untuk membuat info server, berikut
listing programnya:
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
telah 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 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();
}
}
Untuk info client, listing
programnya adalah:
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("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();
}
}
Jalankan program info server,
biarkan tetap terbuka:
Kemudian jalankan info client,
masukkan perintah “Time” untuk mengetahui waktu pada komputer dan perintah “Net”
untuk mengetahui IP
0 komentar:
Posting Komentar