Java: Server not receiveing messages over tcp socket more than once.

107 Views Asked by At

A Client(botnet server) is trying to send continuous messages over TCP socket to a server(disruptor), but only one message is received at the disruptor. Disruptor is a thread which is created by the botnet server.

code: Botnet server

    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

There are 2 best solutions below

0
On

Either:

  • send a new Message object each time instead of refreshing the old one
  • send it using `writeUnshared()’, or
  • call ObjectOutputStream.reset() before sending a refreshed Message object.
0
On
 receipt = server.accept();

You need to perform this just once to connect your server to a client, try and move that instruction before the while(true).

The same needs to be done for the ObjectInputStream declaration.