------
android培训、
java培训、期待与您交流! ----------
------- android培训、 java培训、期待与您交流! ----------
import java.awt.Frame;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.regex.*;
import java.util.*;
import java.util.Comparator;
import java.awt.event.*;
import java.util.EventListener;
import javax.swing.*;
import java.awt.event.WindowListener;
public class Test extends Frame implements ActionListener,Runnable, WindowListener{
Button b=new Button("OK");
TextField aim=new TextField(11);
TextField text=new TextField(30);
java.awt.List list=new java.awt.List();
DatagramSocket ds=null;
public Test(){
try{
ds=new DatagramSocket(2345);
}catch(Exception e){
e.printStackTrace();
}
Panel p=new Panel();
aim.setText("127.0.0.1");
p.add(aim);
p.add(text);
p.add(b);
this.add("North",p);
this.add(list);
this.setTitle("俺是客户端");
this.setSize(500,280);
this.setVisible(true);
b.addActionListener(this);
this.addWindowListener(this);
new Thread(this).start();
}
public static void main(String[] agr)throws Exception{
new Test();
}
public void run(){
try{
//接收
DatagramSocket ds=new DatagramSocket(1234);
byte buffer[]=new byte[256];
DatagramPacket dp=new DatagramPacket(buffer,256);
while(true){
ds.receive(dp);
list.add(dp.getAddress().getHostAddress()+"说:"+new String(buffer).trim());
}
}catch(Exception e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
//发送
try{
byte[] msg=text.getText().getBytes();
int len=text.getText().length();
InetAddress ia=InetAddress.getByName(aim.getText());
int port=1234;
DatagramPacket dp=new DatagramPacket(msg,len,ia,port);
ds.send(dp);
}catch(Exception ee){
ee.printStackTrace();
}
}
/**
* Method windowOpened
*
*
* @param e
*
*/
public void windowOpened(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowClosing
*
*
* @param e
*
*/
public void windowClosing(WindowEvent e) {
// TODO: Add your code here
System.exit(0);
}
/**
* Method windowClosed
*
*
* @param e
*
*/
public void windowClosed(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowIconified
*
*
* @param e
*
*/
public void windowIconified(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowDeiconified
*
*
* @param e
*
*/
public void windowDeiconified(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowActivated
*
*
* @param e
*
*/
public void windowActivated(WindowEvent e) {
// TODO: Add your code here
}
/**
* Method windowDeactivated
*
*
* @param e
*
*/
public void windowDeactivated(WindowEvent e) {
// TODO: Add your code here
}
}
------- android培训、 java培训、期待与您交流! ----------