Pages

Jumat, 05 Mei 2017

Logika Program Pemrograman Jaringan

Koding GetIP


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();
}
}
Kodingan diatas digunakan untuk mendapatkan alamat ip address . didalam kodingan ini ip address di masukan null yang artinya belum ditentukan alamat ip addressnya, kemudian baru program akan mendapatkan alamat ip address dari localhost pada komputer masing-masing.
Output
Koding GetName

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());
}
}
Koding diatas berfungsi untuk menampilkan nama dari komputer anda sesuai dengan ip local host yang sudah ditampilkan pada koding getip diatas.
Output
Koding 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());
}
}
Koding diatas berfungi untuk menyimpan alamat ip yang kita input terlebih dahulu bukan mendapatkan alamat ip address dari komputer kita.  Didalam koding diatas terdapat fungsi percabangan dimana jika tidak ada alamat ip address yang kita input maka program akan menampilkan "Pemakaian: java IPtoName <IP address>" lalu keluar, jika kita input alamat ip addreessnya maka yang akan muncul adalah alamar ip address yang kita input.
Output
Koding Ns LookUP

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();
}
}
Koding diatas fungsinya tidak berbeda jauh dengan koding IptoName, bedanya dalam koding ini yang kita inputkan adalah hostname dari komputer kita. Untuk alur program juga tidak berbeda jauh jika kita tidak inputkan hostnamenya maka akan muncul “Pemakaian: java NsLookup <hostname>” , jika kita input hostnamenya maka akan muncul hostname yang kita input dan apabila kita menginput null maka yang akan tampil adalah "Unknown host".
Output
Koding Client Server
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();
}
}
}
Koding diatas berfungsi sebagai server yang akan melayani client, dimana server ini akan memerisa setiap port yang sudah ditentukan apabila port tersebut dalam keadaan hidup maka server akan “Aplikasi Server Hidup” .


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;
// Membuka koneksi 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);
}
// Menulis 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);
}
// Menerima tanggapan 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);
}
}
}
Koding diatas berfungsi sebagai client, jika server sudah hidup maka server akan mengirimkan sebuaah salam kepada client sebagai tanda bahwa client dan server sudah saling terhubung satu sama lain.
Output



0 komentar:

Posting Komentar