📄 udpget.java
字号:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class UDPGet extends Frame implements ActionListener
{
TextArea TextArea1=new TextArea();
Button button1=new Button();
public static void main(String[] args)
{
UDPGet get=new UDPGet();
get.setLocation(100,100);
get.setSize(300,200);
get.show();
get.waitForData();
}
public UDPGet()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
void waitForData()
{
try
{
byte[] buffer=new byte[1024];
DatagramPacket packet=new DatagramPacket(buffer,buffer.length);
DatagramSocket socket=new DatagramSocket(9999);
while(true)
{
socket.receive(packet);
String s=new String(buffer,0,0,packet.getLength());
TextArea1.append(s+"\n");
packet=new DatagramPacket(buffer,buffer.length);
}
}catch(Exception e)
{
}
}
private void jbInit() throws Exception
{
this.setTitle("接受数据报");
TextArea1.setText("");
button1.setLabel("退出 ");
this.add(TextArea1,BorderLayout.CENTER);
this.add(button1,BorderLayout.SOUTH);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Component com=(Component)e.getSource();
if(com.equals(button1))
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -