Pemograman jaringan komputer 4

Posted by stefanus dear damanik Kamis, 03 Juli 2014 0 komentar
TUGAS IV
Melihat Info Server dan Info Client Menggunakan
Bahasa Pemrograman Java



Oleh :


Stefanus Dear Damanik
(061130701309)
6CD


JURUSAN TEKNIK KOMPUTER
POLITEKNIK NEGERI SRIWIJAYA
PALEMBANG

Pada bagian kali ini, Program yang dibuat, guna untuk mengetahui info server dan info client suatu komputer. 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();
}

}

untuk lebih lengkap download panduannya disini klik here

0 komentar:

Posting Komentar

pencarian blog

Total Tayangan Laman