本帖最后由 郭阳 于 2012-10-6 16:12 编辑
- import java.net.*;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- class MyQQ
- {
- public static void main(String[] args) throws Exception
- {
- MyQQFrame myqq=new MyQQFrame();
- }
- }
- class MyQQFrame
- {
- MyQQFrame()
- {
- init();
- }
- private Frame frame;
- private TextArea ta_sender;
- private TextArea ta_receiver;
- private Button button;
- private DatagramSocket ds_sender;
- public void init()
- {
- frame=new Frame("MyQQ");
- frame.setVisible(true);
- frame.setBounds(30,30,500,400);
- frame.setLayout(new FlowLayout());
- ta_sender=new TextArea(3,35);
- ta_receiver=new TextArea(16,60);
- button=new Button("发送");
- frame.add(ta_receiver);
- frame.add(ta_sender);
- frame.add(button);
- try
- {
- DatagramSocket ds_sender=new DatagramSocket();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
-
- setEvent();
- startReceive();
-
- }
-
-
-
-
- public void startReceive()
- {
- new Thread(new Runnable(){
- public void run()
- {
- try
- {
- DatagramSocket ds=new DatagramSocket(6777);
- while(true)
- {
- byte[] buf=new byte[1024];
- DatagramPacket dp=new DatagramPacket(buf,buf.length);
- ds.receive(dp);
- String ip=dp.getAddress().getHostAddress();
- String data=new String(dp.getData(),0,dp.getLength());
- ta_receiver.append(ip+"\r\n\t"+data+"\r\n");
- }
- }
- catch (Exception e)
- {
- //System.out.println(e.toString());//把接受异常屏蔽,说明是发送出的问题
- }
- }
- }).start();
- }
- public void setEvent()
- {
-
-
-
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- try
- {
- byte[] buf=ta_sender.getText().getBytes();
- DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.3"),6777);
- ds_sender.send(dp);
-
- }
- catch (Exception ex)
- {
- System.out.println(ex.toString());
- }
- }
- });
-
-
- frame.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- });
- }
- }
复制代码 是发送出的毛病。实在找不到到底哪里写错了,谢谢
|