日期:2014-05-17  浏览次数:20719 次

多线程 UDP 程序
在写一个多线程的UDP程序,想在服务器的一个端口上监听数据,每收到一个数据包的时候把数据提取出来丢给一个子线程去处理,处理完之后子线程返回一个返回数据给主程序,然后从该端口上把返回数据发回去,请问该怎么设计才能实现这个目的?怎么让主程序知道子线程已经处理完了呢?
  下面是我的主程序,每收到一个数据就交给ServerSubThread_UDP子线程去处理,现在就是不知道怎么把子线程处理完的数据交给主程序。
Java code

            InetAddress bind_addr = InetAddress.getByName(DNSServer_UDP.BIND_ADDRESS);
            serverSocket = new DatagramSocket(DNSServer_UDP.BIND_PORT, bind_addr);
            while (bListerning) {
                System.out.println("\nUDP Listening...\n");
                byte receiveData[] = new byte[DNSServer_UDP.PACKETBUFFSIZE];
                DatagramPacket receivePacket = new DatagramPacket(receiveData, DNSServer_UDP.PACKETBUFFSIZE);
                // Receive a packet
                serverSocket.receive(receivePacket);
                // Print client IP and port
                System.out.println("Accept one: IP = " + receivePacket.getAddress().getHostAddress() + ", Port = " + receivePacket.getPort());
                // Get the data
                ONSRecordAction recordAction = new ONSRecordAction();
                ByteArrayInputStream bin = new ByteArrayInputStream(receiveData);
                ObjectInputStream oin = new ObjectInputStream(bin);
                recordAction = (ONSRecordAction)oin.readObject();
                
                bin.close();
                oin.close();
                // Process the request in a new thread and keep listening in the port to get new packet
                new ServerSubThread_UDP(recordAction, receivePacket.getAddress().getHostAddress(), receivePacket.getPort()).start();
            }            }



------解决方案--------------------
加我q 710176178