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

📄 j_send.java

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

/**
 * 在这里给出对类 J_Send 的描述。
 * 
 * 陈华葵(20042104038)
 * 2006年12月9日
 */
import java.awt.event.*;//ActionListener
import java.awt.*;
import java.net.*;
import javax.swing.*;//JFrame
import java.io.*;//IOException
public class J_Send
{
	public static void main(String[] args)
	{
	    J_SendFrame frame=new J_SendFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.show();
	   }
}

class J_SendFrame extends JFrame implements Runnable,ActionListener
{
   TextField out_message=new TextField("发送数据:");
   TextArea in_message=new TextArea();
   Button button=new Button("发送");
   DatagramSocket dSocket;
   DatagramPacket inPacket;//接收包
   DatagramPacket outPacket;//发送包
   InetAddress sAddr;//地址
   String message1;//包中的数据
   String message2;
   //byte[] inBuffer=new byte[100];
   byte[] outBuffer;
   J_SendFrame()
   {
       super("发送操作");
       setVisible(true);
       setSize(300,300);
       Container content=getContentPane();
       content.add(button,"North");
       content.add(out_message,"South");
       content.add(in_message,"Center");
       button.addActionListener(this);
    }//end of J_SendFrame
    public void actionPerformed(ActionEvent event)
    {
        outBuffer=out_message.getText().trim().getBytes();//把发送数据保存到数据包
        try
        {           
            dSocket =new DatagramSocket();
            sAddr=InetAddress.getByName("localhost");//获得地址
            outPacket=new DatagramPacket(outBuffer,outBuffer.length,sAddr,9999);//新建数据包
            dSocket.send(outPacket);//发送数据包     
            message1=new String(outPacket.getData(),0,outPacket.getLength());//获得包中的数据
            in_message.append(message1+"\n");//把发送的数据显示出来            
            in_message.append("数据包目标地址:"+outPacket.getAddress()+"at port:"+outPacket.getPort()+"\n");//
            Thread thread=new Thread(this);
            thread.start();//线程接收数据
        }//end of try
        catch(IOException e)
        {
            System.out.println("IOExcepton 3");
        }//end of catch
    }//end of actionPerform
    public void run()
    {byte[] inBuffer=new byte[100];
        try
        {    
           dSocket=new DatagramSocket(9999);
            while(true)
            {
            inPacket=new DatagramPacket(inBuffer,inBuffer.length);//新建接收的数据包
            dSocket.receive(inPacket);//接收数据包
            message2=new String(inPacket.getData(),0,inPacket.getLength());//接收到的包中的数据
            in_message.append("接收到的包:"+message2);
            in_message.append("该包来自:"+inPacket.getAddress()+" at port "+inPacket.getPort()+"\n");
           }//end of while
        }//end of try
        catch(IOException e)
        {
            System.out.println("IOException 4");
        }//end of catch
        
    }//end of run
}//end of class
            
            
            

⌨️ 快捷键说明

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