Minggu, 11 Juni 2017

Pemrograman Jaringan - Membangun Aplikasi Client-Server Sederhana menggunakan TCP

Membangun Aplikasi Client-Server Sederhana menggunakan TCP

Listing 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();

Pemrograman Jaringan - NsLookup



NsLookup
Listing
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();
}
}
Logika
Pada Program Nslookup ini adalah kebalikan dari program IptoName yang berbeda adalah Nslookup memasukkan hostname komputer untuk menampilkan IP address

Pemrograman Jaringan - IPtoName


IPtoName
Listing
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());
}
}
Logika
Pada program IptoName ini berfungsi untuk menampilakan host dengan memasukkan  IP address komputer lalu program akan memproses apabila mengenali IP address tersebut maka akan memuculkan name host nya

Pemrograman Jaringan - GetName



getName
Listing
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());
}
}


Logika
Pada program getName ini berfungsi untuk mendapatkan nama dari host yang dipakai saat program dijalankan di komputer bersangkutan pada program ini hampir sama lisitngnya dengan program getIP yang berbeda adalah sintaks

Pemrograman Jaringan - GetIP


getIP
Listing Program
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();
}
}

Logika
Pada Program getIP ini berfungsi untuk mendaptkan IP komputer