日期:2014-05-20  浏览次数:20800 次

本人写的仿QQ的一段代码就是联不了网!
以下是本人写的仿QQ的一段代码,可以运行成功但就是联不了网!这是为什么?
不好意思代码比较杂乱,又不喜欢注释,希望大哥大姐们耐心看看啊!
代码的大致意思是在登陆对话框输入对方IP地址和自己的姓名,就可以和对方进行聊天了!
当然对方也是同样登陆,填上我的IP地址.
Java code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
//import java.io.*;

public class LanSQQ 
{
    public static void main(String[] args)
    {
        
        //InetAddress server=InetAddress.getByName();
        QQDL qqdl=new QQDL();
        qqdl.setTitle("乞丐QQ");
        qqdl.setSize(200,150);
        qqdl.setLocation(600,400);
        qqdl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        qqdl.setVisible(true);
        //if(qqdl.txtLZ.getText().equals("y")) {
            //qqdl.setVisible(false);
            //QQPanel QQ = new QQPanel(/*qqdl.txtXM.getText(), qqdl.LabIp.getText()*/);
            //QQ.setTitle("乞丐QQ");
            //QQ.setSize(400, 500);
            //QQ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //QQ.setVisible(true);    
    }
}
class QQDL extends JFrame implements ActionListener 
{
    /**
     * 
     */
    private static final long serialVersionUID = 6903763380037544539L;
    private JLabel LabXX =new JLabel("请输入您的信息");
    private JLabel LabXM =new JLabel("姓  名");
    JTextField txtXM =new JTextField();
    JLabel LabIp =new JLabel("IP地址");
    private JTextField txtIp=new JTextField();
    private JButton bntDL =new JButton("登陆");
    public QQDL()
    {
        //ActionListener al=new LAFActionlistener();
        //bntDL.addActionListener(al);
        bntDL.addActionListener(this);
        JPanel pa=new JPanel();
        pa.add(LabXX);
        getContentPane().add(pa,BorderLayout.NORTH);
        JPanel pb=new JPanel();
        pb.setLayout(new GridLayout(2,2));
        pb.add(LabXM);
        pb.add(txtXM);
        pb.add(LabIp);
        pb.add(txtIp);
        getContentPane().add(pb,BorderLayout.CENTER);
        JPanel pc=new JPanel();
        pc.add(new JLabel());
        pc.add(bntDL);
        getContentPane().add(pc,BorderLayout.SOUTH);
    }             
    public void actionPerformed(ActionEvent e)
        {
           Object shi=e.getSource();
        QQPanel QQ=new QQPanel(txtXM.getText(),txtIp.getText());
        QQ.setTitle("乞丐QQ");
        QQ.setSize(400,500);
        QQ.setLocation(550,200);
        QQ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        QQ.setVisible(true);
        if(shi==bntDL){this.setVisible(false);}
        }
}
class QQSender extends Thread
{
     private InetAddress server;
     private int port;
     private DatagramSocket dsocket;
     private JTextArea txtFS;
     public QQSender(InetAddress server,int port,DatagramSocket dsocket,JTextArea txtFS)
     {
         this.server=server;
         this.port=port;
         this.dsocket=dsocket;
         this.txtFS=txtFS;
     }
     public void run()
     {
        try 
        {
            while (true) 
            {
                byte[] data = (txtFS.getText()).getBytes();
                DatagramPacket outgoing = new DatagramPacket(data, data.length,
                        server, port);
                dsocket.send(outgoing);
            }
        } catch (Exception e) 
        {
        }     
     }
}

class QQReceiver extends Thread
{
     //private InetAddress server;
     //private int port;
     private DatagramSocket dsocket;
     private JTextArea txtLiao;
     public QQReceiver(InetAddress server,int port,DatagramSocket dsocket,JTextArea txtLiao)
     {
         //this.server=server;
         //this.port=port;
         this.dsocket=dsocket;
         this.txtLiao=txtLiao;
         setName("LanSQQ-Receiver");
     }
     public void run()
     {
         byte[] buffer=new byte[10000];
         try
         {
             while(true)
             {
                 DatagramPacket incom=new DatagramPacket(buffer,buffer.length);
                 dsocket.receive(incom);
                 String data=new String(incom.getData(),0,incom.getLength());
                 txtLiao.append(data);
             }
         }catch(Exception e)
         {     
         }finally
         {
             
         } 
     }
}
class QQPanel extends JFrame
{
    /**
     * 
     */
    private static final long serialVersionUID = -7871881624202450654L;
    private DatagramSocket dsocket;
    private JLabel LiaoLabel =new    JLabel("聊天记录");
    private JTextArea txtLiao =new JTextArea();
    private JTextArea txtFS =new JTextArea();
    private JButton bntFS =new    JButton("发送");
    private String yh;
    private String ip;
    public QQPanel(String yh,String ip)
    {        
         this.yh=yh;
         this.ip=ip;
         
         ActionListener al=new LAFActionlistener();
         bntFS.addActionListener(al);
         
         JPanel p1=new JPanel();
            p1.setLayout(new GridLayout(1,3));
         p1.add(LiaoLabel);
         p1.add(new JLabel());
         p1.add(new JLabel());
         getContentPane().add(p1,BorderLayout.NORTH);
         
         JPanel p2=new JPanel();
         txtLiao.setBackground(Color.LIGHT_GRAY);
         txtLiao.setLineWrap(true);
         txtLiao.setEditable(false);
         txtFS.setLineWrap(true);
         txtFS.setBackground(Color.LIGHT_GRAY);
         JScrollPane s1=new JScrollPane(txtLiao);
         JScrollPane s2=new JScrollPane(txtFS);
         //s1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
         //s2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
         p2.setLayout(new GridLayout(2,1,5,5));
         p2.add(s1);
         p2.add(s2);
         getContentPane().add(p2,BorderLayout.CENTER);
         
         JPanel p3=new JPanel();
         p3.setLayout(new GridLayout(1,3));
         p3.add(new JLabel());
         p3.add(new JLabel());;
         p3.add(bntFS);
         getContentPane().add(p3,BorderLayout.SOUTH);
        try {
            dsocket=new DatagramSocket(8000); 
            new QQSender(InetAddress.getByName(this.ip), 8080, dsocket, txtFS).start();
            System.out.println("ok!");
        } catch (Exception e) {
            
        }         
         
     }
     class LAFActionlistener implements ActionListener
     {             
         public void actionPerformed(ActionEvent e)
         {
             String ss=txtFS.getText();
             String sss=yh+":"+"\n"+ss+"\n";
             txtFS.setText("");
             //txtLiao.append(yh);
             txtLiao.append(sss);
            try {
                new QQSender(InetAddress.getByName(ip), 8080, dsocket, txtFS).start();
                System.out.println("yes!");
            } catch (Exception a) {
                // TODO: handle exception
            }             
         }
     }
}