📄 udpon.java
字号:
import java.net.*;
public class UDPOn {
public static void main(String[] args) {
String ip = "192.168.10.199";
int port = 5000;
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getByName(ip);
} catch (Exception e) {
System.exit(1);
}
DatagramSocket dsock = null;
try {
dsock = new DatagramSocket();
char STX = 0x02;
char ETX = 0x03;
String MSG = "on";
String SNDMSG = STX + MSG + ETX;
DatagramPacket sendPacket = new DatagramPacket(SNDMSG.getBytes(), SNDMSG.getBytes().length, inetAddress, port);
dsock.send(sendPacket);
System.out.println("SNDMSG: " + SNDMSG);
/**
byte[] buffer = new byte[100];
DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
dsock.receive(receivePacket);
String msg = new String(receivePacket.getData(), 0, receivePacket.getData().length);
System.out.println("辑滚肺 傈崔罐篮 蔼 : " + msg.trim());
**/
} catch (Exception e) {
System.out.println(e);
} finally {
if (dsock != null)
dsock.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -