如何使我的Receiver线程通知并唤醒我的套接字类?

时间:2019-02-24 23:35:31

标签: java multithreading sockets

我有一个多播套接字,该套接字需要向服务器发送注册消息并等待确认消息。多播套接字(ChatClient)实例化一个Receiver类,该类不断侦听传入的udp数据包并进行处理。 (如果只是消息包,请在屏幕上打印;如果是“已注册确认”的包,请在屏幕上进行确认。)
在Register(String username)方法上,ChatClient对象等待3000毫秒内的消息。如果时间到了(或者如果它接收到“拒绝注册”数据包),则注册被取消,否则接收方将立即收到刚刚接收到“确认确认”数据包的通知。

问题是,同步块中的代码无法运行。 (下面是带有3个类的一些测试代码)。

我如何让接收者通知主线程并结束ChatClient的等待?

公共类ChatClient扩展了ChatHost {

InetAddress server_address;
int server_port;
int id;
boolean awaiting = false;

public ChatClient(InetAddress group_address, int group_port, InetAddress server_address, int server_port) throws IOException {
    super(group_port, group_address);
    this.server_address = server_address;
    this.server_port = server_port;

}

public static void main(String[] args) throws IOException {
    ChatClient test = new ChatClient(InetAddress.getByName("224.1.1.1"), 6667, InetAddress.getByName("localhost"), 6666);
    test.Register("test");
    new ChatClient(InetAddress.getByName("224.1.1.1"), 6667, InetAddress.getByName("localhost"),6666);

}

public void Register(String username) throws IOException {
    ChatPacket message = new ChatPacket(ChatPacket.Type.REGISTRATION_REQUEST, username);
    byte[] buffer = ChatPacket.serialize(message);
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, server_address, server_port);
    send(packet);
    awaiting = true;
    synchronized (this){
        try {
            wait(3000);
        } catch (InterruptedException e) {
            awaiting = false;
            System.out.println("Notify works");
        }
    awaiting = false;
    }
}

公共抽象类ChatHost扩展了MulticastSocket {

public InetAddress group_address;
protected Thread ReceiverThread;

public ChatHost(int port, InetAddress group) throws IOException {
    super(port);
    this.group_address = group;


    ReceiverThread =
            new Thread(new Receiver(this));
    ReceiverThread.start();
}

public void sendMessage(String message_content, int id, InetAddress receiver_address, int receiver_port) throws IOException {
    ChatPacket message = new ChatPacket(ChatPacket.Type.MESSAGE, id, message_content);
    byte[] buffer = ChatPacket.serialize(message);
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiver_address, receiver_port);
    send(packet);
    System.out.println(buffer.length);    //TEST
}

public abstract void messageReceived(DatagramPacket packet);

公共类Receiver实现Runnable

{
    ChatHost host = null;
    static public boolean ack = false;

    public Receiver(ChatHost host){
        this.host = host;
    }

    @Override
    public void run() {
        while (true) {
            byte[] buf = new byte[1000];
            DatagramPacket received_packet = new DatagramPacket(buf, buf.length);
            try {
                host.receive(received_packet);
            } catch (IOException e) {
                e.printStackTrace();
            }

            synchronized (host){
                if(awaiting){
                    host.notifyAll();
            }

0 个答案:

没有答案