⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientapp_udp.java

📁 这个是一个由客户端和服务端程序的UDP通信的Java应用程序
💻 JAVA
字号:
// Clientapp_UDP.java

import  java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
//import  java.util.*;
public class Clientapp_UDP extends WindowAdapter implements ActionListener,KeyListener
{
   TextField str_send;
   Label Label;
   TextArea msg;
   Button send,exit;
   Panel pl;
   String  hostname;
   DatagramSocket receiveSocket,sendSocket;
   DatagramPacket receivePacket,sendPacket;

   public void display()
   {
     Frame f= new Frame("在线聊天---客户端");
     f.setSize(400,350);
     f.setLocation(400,350);
     f.setBackground(Color.red);
     pl=new Panel();
     f.add(pl,"South");
     msg= new TextArea();
     msg.setSize(100,250);
     msg.setBackground(Color.white);
     msg.setEditable(false);
     f.add(msg);
     Label= new Label ("发送消息");
     pl.add(Label);
     str_send = new TextField(20);
     pl.add(str_send);
     str_send.addKeyListener(this);
     send = new Button("发送");
     pl.add(send);
     str_send.addActionListener(this);
     exit = new Button("退出");
     pl.add(exit);
     exit.addActionListener(this);
     f.addWindowListener(this);
     f.setVisible(true);

     try
     {
       //sendSocket = new DatagramSocket(3000);
       sendSocket = new DatagramSocket();
     }
     catch(IOException e)
     {
       msg.append(e + "\n");
     }
  }

   public void receiveMessage()
   {
   try{
         receiveSocket = new DatagramSocket(3001);
         while(true)
          {
           byte  []buf = new byte[500];
            receivePacket = new DatagramPacket(buf, buf.length);
           //receivePacket=new DatagramPacket(buf,500);
           receiveSocket.receive(receivePacket);
           ByteArrayInputStream bin = new ByteArrayInputStream(receivePacket.getData());
           BufferedReader reader = new BufferedReader (new InputStreamReader(bin));
           msg.append("Server:"+reader.readLine());
           msg.append("\n");
          }
      }
      catch(Exception e)
       {
         msg.append(e+"\n");
       }
    }



      public void sendMessage()
       {
         try
         {
         	  //sendSocket = new DatagramSocket();  
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(bout);
            pout.print(str_send.getText());
            byte buf[]= bout .toByteArray();
             sendPacket = new DatagramPacket(buf,buf.length,InetAddress.getByName("localhost"),3333);
            //sendPacket = new DatagramPacket(buf,buf.length,InetAddress.getByName(hostname),3333);
            sendSocket.send(sendPacket);
            msg.append("Client:" + str_send.getText() + "\n");
            str_send.setText(" ");

           }
           catch(IOException err)
           {
             msg.append(err+ "\n");
            }
        }

         public void actionPerformed(ActionEvent e )
         {
             if(e.getSource()==send)
               {
                 sendMessage();
               } 
             if(e.getSource()==exit)
               {
                   System.out.println("聊天程序已结束,再见!\n");
                   System.exit(0);
                }
          }


           public void windowClosing(WindowEvent e)
              {
                 System.out.println(" 聊天程序已结束,再见!\n");
                 System.exit(0);
              }



           public void keyPressed(KeyEvent e)
             {
                if(e.getSource() == str_send) 
                 {
                      if(e.getKeyChar()==KeyEvent.VK_ENTER)
                        {
                             sendMessage();
                        }
                  }
             }

             public static void main(String arg[])
               {
                   Clientapp_UDP client = new Clientapp_UDP();
                   client.display();
                   client.receiveMessage();
               }
             public void keyTyped(KeyEvent e){}
             public void keyReleased(KeyEvent e){}
}









 















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -