Sie sind auf Seite 1von 2

import java.awt.event.

*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class ClientFrame extends Frame implements Runnable


{
Button send,end;
TextArea ta;
TextField tf;
Panel p1,p2;
Thread t;
byte buffer1[]=new byte[1024];
byte buffer2[]=new byte[1024];
DatagramSocket ds;
int clientport=789,serverport=790;
InetAddress ia;
public ClientFrame()
{
try{
ds=new DatagramSocket(clientport);
ia=InetAddress.getLocalHost();
}catch(Exception e){System.out.println(e.getMessage());}
t=new Thread(this);
t.start();
send=new Button("Send");
end=new Button("End");
ta=new TextArea(5,40);
tf=new TextField();
p1=new Panel();
p2=new Panel();
setLayout(new BorderLayout());
p1.setLayout(new GridLayout(1,2));
p2.setLayout(new GridLayout(2,1));
add(ta,BorderLayout.CENTER);
p1.add(send);
p1.add(end);
p2.add(tf);
p2.add(p1);
add(p2,BorderLayout.SOUTH);
send.addActionListener(new MyClass());
end.addActionListener(new MyClass());
addWindowListener(new WindowClass());
}
public void run()
{
try
{
while(true)
{
DatagramPacket p=new DatagramPacket(buffer2,buffer2.length);
ds.receive(p);
String str=new String(p.getData(),0,p.getLength());
if(str.equals("end")||str.equals("End")||str.equals("END"))
ta.append("Good Bye............");
else
ta.append(str+"\n");
}
}catch(Exception e){System.out.println(1);}
}
public class MyClass implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==end)
System.exit(0);
else if(ae.getSource()==send)
{
try
{
String str=tf.getText();
buffer1=str.getBytes();
ds.send(new
DatagramPacket(buffer1,str.length(),ia,serverport));
tf.setText("");
if(str.equals("end")||str.equals("End")||str.equals("END"))
System.exit(0);
}catch(Exception e){System.out.println(e.getMessage());}
}
}
}
class WindowClass extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public static void main(String agrs[])
{
ClientFrame cf=new ClientFrame();
cf.setSize(200,250);
cf.setVisible(true);
}
}