Program untuk mengetahui IP.
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama getIP.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; i
{
if (i > 0)
{
System.out.print(".");
}
System.out.print(ip[i] & 0xff);
}
System.out.println();
}
}
Program untuk mengetahui Host Name.
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama getName.java
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());
}
}
Program untuk mengetahui Host Name melalui IP.
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama IPtoName.java
import java.net.*;
public class IPtoName
{
public static void main(String args[])
{
{
public static void main(String args[])
{
if (args.length == 0)
{
System.out.println("Pemakaian: java IPtoName");
{
System.out.println("Pemakaian: java IPtoName
System.exit(0);
}
}
String host = args[0];
InetAddress address = null;
try
{
{
address = InetAddress.getByName(host);
}
catch (UnknownHostException e)\
{
}
catch (UnknownHostException e)\
{
System.out.println("invalid IP - malformed IP");
System.exit(0);
}
System.out.println(address.getHostName());
}
}
System.out.println(address.getHostName());
}
}
Program untuk mengetahui IP melalui Host Name.
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama NsLookup.java
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
{
if (i > 0) System.out.print(".");
System.out.print((ip[i]) & 0xff);
}
System.out.println();
}
}
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama simpleServer.java
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();
}
}
}
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama simpleClient.java
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);
}
}
}
Program Data Server Client.
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama Staff.java
import java.io.*;
public class Staff implements Serializable
{
String nama;
String divisi;
int umur;
public Staff(String nama, String divisi, int umur)
{
this.nama=nama;
this.divisi=divisi;
this.umur= umur;
}
public void print()
{
System.out.println("Data Staff: ");
System.out.println("Nama : " + nama);
System.out.println("Divis: " + divisi);
System.out.println("Umur : "+ umur);
}
}
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama ObjectClient.java
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("Hendry","IT",30);
os.writeObject(pegawai);
System.out.println("Client mengirim data pegawai:");
pegawai.print();
}
catch(Exception e){
e.printStackTrace();
}
}
}
Ketikan listing program dibawah ini pada notepad, kemudian save dengan nama ObjectServer.java
import java.net.*;
import java.io.*;
public class ObjectServer
{
private static int SRV_PORT=5000;
private static ObjectInputStream is=null;
public static void main(String argv[]) throws Exception
{
//membuat soket server dan menunggu koneksi
ServerSocket soketServer= new ServerSocket(SRV_PORT);
Socket soketClient= soketServer.accept();
//membuat stream untuk baca obyek
is= new ObjectInputStream(soketClient.getInputStream());
//menunggu dan membaca obyek yang dikirimkan
Staff pegawai= (Staff) is.readObject();
System.out.println("Server menerima data Pegawai");
pegawai.print();
}
}