//Mohd Syafirul bin Ramli 810320145415
//Muhammad Faizal bin Arishah 800107045037
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class SimpleClientFirulApplet extends Applet implements ActionListener{
Button connectButton;
TextField noportField;
Button hantarButton;
TextField hantarField;
Socket cl = null, cl2 = null;
BufferedReader is = null;
DataOutputStream os = null;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String userInput = null;
String output = null;
int noport;
String ayatstatus1="Simple Client Server";
String ayatstatus2="Masukkan nombor port dan klik Connect";
public void init()
{
setLayout(null);
setBackground(java.awt.Color.yellow);
setSize(400,150);
connectButton = new Button("Connect");
noportField = new TextField("",10);
hantarButton = new Button("Send");
hantarField = new TextField("",10);
connectButton.setBounds(220,20,100,25);
noportField.setBounds(100,20,100,25);
hantarButton.setBounds(220,70,100,25);
hantarField.setBounds(100,70,100,25);
add(noportField);
add(connectButton);
add(hantarField);
add(hantarButton);
connectButton.addActionListener(this);
hantarButton.addActionListener(this);
}//end init()
// Utk memaparkan status pada applet
public void paint(Graphics g)
{
g.drawString(ayatstatus1,20,120);
g.drawString(ayatstatus2,20,140);
}//end paint()
// Utk respon pada event tertentu
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == connectButton){
try{
noport=Integer.parseInt(noportField.getText());
cl = new Socket("localhost",noport);
is = new BufferedReader(new InputStreamReader(cl.getInputStream()));
os = new DataOutputStream(cl.getOutputStream());
ayatstatus1="Status : connected to "+noport+ " on localhost";
ayatstatus2="Masukkan input : ";
repaint();
}catch(UnknownHostException e1) {
ayatstatus1="Unknown host: "+e1;
ayatstatus2="";
repaint();
}catch(IOException e2) {
ayatstatus1="Error io: "+e2;
ayatstatus2="";
repaint();
}
//get input from the user and send it to the greetings server
}else if (evt.getSource() == hantarButton){
try{
userInput = hantarField.getText();
os.writeBytes(userInput+"\n");
}catch(IOException ex){
ayatstatus1="error writing to server."+ex;
ayatstatus2="";
repaint();
}
//receive a reply from the greetings server.
try{
output = is.readLine();
ayatstatus1="";
ayatstatus2="Dapat dari server: " +output;
repaint();
}catch(IOException e){
e.printStackTrace();
}
//close the input stream, output stream and connection
try{
is.close();
os.close();
cl.close();
}catch(IOException x){
ayatstatus1="";
ayatstatus2="Error writing. . ."+x;
}
}//end if
}//end actionPerformed()
}//end class
