基于udp的point- to-point的聊天程序

基于udp的point- to-point的聊天程序

code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import java.net.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
public class upd extends JFrame implements Runnable {
    
public upd(){
        thread
= new Thread(this);
        thread.start();     
        
        JFrame window 
= new JFrame("IM");
        Toolkit thekit 
= window.getToolkit();
        Dimension wndsize 
= thekit.getScreenSize();
        
this.setBounds(wndsize.width/4,wndsize.height/4,489,520);
         
this.setLayout(null);
         
try {
           jbInit();
       } 
catch (Exception ex) {
           ex.printStackTrace();
       }
       
this.addWindowListener(new WindowAdapter()
       {
           
public void WindowClosing(WindowEvent e)
           {
               System.exit(
0);
           }
       }
       );
       
this.setVisible(true);
       tr.run();
    }
    
public void run(){
        
//接受数据包
  
//用datagramsocket去接受一个包,而这个包是由datagrampacket的一个构造函数

     DatagramPacket datagrampacket 
= null;
     DatagramSocket datagramsocket 
= null;

     
byte data[] = new byte[1024];
     
try
     {
         datagrampacket 
= new DatagramPacket(data,data.length);
         datagramsocket 
= new DatagramSocket(9999);

     }
     
catch(Exception e) {out_img.setText(e.toString());}
     
while(true)
     {
         
if(datagramsocket ==null)
         {
             
break;
         }
         
else
             
try
             {
                 datagramsocket.receive(datagrampacket);
                 
int length = datagrampacket.getLength();
                InetAddress inetaddress 
= datagrampacket.getAddress();
                
int port = datagrampacket.getPort();
                String message 
= new String(datagrampacket.getData(),0,length);
                in_img.append(inetaddress
+"对你说:"+message+"\r");
             }
         
catch(Exception e){}
     }
 }
    
public static void main(String[] args) {
      upd Un 
= new  upd();
    }
               
//定义jframe中的控件 和 控件的位置
    private void jbInit() throws Exception {
        jButton1.setBounds(
new Rectangle(2824428123));
        jButton1.setText(
"关闭");
        jButton1.addActionListener(
new upd_jButton1_actionAdapter(this));
        jLabel1.setText(
"  IP ");
        jLabel1.setBounds(
new Rectangle(1172604215));
        in_img.setEnabled(
false);
        in_img.setBorder(BorderFactory.createLineBorder(Color.black));
        in_img.setSelectionColor(Color.black);
        in_img.setLineWrap(
true);
        in_img.setForeground(Color.black);
        out_img.setForeground(Color.black);
        out_img.setBorder(BorderFactory.createLineBorder(Color.black));
        out_img.setSelectionColor(Color.black);
        out_img.setBounds(
new Rectangle(15286436108));
        out_img.addKeyListener(
new upd_out_img_keyAdapter(this));
        out_img.setLineWrap(
true);
        
this.getContentPane().setBackground(Color.lightGray);
        
this.setJMenuBar(null);
        
this.setVisible(true);
        send.setBounds(
new Rectangle(3884428123));
        send.setText(
"发送");
        send.addActionListener(
new upd_send_actionAdapter(this));
        jScrollPane1.setBounds(
new Rectangle(1613452245));
        jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.getViewport().add(in_img, 
null);
        jScrollPane2.setBounds(
new Rectangle(14286454126));
        jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane2.getViewport().add(out_img,
null);
        
this.getContentPane().add(out_img);
        
this.getContentPane().add(IP);
        
this.getContentPane().add(jLabel1);
        
this.getContentPane().add(send);
        
this.getContentPane().add(jButton1);
        
this.getContentPane().add(jScrollPane1);
        jScrollPane1.getViewport().add(in_img);
        
this.getContentPane().add(jScrollPane2);
        IP.setText(
"127.0.0.1");
        IP.setBounds(
new Rectangle(19126022722));
        
        
        panel.setBounds(
0415489,22);    
        tr.setBounds(
0,0489,22);
        panel.setBackground(Color.lightGray);         
        panel.add(tr);
        
this.getContentPane().add(panel);
       
        
        
        
    }
    JPanel panel 
= new JPanel();
    JButton jButton1 
= new JButton();
    JTextField IP 
= new JTextField();
    JLabel jLabel1 
= new JLabel();
    JTextArea in_img 
= new JTextArea();
    JTextArea out_img 
= new JTextArea();
    JButton send 
= new JButton();
    Thread thread ;
    TitledBorder titledBorder1 
= new TitledBorder("");
    TitledBorder titledBorder2 
= new TitledBorder("");
    JScrollPane jScrollPane1 
= new JScrollPane();
    JScrollPane jScrollPane2 
= new JScrollPane();
    thread_run tr 
= new thread_run();
    
public void jButton1_actionPerformed(ActionEvent e) {//关闭按钮 退出程序

        System.exit(
0);
    }
    
//发送信息
    
//用datagramsocket去发送一个包,而这个包是由datagrampacket的一个构造函数
    public void send_actionPerformed(ActionEvent e) {
        
byte buffer[] = out_img.getText().getBytes();
        
try
        {
            InetAddress inetaddress 
= InetAddress.getByName(IP.getText());
            DatagramPacket datagrampacket 
= new DatagramPacket(buffer,buffer.length,inetaddress,9999);
            DatagramSocket datagramsocket 
= new DatagramSocket();
            in_img.append(
"你对"+IP.getText()+"说:"+out_img.getText()+"\n");
            out_img.setText(
"");
            datagramsocket.send(datagrampacket);
        }
        
catch(Exception ex){}
    }

    
public void out_img_keyReleased(KeyEvent e) {
        
if(e.getKeyCode() == KeyEvent.VK_ENTER)
        {
        
byte buffer[] = out_img.getText().getBytes();
      
try
      {
          InetAddress inetaddress 
= InetAddress.getByName(IP.getText());
          DatagramPacket datagrampacket 
= new DatagramPacket(buffer,buffer.length,inetaddress,9999);
          DatagramSocket datagramsocket 
= new DatagramSocket();
          in_img.append(
"你对"+IP.getText()+"说:"+out_img.getText()+"\r");
          out_img.setText(
"");
          datagramsocket.send(datagrampacket);
      }
      
catch(Exception ex){}
        }
    }
}
class upd_out_img_keyAdapter extends KeyAdapter {
    
private upd adaptee;
    upd_out_img_keyAdapter(upd adaptee) {
        
this.adaptee = adaptee;
    }

    
public void keyReleased(KeyEvent e) {
        adaptee.out_img_keyReleased(e);
    }
}
class upd_send_actionAdapter implements ActionListener {
    
private upd adaptee;
    upd_send_actionAdapter(upd adaptee) {
        
this.adaptee = adaptee;
    }

    
public void actionPerformed(ActionEvent e) {
        adaptee.send_actionPerformed(e);
    }
}
class upd_jButton1_actionAdapter implements ActionListener {
    
private upd adaptee;
    upd_jButton1_actionAdapter(upd adaptee) {
        
this.adaptee = adaptee;
    }

    
public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}
class thread_run extends Canvas implements Runnable
{
    thread_run(){}
    
static int x = 0;
     
public void paint(Graphics g)
        {
            
super.paint(g);
                    
            g.setColor(Color.red);
            g.drawString(
"欢迎使用JWROOM——WSY Instant Messaging System ", x,10);
            
        }
    
public void run()
    {
        
        
while(true)
        {
            
if(x<181)
            {
                
try{Thread.sleep(10);}catch(Exception e){}
                x 
+= 1;
                repaint();
                
            }
            
else
            {
                
while(true)
                {
                
try{Thread.sleep(10);}catch(Exception e){}
                x 
-= 1;
                repaint();
                
if(x==0)break;    
                }                
            }
        }
    }
}



