请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

provider回的信息 searcher收不到

package udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class UDPSearcher {

private static final int LISTENPORT = 30000;
public static void main(String args[]) throws Exception {
    Listener listener = listen();
    sendBroadcast();
    System.in.read();
    List<Device> devices = listener.getDeviceAndClose();
    for(Device device:devices ){
        System.out.println(device.toString());
    }
}

private static Listener listen() throws InterruptedException {
    System.out.println("UDPSearcher start!");
    CountDownLatch countDownLatch = new CountDownLatch(1);

    Listener listener = new Listener(LISTENPORT,countDownLatch);
    listener.start();
    countDownLatch.await();
    return listener;
}


private static class Device{
    final int port;
    final String ip;
    final String sn;

    private Device(int port, String ip, String sn) {
        this.port = port;
        this.ip = ip;
        this.sn = sn;
    }

    @Override
    public String toString() {
        return "Device{" +
                "port=" + port +
                ", ip='" + ip + '\'' +
                ", sn='" + sn + '\'' +
                '}';
    }
}


private static class Listener extends Thread {
    private final int listenPort;
    private final CountDownLatch countDownLatch;
    private final List<Device> devices = new ArrayList<>();
    private boolean done = false;
    private DatagramSocket ds  = null;

    public Listener(int listenPort, CountDownLatch countDownLatch) {
        this.listenPort = listenPort;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        super.run();
        countDownLatch.countDown();
        try{
            ds = new DatagramSocket(listenPort);

            while (!done){
                byte buf[] = new byte[512];
                final DatagramPacket receivePacket = new DatagramPacket(buf,buf.length);
                //接收
                ds.receive(receivePacket);
                byte data[]= receivePacket.getData();
                String receivesStr = new String(data,data.length);
                String IP = receivePacket.getAddress().getHostAddress();
                int PORT = receivePacket.getPort();
                System.out.println("searcherSocket 接收到 ip: "+IP+"\tport:"+PORT+"\tLength"+data.length+"\tDATA"+receivesStr);
                String SN = MessageCreator.parseSN(receivesStr);
                if(SN!=null){
                    Device d  = new Device(PORT,IP,SN);
                    devices.add(d);
                }
            }
            System.out.println("UDPSearcher finish!");

        }catch (Exception e){

        }finally {
            close();
        }
    }
    private void close(){
        done = true;
        if(ds!=null){
            ds.close();
            ds = null;
        }
    }
    private List<Device> getDeviceAndClose(){
        close();
        return devices;
    }
}

public static void sendBroadcast() throws Exception {
    System.out.println("UDPSearcher sendBroadcast started.");
    DatagramSocket searcherSocket = new DatagramSocket();
    String sendStr = MessageCreator.buildWithPort(LISTENPORT);

    final DatagramPacket searcherPacket = new DatagramPacket(sendStr.getBytes(),sendStr.getBytes().length);
    searcherPacket.setAddress(InetAddress.getByName("255.255.255.255"));
    searcherPacket.setPort(20000);
    searcherSocket.send(searcherPacket);
    searcherSocket.close();
    System.out.println("UDPSearcher sendBroadcast finish.");

}

}

正在回答 回答被采纳积分+3

2回答

Qiujuer 2021-01-21 11:41:40

单看UDPSearcher来说应该没有问题,需要看一下你的

provider

部分的源码,另外你当前的日志输出发我一下看看。截图就行。

0 回复 有任何疑惑可以回复我~
  • 提问者 慕粉1890165 #1
    UDPProvider started.
    UDPProvider 接收到 ip: 192.168.1.116	port:62243	Length46	DATA这是暗号,请回电端口(Port):30000
    以上是Provider的日志。
    以下是searcher的日志。
    UDPSearcher start!
    UDPSearcher sendBroadcast started.
    UDPSearcher sendBroadcast finish.
    回复 有任何疑惑可以回复我~ 2021-01-23 10:16:38
  • 提问者 慕粉1890165 #2
    public static void main(String args[]) throws IOException {
    
            String sn = UUID.randomUUID().toString();
            Provider provider = new Provider(sn);
            provider.run();
    
            //读取任意字符退出
            System.in.read();
            provider.exit();
        }
    回复 有任何疑惑可以回复我~ 2021-01-23 10:18:18
  • 提问者 慕粉1890165 #3
    不让发过长的代码,我重新提问吧
    回复 有任何疑惑可以回复我~ 2021-01-23 10:19:25
Qiujuer 2021-01-21 00:27:00

收到,我调试一下看看,明天给你答复哈。

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信