📄 udpsend.java
字号:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class UDPSend extends Frame implements ActionListener
{ GridLayout net;
TextField textField1=new TextField();
Button button1=new Button();
Button button2=new Button();
public static void main(String args[])
{
UDPSend send=new UDPSend();
send.setLocation(100,100);
send.setSize(300,150);
send.show();
}
public UDPSend()
{try{
this.setTitle("发送数据报");
button1.setLabel("发送");
button2.setLabel("退出");
net=new GridLayout(3,1);
setLayout(net);
this.add(textField1,null);
this.add(button1,null);
this.add(button2,null);
textField1.addActionListener(this);
button2.addActionListener(this);button1.addActionListener(this); }
catch(Exception e){e.printStackTrace();
}
}
void sendData()
{
try
{
String msg=textField1.getText();
if(msg.equals(""))
return;
textField1.setText("");
InetAddress address=InetAddress.getByName("localhost");
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);}
catch(Exception e){}
}
public void actionPerformed(ActionEvent e)
{Component com=(Component)e.getSource();
if(com.equals(button1))
sendData();
else if(com.equals(button2))
System.exit(0);
else
sendData();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -