debian怎么配置静态ip地址
305
2022-09-04
JAVA-UDP
public static void main(String[] args) throws IOException { System.out.println("UDPSearcher Started."); //作为搜索方,让系统自动分配端口 DatagramSocket ds=new DatagramSocket(); //构建一份请求数据 String requestData="HelloWord"; byte[] requestDataBytes=requestData.getBytes(); //直接构建packet DatagramPacket requestPacket=new DatagramPacket(requestDataBytes, requestDataBytes.length); //本机2000端口 requestPacket.setAddress(InetAddress.getLocalHost()); requestPacket.setPort(2000); //发送 ds.send(requestPacket); //构建接收实体 final byte[] buf=new byte[512]; DatagramPacket receivePack=new DatagramPacket(buf,buf.length); //接收 ds.receive(receivePack); //打印接收到的信息与发送者的信息 //发送者的ip地址 String ip=receivePack.getAddress().getHostAddress(); int port=receivePack.getPort(); int dataLen=receivePack.getLength(); String data=new String(receivePack.getData(),0,dataLen); System.out.println("UDPSearcher receive form ip:"+ip +"\tport:"+port+"\tdata:"+ data); //完成 System.out.println("UDPSearcher Finished."); ds.close(); }
public static void main(String[] args) throws IOException { System.out.println("UDPProvider Started."); //作为接受者,指定一个端口用于数据接收 DatagramSocket ds=new DatagramSocket(20000); //构建接收实体 final byte[] buf=new byte[512]; DatagramPacket receivePack=new DatagramPacket(buf,buf.length); //接收 ds.receive(receivePack); //打印接收到的信息与发送者的信息 //发送者的ip地址 String ip=receivePack.getAddress().getHostAddress(); int port=receivePack.getPort(); int dataLen=receivePack.getLength(); String data=new String(receivePack.getData(),0,dataLen); System.out.println("UDPProvider receive form ip:"+ip +"\tport:"+port+"\tdata:"+ data); //构建一份回送数据 String responseData="Receive data with len:"+dataLen; byte[] responseDataBytes=responseData.getBytes(); //直接根据回送者构建一份回送信息 DatagramPacket responsePacket=new DatagramPacket(responseDataBytes, responseDataBytes.length, receivePack.getAddress(), receivePack.getPort()); ds.send(responsePacket); //完成 System.out.println("UDPProvider Finished."); ds.close();}
广播局域网搜索功能:
public class MessageCreator { private static final String SN_HEADER = "收到暗号,我是(SN):"; private static final String PORT_HEADER = "这是暗号,请回电端口(Port):"; public static String buildWithPort(int port) { return PORT_HEADER + port; } public static int parsePort(String data) { if (data.startsWith(PORT_HEADER)) { return Integer.parseInt(data.substring(PORT_HEADER.length())); } return -1; } public static String buildWithSn(String sn) { return SN_HEADER + sn; } public static String parseSn(String data) { if (data.startsWith(SN_HEADER)) { return data.substring(SN_HEADER.length()); } return null; }}
import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.util.UUID;public class UDPProvider { public static void main(String[] args) throws IOException { // 生成一份唯一标示 String sn = UUID.randomUUID().toString(); Provider provider = new Provider(sn); provider.start(); // 读取任意键盘信息后可以退出 //noinspection ResultOfMethodCallIgnored System.in.read(); provider.exit(); } private static class Provider extends Thread { private final String sn; private boolean done = false; private DatagramSocket ds = null; public Provider(String sn) { super(); this.sn = sn; } @Override public void run() { super.run(); System.out.println("UDPProvider Started."); try { // 监听20000 端口 ds = new DatagramSocket(20000); while (!done) { // 构建接收实体 final byte[] buf = new byte[512]; DatagramPacket receivePack = new DatagramPacket(buf, buf.length); // 接收 ds.receive(receivePack); // 打印接收到的信息与发送者的信息 // 发送者的IP地址 String ip = receivePack.getAddress().getHostAddress(); int port = receivePack.getPort(); int dataLen = receivePack.getLength(); String data = new String(receivePack.getData(), 0, dataLen); System.out.println("UDPProvider receive form ip:" + ip + "\tport:" + port + "\tdata:" + data); // 解析端口号 int responsePort = MessageCreator.parsePort(data); if (responsePort != -1) { // 构建一份回送数据 String responseData = MessageCreator.buildWithSn(sn); byte[] responseDataBytes = responseData.getBytes(); // 直接根据发送者构建一份回送信息 DatagramPacket responsePacket = new DatagramPacket(responseDataBytes, responseDataBytes.length, receivePack.getAddress(), responsePort); ds.send(responsePacket); } } } catch (Exception ignored) { } finally { close(); } // 完成 System.out.println("UDPProvider Finished."); } private void close() { if (ds != null) { ds.close(); ds = null; } } /** * 提供结束 */ void exit() { done = true; close(); } }}
import java.io.IOException;import java.net.*;import java.util.ArrayList;import java.util.List;import java.util.concurrent.CountDownLatch;//搜索者,用于搜搜服务支持方public class UDPSearcher { private static final int LISTEN_PORT = 30000; public static void main(String[] args) throws IOException ,InterruptedException{ System.out.println("UDPSearcher Started."); Listener listener = listen(); sendBroadcast(); // 读取任意键盘信息后可以退出 //noinspection ResultOfMethodCallIgnored System.in.read(); List
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~