找不到服务器线程方法getInstance()

时间:2016-05-30 04:33:26

标签: java multithreading client-server

我试图使用以下代码运行服务器。但是当我尝试创建一个线程时,我找不到method getInstance()错误。请帮忙。

import java.net.*;
import java.io.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Server {
   public static void main(String args[]) {       
     Thread discoveryThread = new Thread(DiscoveryThread.getInstance());    
     discoveryThread.start();
   }   
}

class DiscoveryThread implements Runnable {
  DatagramSocket socket;    

  @Override
  public void run() {
    try {
      //Keep a socket open to listen to all the UDP trafic that is destined for this port
      socket = new DatagramSocket(8888, InetAddress.getByName("0.0.0.0"));
      socket.setBroadcast(true);

      while (true) {
        System.out.println(getClass().getName() + ">>>Ready to receive broadcast packets!");

        //Receive a packet
        byte[] recvBuf = new byte[15000];
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        socket.receive(packet);

        //Packet received
        System.out.println(getClass().getName() + ">>>Discovery packet received from: " + packet.getAddress().getHostAddress());
        System.out.println(getClass().getName() + ">>>Packet received; data: " + new String(packet.getData()));

        //See if the packet holds the right command (message)
        String message = new String(packet.getData()).trim();
        if (message.equals("DISCOVER_FUIFSERVER_REQUEST")) {
          byte[] sendData = "DISCOVER_FUIFSERVER_RESPONSE".getBytes();

          //Send a response
          DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
          socket.send(sendPacket);

          System.out.println(getClass().getName() + ">>>Sent packet to: " + sendPacket.getAddress().getHostAddress());
        }
      }
    } catch (IOException ex) {
      Logger.getLogger(DiscoveryThread.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  }

错误:

Server.java:10: error: cannot find symbol
Thread discoveryThread = new Thread(DiscoveryThread.getInstance());
                                                        ^
  symbol:   method getInstance()
  location: class DiscoveryThread

1 个答案:

答案 0 :(得分:1)

DiscoveryThread类似乎没有getInstance()方法。尝试使用new构建它。