import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import java.net.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.border.TitledBorder;
public class Untitled1 extends JFrame implements Runnable {
    
public Untitled1(){
        thread
= new Thread(this);
        thread.start();
        JFrame window 
= new JFrame("IM");
        Toolkit thekit 
= window.getToolkit();
        Dimension wndsize 
= thekit.getScreenSize();
        
this.setBounds(wndsize.width/4,wndsize.height/4,489,490);
         
this.setLayout(null);
         
try {
           jbInit();
       } 
catch (Exception ex) {
           ex.printStackTrace();
       }
       
this.setVisible(true);
    }
    
public void run(){
        
//接受数据包
  
//用datagramsocket去接受一个包,而这个包是由datagrampacket的一个构造函数


     DatagramPacket datagrampacket 
= null;
     DatagramSocket datagramsocket 
= null;

     
byte data[] = new byte[1024];
     
try
     {
         datagrampacket 
= new DatagramPacket(data,data.length);
         datagramsocket 
= new DatagramSocket(9999);

     }
     
catch(Exception e) {out_img.setText(e.toString());}
     
while(true)
     {
         
if(datagramsocket ==null)
         {
             
break;
         }
         
else
             
try
             {
                 datagramsocket.receive(datagrampacket);
                 
int length = datagrampacket.getLength();
                InetAddress inetaddress 
= datagrampacket.getAddress();
                
int port = datagrampacket.getPort();
                String message 
= new String(datagrampacket.getData(),0,length);
                in_img.append(inetaddress
+"对你说:"+message+"\r");
             }
         
catch(Exception e){}
     }
 }


    
public static void main(String[] args) {
      Untitled1 Un 
= new  Untitled1();
    }
               
//定义jframe中的控件 和 控件的位置
    private void jbInit() throws Exception {
        jButton1.setBounds(
new Rectangle(2824228123));
        jButton1.setText(
"关闭");
        jButton1.addActionListener(
new Untitled1_jButton1_actionAdapter(this));
        jLabel1.setText(
"  IP ");
        jLabel1.setBounds(
new Rectangle(1172604215));
        in_img.setEnabled(
false);
        in_img.setBorder(BorderFactory.createLineBorder(Color.black));
        in_img.setSelectionColor(Color.black);
        in_img.setLineWrap(
true);
        in_img.setForeground(Color.black);
        out_img.setForeground(Color.black);
        out_img.setBorder(BorderFactory.createLineBorder(Color.black));
        out_img.setSelectionColor(Color.black);
        out_img.setBounds(
new Rectangle(15286436108));
        out_img.addKeyListener(
new Untitled1_out_img_keyAdapter(this));
        out_img.setLineWrap(
true);
        
this.getContentPane().setBackground(Color.lightGray);
        
this.setJMenuBar(null);
        
this.setVisible(true);
        send.setBounds(
new Rectangle(3884228123));
        send.setText(
"发送");
        send.addActionListener(
new Untitled1_send_actionAdapter(this));
        jScrollPane1.setBounds(
new Rectangle(1613452245));
        jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.getViewport().add(in_img, 
null);
        jScrollPane2.setBounds(
new Rectangle(14286454126));
        jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane2.getViewport().add(out_img,
null);
        
this.getContentPane().add(out_img);
        
this.getContentPane().add(IP);
        
this.getContentPane().add(jLabel1);
        
this.getContentPane().add(send);
        
this.getContentPane().add(jButton1);
        
this.getContentPane().add(jScrollPane1);
        jScrollPane1.getViewport().add(in_img);
        
this.getContentPane().add(jScrollPane2);
        IP.setText(
"127.0.0.1");
        IP.setBounds(
new Rectangle(19126022720));
    }

