program getIP ketik script berikut pada textpad kemudian save file dengan ekstensi .java
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; i0) {
System.out.print("."); } System.out.print(ip[i] & 0xff); }System.out.println();
}
}
kemudian compile file yang telah dibuat maka akan tampil gambar seperti dibawah ini :
program getIP :
program getName digunakan untuk menampilkan nama komputer, berikut script 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());}}
setelah selesai kemudian save file dengan ekstensi file .java
maka akan tampil seperti dibawah ini :
IPtoName mengubah IP menjadi nama berikut adalah scriptnya :
import java.net.*;
public class IPtoName { public static void main (String args[]) {
if (args.length == 0) { System.out.println("Pemakaian: java IPtoName ");
System.exit(0);
}
String host = args[0]; InetAddress address = null;
try{
address = InetAddress.getByName(host);
} catch (UnknownHostException e) {
System.out.println("invalid IP - malformad IP"); System.exit(0);
}
System.out.println(address.getHostName()); } }
Nslookup :
import java.net.*;
public class NsLookup { public static void main (String args[]) {
if (args.length == 0) {
System.out.println("Pemakaian : java NsLookup ");
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 0) System.out.print("."); System.out.print((ip[i]) & 0xff);
}
System.out.println();
}
}
Script untuk menampilkan program 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 io) {
io.printStackTrace();
}}}
program untuk objek client :
import java.net.*;
import java.io.*;
public class ObjectClient{
private static int SRV_PORT = 5000;
private static ObjectOutputStream os=null;
public static void main(String argv[]) throws Exception{
try{
//membuat soket client
Socket soketClient= new Socket("127.0.0.1", SRV_PORT);
//membuat stream untuk pengiriman obyek
os= new
ObjectOutputStream(soketClient.getOutputStream());
//membuat obyek dan mengirimkannya lewat stream obyek
Staff pegawai= new Staff("Arief","IT",21); os.writeObject(pegawai);
System.out.println("Client mengirim data pegawai:");
pegawai.print();
}
catch (Exception e){
e.printStackTrace();
}}
}
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
Script untuk menampilkan program 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 io) {
io.printStackTrace();
}}}
program untuk objek client :
import java.net.*;
import java.io.*;
public class ObjectClient{
private static int SRV_PORT = 5000;
private static ObjectOutputStream os=null;
public static void main(String argv[]) throws Exception{
try{
//membuat soket client
Socket soketClient= new Socket("127.0.0.1", SRV_PORT);
//membuat stream untuk pengiriman obyek
os= new
ObjectOutputStream(soketClient.getOutputStream());
//membuat obyek dan mengirimkannya lewat stream obyek
Staff pegawai= new Staff("Arief","IT",21); os.writeObject(pegawai);
System.out.println("Client mengirim data pegawai:");
pegawai.print();
}
catch (Exception e){
e.printStackTrace();
}}
}