简体   繁体   中英

How to manage lots of incoming packets

I have a socketserver set up with a remote client, and it is functional. Upon opening the client and logging in, I noticed that sometimes , there is an error that seems to be due to the client reading an int when it shouldn't be.

Upon logging on, the server sends a series of messages/packets to the client, and these are anything from string messages to information used to load variables on the client's side.

Occasionally, while logging in, an error gets thrown showing that the client has read a packet of size 0 or a very large size. Upon converting the large-sized number into ascii I once found that it was a bit of a string "sk." (I located this string in my code so it's not entirely random).

Looking at my code, I'm not sure why this is happening. Is it possible that the client is reading an int at the wrong time? If so, how can I fix this?

        InetAddress address = InetAddress.getByName(host);
        connection = new Socket(address, port);
        in = new DataInputStream(connection.getInputStream());
        out = new DataOutputStream(connection.getOutputStream());
        String process;
        System.out.println("Connecting to server on "+ host + " port " + port +" at " + timestamp);
        process = "Connection: "+host + ","+port+","+timestamp + ". Version: "+version;
        write(0, process);
        out.flush();
        while (true) {
            int len = in.readInt();
            if (len < 2 || len > 2000) {
                throw new Exception("Invalid Packet, length: "+len+".");
            }
            byte[] data = new byte[len];
            in.readFully(data);
            for (Byte b : data) {
                System.out.printf("0x%02X ",b);
            }
            try {
                reader.handlePackets(data);
            } catch (Exception e) {
                e.printStackTrace();
                //connection.close();
                //System.exit(0);
                //System.out.println("Exiting");
            }
        }


//Here is code for my write function (Server sided):
public static void write(Client c, Packet pkt) {
    for (Client client : clients) {
        if (c.equals(client)) {
            try {
                out.writeInt(pkt.size());
                out.write(pkt.getBytes());
                out.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

So looking at the write function, I don't really see how it could be confusing the client and making it read for the size of the packet twice for one packet (at least that's what I think is happening).

If you need more information please ask me.

The client side code looks fine, and the server side code looks fine too.

The most likely issue is that this is some kind of issue with multi-threading and (improper) synchronization. For example, maybe two server-side threads are trying to write a packet to the same client at the same time.

It is also possible that your Packet class has inconsistent implementations of size() and getBytes() ... or that one thread is modifying a Packet objects while a second one is sending it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM