Socket DataStream untuk Tipe Data Boolean
06.33.001. Program Server
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class BooleanServer extends JFrame {
private JTextArea jta = new JTextArea();
public static void main(String[]args){
new BooleanServer();
}
public BooleanServer(){
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){
//membuat variable pesanb yang berisi pesan dari client
boolean pesanb = masukanClient.readBoolean();
//membuat variable pesanServer yang akan di kirim ke client
boolean pesanServer = false;
//mengirim pesan ke client
keluaranDiClient.writeBoolean(pesanServer);
jta.append("Ada request dari client" + "\n");
//menampilkan pesan dari client
jta.append("pesan yang diterima dari client : " + pesanb + "\n");
//menampilkan pesan yang dikirim ke server
jta.append("pesan dari server : " + pesanServer +"\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 BooleanClient 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 BooleanClient();
}
public BooleanClient(){
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Masukkan data boolean"), 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 pesan dari keyboard
boolean pesanb = Boolean.parseBoolean(jtf.getText().trim());
//mengirim pesan ke server
keServer.writeBoolean(pesanb);
keServer.flush();
//menerima pesan dari server
boolean pesanServer = dariServer.readBoolean();
//menampilkan pesan
jta.append("pesan dari client : "+pesanb+"\n");
jta.append("pesan dari server : "+pesanServer+"\n");
}catch(IOException ex){
System.err.println(ex);
}
}
}
}
3. ScreenShoot Program
0 komentar