.:For Network Programming ( SCN 4223 ) Purpose Only:.

AC990856 Page

--»Assigment 4 ---- SimpleServerFirul.java , SimpleClientFirulApplet.java (Group Assg & consist 2 mmber)

--SimpleServerFirul.java
//Mohd Syafirul bin Ramli 810320145415
//Muhammad Faizal bin Arishah 800107045037

import java.io.*;
import java.net.*;
import java.util.*;

public class SimpleServerFirul {
public final static int TESTPORT = 5000;
public static void main(String args[]) {
//declaration section:
//declare a server socket and a client socket for the server
//declare an input stream and an ooutput stream
ServerSocket checkServer = null;
String line;
BufferedReader is = null;
DataOutputStream os = null;
Socket clientSocket = null;
Date tarikh;
String firul;
String port="5000";

//try to open a server socket on port TESTPORT
//note that you can't choose a port less than 1023 if you are not privileged users (root)

try{
int noport=Integer.parseInt(port);
checkServer = new ServerSocket(noport);
System.out.println("SimpleServerFirul running . . . ");
}catch (IOException e) {
System.out.println(e);
}

//open input and output stream
try{
clientSocket = checkServer.accept();
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os= new DataOutputStream(clientSocket.getOutputStream());
}catch(Exception ei) {
ei.printStackTrace();
}

//receive the client's message and check if it contains "Greetings"'

try{

line=is.readLine();
System.out.println("Server terima : "+line);
if (line.compareTo("Assalamualaikum")==0) {
os.writeBytes("...Waalaikumussalam...");
}else if (line.compareTo("Tarikh?")==0) {
tarikh=new Date();
os.writeBytes(tarikh.toString());}
else{
os.writeBytes("Maaf, apa yang awak cakap nih ?");
}

}catch (IOException e) {
System.out.println(e);
}


//close the input/output streams and the connection
try{
os.close();
is.close();
clientSocket.close();
}catch(IOException ic){
ic.printStackTrace();
}
}//end main
}//end class

 

--SimpleClientFirulApplet.java

//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

 

Copyright @2003 Ac990856
U6C3-9,Kolej Perdana.
Tel: 6012-7615174 ;
e-mail: masalapang@hotmail.com.my