0

客户端(僵尸网络服务器)试图通过 TCP 套接字向服务器(破坏者)发送连续消息,但破坏者只收到一条消息。Disruptor 是由僵尸网络服务器创建的线程。

代码:僵尸网络服务器

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Jedis jedis = new Jedis("localhost");
    String pattern = new String("TKproject");
    input = new Disruptor(30001,1024,jedis,pattern);
    int count = 0;
    Thread start = new Thread(input);
    start.start();
    try {
        request = new Socket("localhost",30001);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Random rand = new Random();
    Message msg = new Message();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(request.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    while(true){
        System.out.println("count is : " + count);
    count++;
    if(count == 5)
        break;
    if(count % 15 == 0)
        jedis.rpush(pattern,Integer.toString(count));
    int next = rand.nextInt(3);
    msg.setMessageId(count);
    switch (next){
    case 0: msg.setType(MessageType.HELLO);
            break;
    case 1: msg.setType(MessageType.REQUEST);
            break;
    case 2: msg.setType(MessageType.REPLY);
            break;  
    default: msg.setType(MessageType.REQUEST);
             break;
    }
    //System.out.println("Message id "+msg.Messageid);
    try {
        oos.writeObject(msg);
        //oos.flush();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
}

破坏者运行()

   public void run() {
    // TODO Auto-generated method stub
    while(true){
        System.out.println("Disruptor Running");
        Socket receipt = null;
        try {
             receipt = server.accept();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ObjectInputStream recv = null;
        try {
            recv = new ObjectInputStream(receipt.getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
        byte [] rcvbytes = new byte[2048];
        try {
            recv.read(rcvbytes);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        try {
            storage.write((Message)recv.readObject());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
4

2 回答 2

0
 receipt = server.accept();

您只需执行一次即可将服务器连接到客户端,尝试将该指令移到while(true).

声明也需要这样做ObjectInputStream

于 2015-02-15T20:04:53.383 回答
0

任何一个:

  • Message每次发送一个新对象而不是刷新旧对象
  • 使用 `writeUnshared()' 发送它,或者
  • ObjectOutputStream.reset()在发送刷新的Message对象之前调用。
于 2015-02-15T21:41:40.773 回答