Java:服务器不能通过tcp套接字多次接收消息。

时间:2014-12-23 12:50:06

标签: java sockets tcp disruptor-pattern

客户端(僵尸网络服务器)正在尝试通过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();
    }
    }
}

Disruptor run()

   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();
        }
    }

2 个答案:

答案 0 :(得分:0)

 receipt = server.accept();

您需要执行此操作一次才能将服务器连接到客户端,尝试在while(true)之前移动该指令。

ObjectInputStream声明需要做同样的事情。

答案 1 :(得分:0)

或者:

  • 每次发送一个新的Message对象,而不是刷新旧的
  • 使用`writeUnshared()'或
  • 发送它
  • 在发送刷新的ObjectOutputStream.reset()对象之前调用Message