自己写的山寨版飞秋

                                                      ------ 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培训、期待与您交流! ----------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值