发送方创建步骤:
1. 创建一个DatagramSocket对象
DatagramSocket socket = new DatagramSocket (4567);
2. 创建一个 InetAddress , 相当于是地址
InetAddress serverAddress = InetAddress.getByName("想要发送到的那个IP地址");
3. 这是随意发送一个数据
String str = "hello";
4. 转为byte类型
byte data[] = str.getBytes();
5. 创建一个DatagramPacket 对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket package = new DatagramPacket (data , data.length , serverAddress , 4567);
6. 调用DatagramSocket对象的send方法 发送数据
socket . send(package);
udp的辅助工具类:
这在自己项目中主要用来发送频道号码,如果是1...9自然发送一次即可,如果是多余1位的,就依次发送。
package com.wotlab.home.moneyplantairs.utils; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; /** * 为红外适配器发送指令的助手类 * @author lx * */ public class UDPHelper { private static DatagramSocket s = null; private static InetAddress local; public static void sendSingle(int message, String ip) { int server_port = Constants.SOCKET_PORT; if (s == null) { try { s = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } try { // 换成服务器端IP,ip为想要发送到的那个ip地址 local = InetAddress.getByName(ip); } catch (UnknownHostException e) { e.printStackTrace(); } } String send = "### " + message + " ###"; DatagramPacket p = new DatagramPacket(send.getBytes(), send.length(), local, server_port); try { s.send(p); } catch (IOException e) { e.printStackTrace(); } } public static void sendChannel(int channelID, String ip) { char[] strChannel = (channelID + "").toCharArray(); for (int i = 0; i < strChannel.length; i++) { switch (Integer.parseInt(strChannel[i] + "")) { case 0: sendSingle(0x11, ip); break; case 1: sendSingle(0x28, ip); break; case 2: sendSingle(0x18, ip); break; case 3: sendSingle(0x08, ip); break; case 4: sendSingle(0x22, ip); break; case 5: sendSingle(0x12, ip); break; case 6: sendSingle(0x02, ip); break; case 7: sendSingle(0x29, ip); break; case 8: sendSingle(0x19, ip); break; case 9: sendSingle(0x09, ip); break; } if (i + 1 != strChannel.length) { try { Thread.sleep(1000); } catch (Exception e) { } } } } }
发送tcp的操作
package com.wotlab.home.moneyplantairs.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; /** * Socket的助手类,用于通过TCP协议发送数据 * * @author lx * */ public class SocketHelper { private Socket s = null; BufferedReader input = null; PrintWriter output = null; public SocketHelper() { } public void connect(String ip, int port) throws Exception { s = new Socket(); SocketAddress socAddress = new InetSocketAddress(ip, port); // SocketAddress socAddress = new InetSocketAddress("192.168.1.105", // port); s.connect(socAddress, Constants.SOCKET_TIMEOUT); s.setSoTimeout(Constants.SOCKET_LINK_TIMEOUT); } public void close() throws Exception { output.close(); input.close(); s.close(); } public String sendTCP(String str) { String receive = ""; try { if (s == null) { throw new Exception(""); } output = new PrintWriter(s.getOutputStream()); output.print(str); output.flush(); input = new BufferedReader( new InputStreamReader(s.getInputStream())); receive = input.readLine(); } catch (Exception e) { e.printStackTrace(); } return receive; } }