    JButton jButton1 
= new JButton();
    JTextField IP 
= new JTextField();
    JLabel jLabel1 
= new JLabel();
    JTextArea in_img 
= new JTextArea();
    JTextArea out_img 
= new JTextArea();
    JButton send 
= new JButton();
    Thread thread ;
    TitledBorder titledBorder1 
= new TitledBorder("");
    TitledBorder titledBorder2 
= new TitledBorder("");
    JScrollPane jScrollPane1 
= new JScrollPane();
    JScrollPane jScrollPane2 
= new JScrollPane();
    
public void jButton1_actionPerformed(ActionEvent e) {//关闭按钮 退出程序

        System.exit(
0);
    }
    
//发送信息
    
//用datagramsocket去发送一个包,而这个包是由datagrampacket的一个构造函数
    public void send_actionPerformed(ActionEvent e) {
        
byte buffer[] = out_img.getText().getBytes();
        
try
        {
            InetAddress inetaddress 
= InetAddress.getByName(IP.getText());
            DatagramPacket datagrampacket 
= new DatagramPacket(buffer,buffer.length,inetaddress,9999);
            DatagramSocket datagramsocket 
= new DatagramSocket();
            in_img.append(
"你对"+IP.getText()+"说:"+out_img.getText()+"\n");
            out_img.setText(
"");
            datagramsocket.send(datagrampacket);
        }
        
catch(Exception ex){}
    }

    
public void out_img_keyReleased(KeyEvent e) {
        
if(e.getKeyCode() == KeyEvent.VK_ENTER)
        {
        
byte buffer[] = out_img.getText().getBytes();
      
try
      {
          InetAddress inetaddress 
= InetAddress.getByName(IP.getText());
          DatagramPacket datagrampacket 
= new DatagramPacket(buffer,buffer.length,inetaddress,9999);
          DatagramSocket datagramsocket 
= new DatagramSocket();
          in_img.append(
"你对"+IP.getText()+"说:"+out_img.getText()+"\r");
          out_img.setText(
"");
          datagramsocket.send(datagrampacket);
      }
      
catch(Exception ex){}
        }
    }
}
class Untitled1_out_img_keyAdapter extends KeyAdapter {
    
private Untitled1 adaptee;
    Untitled1_out_img_keyAdapter(Untitled1 adaptee) {
        
this.adaptee = adaptee;
    }

    
public void keyReleased(KeyEvent e) {
        adaptee.out_img_keyReleased(e);
    }
}
class Untitled1_send_actionAdapter implements ActionListener {
    
private Untitled1 adaptee;
    Untitled1_send_actionAdapter(Untitled1 adaptee) {
        
this.adaptee = adaptee;
    }

    
public void actionPerformed(ActionEvent e) {
        adaptee.send_actionPerformed(e);
    }
}
class Untitled1_jButton1_actionAdapter implements ActionListener {
    
private Untitled1 adaptee;
    Untitled1_jButton1_actionAdapter(Untitled1 adaptee) {
        
this.adaptee = adaptee;
    }

    
public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}


  1 import java.awt.*;
  2 import javax.swing.*;
  3 import java.awt.BorderLayout;
  4 import java.awt.Rectangle;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import javax.swing.Box;
  8 import java.net.*;
  9 import java.awt.event.KeyEvent;
 10 import java.awt.event.KeyAdapter;
 11 import javax.swing.BorderFactory;
 12 import java.awt.Color;
 13 import javax.swing.border.TitledBorder;
 14 public class Untitled1 extends JFrame implements Runnable {
 15     public Untitled1(){
 16         thread= new Thread(this);
 17         thread.start();
 18         JFrame window = new JFrame("IM");
 19         Toolkit thekit = window.getToolkit();
 20         Dimension wndsize = thekit.getScreenSize();
 21         this.setBounds(wndsize.width/4,wndsize.height/4,489,490);
 22          this.setLayout(null);
 23          try {
 24            jbInit();
 25        } catch (Exception ex) {
 26            ex.printStackTrace();
 27        }
 28        this.setVisible(true);
 29     }
 30     public void run(){
 31         //接受数据包
 32   //用datagramsocket去接受一个包,而这个包是由datagrampacket的一个构造函数
 33 
 34 
 35      DatagramPacket datagrampacket = null;
 36      DatagramSocket datagramsocket = null;
 37 
 38      byte data[] = new byte[1024];
 39      try
 40      {
 41          datagrampacket = new DatagramPacket(data,data.length);
 42          datagramsocket = new DatagramSocket(9999);
 43 
 44      }
 45      catch(Exception e) {out_img.setText(e.toString());}
 46      while(true)
 47      {
 48          if(datagramsocket ==null)
 49          {
 50              break;
 51          }
 52          else
 53              try
 54              {
 55                  datagramsocket.receive(datagrampacket);
 56                  int length = datagrampacket.getLength();
 57                 InetAddress inetaddress = datagrampacket.getAddress();
 58                 int port = datagrampacket.getPort();
 59                 String message = new String(datagrampacket.getData(),0,length);
 60                 in_img.append(inetaddress+"对你说:"+message+"\r");
 61              }
 62          catch(Exception e){}
 63      }
 64  }
 65 
 66 
 67     public static void main(String[] args) {
 68       Untitled1 Un = new  Untitled1();
 69     }
 70                //定义jframe中的控件 和 控件的位置
 71     private void jbInit() throws Exception {
 72         jButton1.setBounds(new Rectangle(2824228123));
 73         jButton1.setText("关闭");
 74         jButton1.addActionListener(new Untitled1_jButton1_actionAdapter(this));
 75         jLabel1.setText("  IP ");
 76         jLabel1.setBounds(new Rectangle(1172604215));
 77         in_img.setEnabled(false);
 78         in_img.setBorder(BorderFactory.createLineBorder(Color.black));
 79         in_img.setSelectionColor(Color.black);
 80         in_img.setLineWrap(true);
 81         in_img.setForeground(Color.black);
 82         out_img.setForeground(Color.black);
 83         out_img.setBorder(BorderFactory.createLineBorder(Color.black));
 84         out_img.setSelectionColor(Color.black);
 85         out_img.setBounds(new Rectangle(15286436108));
 86         out_img.addKeyListener(new Untitled1_out_img_keyAdapter(this));
 87         out_img.setLineWrap(true);
 88         this.getContentPane().setBackground(Color.lightGray);
 89         this.setJMenuBar(null);
 90         this.setVisible(true);
 91         send.setBounds(new Rectangle(3884228123));
 92         send.setText("发送");
 93         send.addActionListener(new Untitled1_send_actionAdapter(this));
 94         jScrollPane1.setBounds(new Rectangle(1613452245));
 95         jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
 96         jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
 97         jScrollPane1.getViewport().add(in_img, null);
 98         jScrollPane2.setBounds(new Rectangle(14286454126));
 99         jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
100         jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
101         jScrollPane2.getViewport().add(out_img,null);
102         this.getContentPane().add(out_img);
103         this.getContentPane().add(IP);
104         this.getContentPane().add(jLabel1);
105         this.getContentPane().add(send);
106         this.getContentPane().add(jButton1);
107         this.getContentPane().add(jScrollPane1);
108         jScrollPane1.getViewport().add(in_img);
109         this.getContentPane().add(jScrollPane2);
110         IP.setText("127.0.0.1");
111         IP.setBounds(new Rectangle(19126022720));
112     }
113 
114     JButton jButton1 = new JButton();
115     JTextField IP = new JTextField();
116     JLabel jLabel1 = new JLabel();
117     JTextArea in_img = new JTextArea();
118     JTextArea out_img = new JTextArea();
119     JButton send = new JButton();
120     Thread thread ;
121     TitledBorder titledBorder1 = new TitledBorder("");
122     TitledBorder titledBorder2 = new TitledBorder("");
123     JScrollPane jScrollPane1 = new JScrollPane();
124     JScrollPane jScrollPane2 = new JScrollPane();
125     public void jButton1_actionPerformed(ActionEvent e) {//关闭按钮 退出程序
126 
127         System.exit(0);
128     }
129     //发送信息
130     //用datagramsocket去发送一个包,而这个包是由datagrampacket的一个构造函数
131     public void send_actionPerformed(ActionEvent e) {
132         byte buffer[] = out_img.getText().getBytes();
133         try
134         {
135             InetAddress inetaddress = InetAddress.getByName(IP.getText());
136             DatagramPacket datagrampacket = new DatagramPacket(buffer,buffer.length,inetaddress,9999);
137             DatagramSocket datagramsocket = new DatagramSocket();
138             in_img.append("你对"+IP.getText()+"说:"+out_img.getText()+"\n");
139             out_img.setText("");
140             datagramsocket.send(datagrampacket);
141         }
142         catch(Exception ex){}
143     }
144 
145     public void out_img_keyReleased(KeyEvent e) {
146         if(e.getKeyCode() == KeyEvent.VK_ENTER)
147         {
148         byte buffer[] = out_img.getText().getBytes();
149       try
150       {
151           InetAddress inetaddress = InetAddress.getByName(IP.getText());
152           DatagramPacket datagrampacket = new DatagramPacket(buffer,buffer.length,inetaddress,9999);
153           DatagramSocket datagramsocket = new DatagramSocket();
154           in_img.append("你对"+IP.getText()+"说:"+out_img.getText()+"\r");
155           out_img.setText("");
156           datagramsocket.send(datagrampacket);
157       }
158       catch(Exception ex){}
159         }
160     }
161 }
162 class Untitled1_out_img_keyAdapter extends KeyAdapter {
163     private Untitled1 adaptee;
164     Untitled1_out_img_keyAdapter(Untitled1 adaptee) {
165         this.adaptee = adaptee;
166     }
167 
168     public void keyReleased(KeyEvent e) {
169         adaptee.out_img_keyReleased(e);
170     }
171 }
172 class Untitled1_send_actionAdapter implements ActionListener {
173     private Untitled1 adaptee;
174     Untitled1_send_actionAdapter(Untitled1 adaptee) {
175         this.adaptee = adaptee;
176     }
177 
178     public void actionPerformed(ActionEvent e) {
179         adaptee.send_actionPerformed(e);
180     }
181 }
182 class Untitled1_jButton1_actionAdapter implements ActionListener {
183     private Untitled1 adaptee;
184     Untitled1_jButton1_actionAdapter(Untitled1 adaptee) {
185         this.adaptee = adaptee;
186     }
187 
188     public void actionPerformed(ActionEvent e) {
189         adaptee.jButton1_actionPerformed(e);
190     }
191 }
192 
193 

你可能感兴趣的:(基于udp的point- to-point的聊天程序)