udpsend.java
来自「java网络高级编程的配套源码,java网络高级编程为清华出版社出版.」· Java 代码 · 共 83 行
JAVA
83 行
/*源代码清单8-5*/
import java.net.*;
import java.awt.*;
import borland.jbcl.layout.*;
import java.awt.event.*;
public class UDPSend extends Frame implements ActionListener
{
VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
TextField textField1 = new TextField();
Button button1 = new Button();
Button button2 = new Button();
public UDPSend()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
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();
}
void sendData()
{
try
{
String msg=textField1.getText();
if(msg.equals(""))
return;
textField1.setText("");
InetAddress address=InetAddress.getByName("10.15.43.216");
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 static void main(String[] args)
{
UDPSend send=new UDPSend();
send.setLocation(100,100);
send.setSize(200,120);
send.show();
}
private void jbInit() throws Exception
{
this.setTitle("发送数据报");
button1.setLabel("发送");
button2.setLabel("退出");
this.setLayout(verticalFlowLayout1);
this.add(textField1, null);
this.add(button1, null);
this.add(button2, null);
button1.addActionListener(this);
button2.addActionListener(this);
textField1.addActionListener(this);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?