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

📄 j_receive.java

📁 模拟客户和服务器的工作
💻 JAVA
字号:

/**
 * 在这里给出对类 J_Receive 的描述。
 * 
 * @作者(你的名字)
 * @版本(一个版本号或者一个日期)
 */
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class J_Receive
{
	public static void main(String[] args)
	{
	    J_ReceiveFrame frame=new J_ReceiveFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.show();
	 }
}

class J_ReceiveFrame extends JFrame implements Runnable,ActionListener
{
    Button button1=new Button("开始接受");
    Button button2=new Button("停止接收");
    TextArea in_message=new TextArea();
    DatagramSocket dsocket;
    DatagramPacket inPacket;//接收包
    DatagramPacket outPacket;//发送包
    InetAddress cAddr;//客户端的地址
    int cPort;//端口号
    byte[] inBuffer=new byte[100];//保存客户端发送过来的包
    byte[] outBuffer;//保存要发送的包
    String message;//数据包中的数据
    Thread thread;//一个进程
    boolean stop=false;
    J_ReceiveFrame()
    {
        super("接受数据操作");
        setSize(300,300);
        setVisible(true);
        Container content=getContentPane();
        content.setLayout(new FlowLayout(FlowLayout.CENTER));
        button1.addActionListener(this);
        button2.addActionListener(this);
        content.add(in_message,"Center");
        content.add(button1);
        content.add(button2);
        thread=new Thread(this);
    }//end of method J_Receive
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button1)//点击开始按钮
        {
            button1.setBackground(Color.blue);
            button2.setBackground(Color.gray);
            if(!(thread.isAlive()))//如果进程结束
            {
                thread=new Thread(this);//创建进程
            }//end of if
            try
            {
                thread.start();//进程开始
                stop=false;
            }//end of try
           catch(Exception a)
            {
                //System.out.println("IOException e");
            }//end of catch
        }//end of if
        if(e.getSource()==button2)//点击停止按钮
        {
            button1.setBackground(Color.gray);
            button2.setBackground(Color.blue);
            thread.interrupt();//进程被打断
            stop=true;
           // stop=true;           
        }//end of if 
     }//end of method actionPerform
      public void run()
        {
            try
            {
               dsocket=new DatagramSocket(9999);//建立连接
            }//end of try
            catch(IOException e)
            {
                System.out.println("IOException 1");
            }
            while(true)
           {
               if(dsocket==null)
                  break;
               else
               {
                   try
                   {

                  inPacket=new DatagramPacket(inBuffer,inBuffer.length);
                  dsocket.receive(inPacket);//接收数据包
                   message=new String(inPacket.getData(),0,inPacket.getLength());//包中的数据                
                  cAddr=inPacket.getAddress();//获得发送端的地址
                  cPort=inPacket.getPort();//获得端口号
                  in_message.append("收到数据来自:"+cAddr+"端口号:"+cPort+"\n");
                  in_message.append("收到的数据是:"+message+"\n");//在Text中显示数据
                  outBuffer=new String("收到数据!").getBytes();//回显已经收到数据包
                  outPacket=new DatagramPacket(outBuffer,outBuffer.length,cAddr,cPort);
                  dsocket.send(outPacket);//回显已经收到数据包
                 }//end of try
                catch(IOException ee)
                {
                  System.out.println("IOException 2");
                }//end of catch
                if(stop==true)
                    break;
             }//end of else 
           }//end of whlie
        }//end of run
}//end of class J_ReceiveFrame
                  
                
        
        
    
    

⌨️ 快捷键说明

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