Socket DataStream untuk Tipe Data Char
06.46.001. Program Server
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
public class CharServer extends JFrame {
private JTextArea jta = new JTextArea();
public static void main(String[]args){
new CharServer();
}
public CharServer(){
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 pesan yang berisi pesan dari client
char dariClient = masukanClient.readChar();
//membuat variable pesanServer yang akan di kirim ke client
char keluaranClient ='A';
//mengirim pesan ke client
keluaranDiClient.writeChar(keluaranClient);
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 pesan dari client
jta.append("pesan yang diterima dari client : " + dariClient + "\n");
//menampilkan pesan yang dikirim ke server
jta.append("pesan dari server : " + keluaranClient +"\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 CharClient 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 CharClient();
}
public CharClient(){
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Masukkan data char"), 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
char pesan = jtf.getText().charAt(0);
//mengirim pesan ke server
keServer.writeChar(pesan);
keServer.flush();
//menerima pesan dari server
char pesanServer = dariServer.readChar();
//menampilkan pesan
jta.append("pesan dari client : "+pesan+"\n");
jta.append("pesan dari server : "+pesanServer+"\n");
}catch(IOException ex){
System.err.println(ex);
}
}
}
}
3. ScreenShoot Program
4. Penjelasan Program
Download
0 komentar