Selasa, 02 Mei 2017

Tugas Progjar individu pertama



1. Get IP

import java.net.*;
public class getIP {
public static void main(String args[]) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
byte ip[] = host.getAddress();
for (int i=0; i<ip.length; i++) {
if (i > 0) {
System.out.print(".");
}
System.out.print(ip[i] & 0xff);
}
System.out.println();
}
}
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvVVVkUfJoyEsnI1-r0_CEbDzd7ynRKA12mvM39pqJvwQkFzHFxZk4Vncnz05YNOMU6O9KKIaBxDnmLVlxdghDvBFyCoYAr9epZUQNWFvUOTZJ_EiI9v7snPx6kmnoIHwynU9PAImFxDQ/s320/getIP_1.PNG

Fungsi dari kodingan diatas untuk mempunyai sebuah alamat IP yang ada pada dekstop

2. Get Name

import java.net.*;
public class getName {
public static void main(String args[]) throws Exception {
InetAddress host = null;
host = InetAddress.getLocalHost();
System.out.println("Nama komputer Anda: " +
host.getHostName());
}
}

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDdHssGb6k9e1g5q59wuI8qlPnDk8gHFgr88jz6kRUh8DeFsXCZYqT83cj7BbZIjirdNnptnSWH7VY0b0fhEe-tR9a2P41nopw-4NJZx-QWurdmNW4RYil5-gHx7wp_6VqRAgXpzKwzTQ/s320/getName_2.PNG

3. IP to Name

import java.net.*;
public class IPtoName {
public static void main(String args[]) {
if (args.length == 0) {
System.out.println("Pemakaian: java IPtoName <IP address>");
System.exit(0);
}
String host = args[0];
InetAddress address = null;
try {
address = InetAddress.getByName(host);
} catch (UnknownHostException e) {
System.out.println("invalid IP - malformed IP");
System.exit(0);
}
System.out.println(address.getHostName());
}
}

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhv1XKdPTe3bwGsuVSsquN5SoSXiXoODQM1281ywrOcw-5-3PKHDZZHluhvNum0FHYbaqE3ihcBRjLk5bMy4A9oZ_bEzqTT78sWs8IIXNpewDibvVqU_ueJiOTPdpWFSRRn24vLHidu2A4/s320/IPtoName_3.PNG


4. NsLookup

import java.net.*;
public class NsLookup {
public static void main(String args[]) {
if (args.length == 0) {
System.out.println("Pemakaian: java NsLookup <hostname>");
System.exit(0);
}
String host = args[0];
InetAddress address = null;
try {
address = InetAddress.getByName(host);
} catch(UnknownHostException e) {
System.out.println("Unknown host");
System.exit(0);
}
byte[] ip = address.getAddress();
for (int i=0; i<ip.length; i++) {
if (i > 0) System.out.print(".");
System.out.print((ip[i]) & 0xff);
}
System.out.println();
}
}

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUI77lFE1EVXT4n4fGblYWTuDFz7v3NEC0T_Op42HezgmoR7HQSBhIuCsYZCVuU1mUz-RIgFSza0A-DigfNXsmYQCzc0GUXjlHLpmfwVpwhzmIJfD9rPWZD_MTDurpLPKlQJfVS2l-0es/s320/NsLookup_4.PNG


5. Membangun aplikasi server dan client

a. Simple Server

import java.io.*;
import java.net.*;
public class simpleServer {
public final static int TESTPORT = 5000;
public static void main(String args[]) {
ServerSocket checkServer = null;
String line;
BufferedReader is = null;
DataOutputStream os = null;
Socket clientSocket = null;
try {
checkServer = new ServerSocket(TESTPORT);
System.out.println("Aplikasi Server hidup ...");
} catch (IOException e) {
System.out.println(e);
}
try {
clientSocket = checkServer.accept();
is = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
os = new DataOutputStream(clientSocket.getOutputStream());
} catch (Exception ei) {
ei.printStackTrace();
}
try {
line = is.readLine();
System.out.println("Terima : " + line);
if (line.compareTo("salam") == 0) {
os.writeBytes("salam juga");
} else {
os.writeBytes("Maaf, saya tidak mengerti");
}
} catch (IOException e) {
System.out.println(e);
}
try {
os.close();
is.close();
clientSocket.close();
} catch (IOException ic) {
ic.printStackTrace();
}
}
}

b. Simple Client

import java.io.*;
import java.net.*;
public class simpleClient {
public final static int REMOTE_PORT = 5000;
public static void main(String args[]) throws Exception {
Socket cl = null;
BufferedReader is = null;
DataOutputStream os = null;
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
String userInput = null;
String output = null;
// Memberikan sebuah jalan agar dapat terkoneksi ke server pada port REMOTE_PORT
try {
cl = new Socket(args[0], REMOTE_PORT);
is = new BufferedReader(new
InputStreamReader(cl.getInputStream()));
os = new DataOutputStream(cl.getOutputStream());
} catch(UnknownHostException e1) {
System.out.println("Unknown Host: " + e1);
} catch (IOException e2) {
System.out.println("Erorr io: " + e2);
}
// Memberi tau ke server
try {
System.out.print("Masukkan kata kunci: ");
userInput = stdin.readLine();
os.writeBytes(userInput + "\n");
} catch (IOException ex) {
System.out.println("Error writing to server..." + ex);
}
// Mendapatkan sebuah pemberitahuana dari server
try {
output = is.readLine();
System.out.println("Dari server: " + output);
} catch (IOException e) {
e.printStackTrace();
}
// close input stream, output stream dan koneksi
try {
is.close();
os.close();
cl.close();
} catch (IOException x) {
System.out.println("Error writing...." + x);
}
}
}
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhthoOrzltkO7J9QAEIubne27dE4A6yVwpQ7oHDQyI6xAewA8xF1FOkxhbsydaDoZc0LAKUZZJCduxHynveYybXRpAj0BQs-7d3f_krZ1pefEMcGVEoasKrWWgnPtRbYKV6t-eCE3vyTW0/s320/serverclient_5.PNG

Tidak ada komentar:

Posting Komentar