Program Java InetAddress dengan GUI
04.35.001. Program Server
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class ProgramServer extends JFrame {
private JTextArea jta = new JTextArea();
public static void main(String[]args){
new ProgramServer();
}
public ProgramServer(){
setLayout(new BorderLayout());
add(new JScrollPane(jta),BorderLayout.CENTER);
setTitle("Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
//memulai server pada port 8000
ServerSocket a=new ServerSocket(8000);
jta.append("Server dimulai pukul "+ new java.util.Date() + '\n');
//membuka layanan untuk dapat menerima request dari client
Socket soket =a.accept();
//mengirim dan menerima data
DataInputStream masukanClient = new DataInputStream(
soket.getInputStream());
DataOutputStream keluaranDiClient = new DataOutputStream(
soket.getOutputStream());
while(true){
//mendapatkan data jari2 dari client
double jari2 = masukanClient.readDouble();
//menghitung luas lingkaran
double luas = jari2 * jari2 * 22 / 7;
//mengirim data berupa luas lingkaran ke client
keluaranDiClient.writeDouble(luas);
jta.append("Ada request dari client" + "\n");
//Mengetahui Host Name dan IP Address Client
InetAddress inetAddressClient = soket.getInetAddress();
jta.append("Nama Host: " + inetAddressClient.getHostName()+ "\n");
jta.append("IP Address: " + inetAddressClient.getHostAddress()+ "\n\n");
//menampilkan jari jari dari client
jta.append("Jari-jari diterima dari client : " + jari2 + "\n");
jta.append("Luas nya : " + luas +"\n==============\n");
}
}catch(IOException ex){
System.err.println(ex);
}
}
}
2. Program Client
import java.io.*; //mengambil paket java.io.*;
import java.net.*;//mengambil paket java.net.*
import java.awt.*;//mengambil paket java.awt.*
import java.awt.event.*;//mengambil paket java.awt.event.*
import javax.swing.*; //mengambil paket javax.swing.*
public class ProgramClient extends JFrame{
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream keServer;
private DataInputStream dariServer;
public static void main(String[]args){
new ProgramClient();
}
public ProgramClient(){
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Masukkan jari-jari"), BorderLayout.WEST);
p.add(jtf, BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
setLayout(new BorderLayout());
add(p,BorderLayout.NORTH);
add(new JScrollPane(jta), BorderLayout.CENTER);
jtf.addActionListener(new ButtonListener());
setTitle("Program Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
//Membuat koneksi ke server pada port 8000
Socket soket = new Socket("192.168.1.2",8000);
dariServer = new DataInputStream(
soket.getInputStream());
keServer = new DataOutputStream(soket.getOutputStream());
}catch(IOException ex){
jta.append(ex.toString()+"\n");
}
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
//mendapatkan input jari2 lingkaran dari keyboard
double jari2 = Double.parseDouble(jtf.getText().trim());
//mengirim nilai jari2 lingkaran ke server
keServer.writeDouble(jari2);
keServer.flush();
//menerima luas lingkaran dari server
double luas = dariServer.readDouble();
//menampilkan jari jari
jta.append("Jari-jarinya : "+jari2+"\n");
jta.append("Luas yang diterima dari server : "+luas+"\n");
}catch(IOException ex){
System.err.println(ex);
}
}
}
}
3. ScreesShoot Program
4. Penjelasan Program
Download
0 komentar