FORM : 1 NewClass.java
//Server Application
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class NewClass extends Frame implements ActionListener,Runnable
{
ServerSocket s;
Socket s1;
BufferedReader br;
BufferedWriter bw;
TextField text;
Button button1,button2;
List list;
public void run()
{
try{s1.setSoTimeout(1);}catch(Exception e){}
while (true)
{
try{
list.addItem(br.readLine());
}catch (Exception h){}
}
}
public static void main(String arg[])
{
new NewClass("Server Applicaton:");
}
public NewClass(String m)
{
super(m);
setSize(200,300);
setLocation(0,0);
this.setLayout(new BorderLayout());
button1 = new Button("Send");
button2 = new Button("Exit");
button1.addActionListener(this);
button2.addActionListener(this);
list = new List();
text = new TextField();
add(list,"Center");
add(button1,"West");
add(button2,"East");
add(text,"South");
setVisible(true);
try{
s = new ServerSocket(100);
s1=s.accept();
br = new BufferedReader(new InputStreamReader(
s1.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(
s1.getOutputStream()));
bw.write("Hello");bw.newLine();bw.flush();
Thread th;
th = new Thread(this);
th.start();
}catch(Exception e){}
}
public void actionPerformed ( ActionEvent e)
{
if (e.getSource().equals(button2))
System.exit(0);
else{try{
bw.write(text.getText());
bw.newLine();bw.flush();
text.setText("");
}catch(Exception m){}
}
}
}
FORM : NewClass1.java
//Client Application
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class NewClass1 extends Frame implements ActionListener,Runnable
{
Socket s;
BufferedReader br;
BufferedWriter bw;
TextField text;
Button button1,button2;
List list;
public static void main(String arg[])
{
new NewClass1("Client Applicaton:");
}
public void run()
{
try{s.setSoTimeout(1);}catch(Exception e){}
while (true)
{
try{list.addItem(br.readLine());
}catch (Exception h){}
}
}
public NewClass1(String m)
{
super(m);
setSize(200,300);
setLocation(300,0);
this.setLayout(new BorderLayout());
button1 = new Button("Send");
button2 = new Button("Exit");
button1.addActionListener(this);
button2.addActionListener(this);
list = new List();
text = new TextField();
add(list,"Center");
add(button1,"West");
add(button2,"East");
add(text,"South");
setVisible(true);
try{
/*Put the current IP address for current machine
if you didn't have an actual server and clients
if you have an actual server and clients put the client IP address*/
s = new Socket("127.0.0.1",100);
br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
Thread th;
th = new Thread(this);
th.start();
}catch(Exception e){}
}
public void actionPerformed ( ActionEvent e)
{
if (e.getSource().equals(button2))
System.exit(0);
else{try{
bw.write(text.getText());
bw.newLine();bw.flush();
}catch(Exception m){}
}
}
}
TAMPILAN
Download Project : klik disini

Tidak ada komentar:
Posting Komentar