dwstate.java

来自「Java Switch On/Off source」· Java 代码 · 共 48 行

JAVA
48
字号
import java.net.*;

public class DWState {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String ip = "192.168.10.199";
		int port = 5000;
		
		InetAddress inetAddress = null;
		try {
			inetAddress = InetAddress.getByName(ip);
		} catch (Exception e) {
			// TODO: handle exception
			System.exit(1);
		}
		
		DatagramSocket dsock = null;
		try {
			dsock = new DatagramSocket();
			String line = null;
			
			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);
			
			
			byte[] buffer = new byte[200];
			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 + =
减小字号Ctrl + -
显示快捷键?