1. Class Server.java
package ProjectDariPakTresna;
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args )
{
try {
ServerSocket socket = new ServerSocket(80);
Socket insocket = socket.accept();
PrintWriter out = new PrintWriter (
insocket.getOutputStream(), true);
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 "
+ "transitional//EN'>");
out.println("<html>");
out.println("<head>");
out.println("<title>");
out.println("A new web page");
out.println("</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>");
out.println("A custom web server! Not bad.");
out.println("</h1>");
out.println("</body>");
out.println("</html>");
insocket.close();
}
catch (Exception e) {}
}
}
2. Class Intercom 1
package ProjectDariPakTresna;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Intercom1 extends Frame implements Runnable, ActionListener
{
private Thread thread;
private Button button1;
private TextArea textarea1, textarea2;
private Label label1, label2, label3;
ServerSocket socket;
PrintWriter out;
Socket insocket;
public static void main(String[] args)
{
new Intercom1();
}
public Intercom1()
{
setLayout(null);
button1 = new Button("KIRIM");
button1.setBounds(160, 660, 60, 20);
add(button1);
button1.addActionListener(this);
label3 = new Label();
label3.setBounds(20, 80, 200, 20);
label3.setText("Kotak Masuk :");
add(label3);
textarea1 = new TextArea("", 7, 45,
TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea1.setBounds(20, 100, 360, 260);
add(textarea1);
label1 = new Label();
label1.setBounds(20, 370, 200, 20);
label1.setText("Tuliskan Pesan Anda Disini:");
add(label1);
textarea2 = new TextArea("", 7, 45,
TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea2.setBounds(20, 390, 360, 260);
add(textarea2);
label2 = new Label("Pesbuk 1");
label2.setFont(new Font("Times New Roman", Font.BOLD, 36));
label2.setBounds(100, 45, 200, 30);
add(label2);
setSize(400, 700);
setTitle("Pesbuk 1");
setVisible(true);
textarea2.requestFocus();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(
WindowEvent e){
System.exit(0);
try{
socket.close();
}catch(Exception ex){}
}
}
);
try {
socket = new ServerSocket(8765);
insocket = socket.accept( );
out = new PrintWriter (insocket.getOutputStream(), true);
thread = new Thread(this);
thread.start();
}
catch (Exception e)
{
textarea1.setText(e.getMessage());
}
}
public void run()
{
String instring;
try {
BufferedReader in = new BufferedReader (new
InputStreamReader(insocket.getInputStream()));
while((instring = in.readLine()) != null){
textarea1.append(instring + "\n");
}
}catch (Exception e)
{
textarea1.setText(e.getMessage());
}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button1){
String text = textarea2.getText();
textarea2.setText("");
out.println(text);
textarea2.requestFocus();
}
}
}
3. Class Intercom 2
package ProjectDariPakTresna;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
class Intercom2 extends Frame implements Runnable, ActionListener
{
private Thread thread;
private Button button1, button2;
private TextArea textarea1, textarea2;
private TextField textfield1;
private Label label1, label2, label3;
Socket socket;
InputStream in;
OutputStream out;
int character;
char[] chars = new char[1];
public static void main(String[] args)
{
new Intercom2();
}
public Intercom2()
{
setLayout(null);
label1 = new Label("Pesbuk 1 IP Address:");
label1.setBounds(20, 80, 125, 20);
add(label1);
textfield1 = new TextField("127.0.0.1");
textfield1.setBounds(150, 80, 110, 20);
add(textfield1);
button1 = new Button("Koneksikan");
button1.setBounds(270, 80, 110, 20);
add(button1);
button1.addActionListener(this);
button2 = new Button("KIRIM");
button2.setBounds(160, 660, 60, 20);
add(button2);
button2.addActionListener(this);
textarea1 = new TextArea("", 7, 45,
TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea1.setBounds(20, 110, 360, 260);
add(textarea1);
label2 = new Label();
label2.setBounds(20, 370, 200, 20);
label2.setText("Tulis Pesan Anda Disini:");
add(label2);
textarea2 = new TextArea("", 7, 45,
TextArea.SCROLLBARS_VERTICAL_ONLY);
textarea2.setBounds(20, 390, 360, 260);
add(textarea2);
label3 = new Label("Pesbuk 2");
label3.setFont(new Font("Times New Roman", Font.BOLD, 36));
label3.setBounds(100, 35, 200, 30);
add(label3);
setSize(400, 700);
setTitle("Pesbuk 2");
setVisible(true);
textarea2.requestFocus();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(
WindowEvent e){
System.exit(0);
}
}
);
}
public void run()
{
try{
while ((character = in.read()) != -1) {
chars[0] = (char)character;
textarea1.append(new String(chars));
}
}
catch(Exception ex)
{textarea1.setText(ex.getMessage());}
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == button1){
try{
socket = new Socket(textfield1.getText(), 8765);
textfield1.setText("Connecting....");
in = socket.getInputStream();
out = socket.getOutputStream();
thread = new Thread(this);
thread.start();
}
catch (IOException ioe){
textarea1.setText("Intercom 1 must be running and\n"
+ "accessible before running Intercom 2.");
textfield1.setText("Not connected");
}
catch (Exception e){
textarea1.setText(e.getMessage());
}
if(socket != null && socket.isConnected()){
textfield1.setText("Connected");
}
}
if(event.getSource() == button2){
try{
String str = textarea2.getText() + "\n";
byte buffer[] = str.getBytes();
out.write(buffer);
textarea2.setText("");
textarea2.requestFocus();
}
catch(Exception ex)
{textarea1.setText(ex.getMessage());}
}
}
}
TAMPILAN PROGRAM