Emm ... Sockets are a very slow thing, as practice shows. There is a possibility that the socket still accepts data (it has not yet received everything), and you are already pulling the buffer. Of course, the buffer will return bytes, but not in the amount you expect. Another variant of this problem is that the program rarely jerks the socket buffer, and the user gets glued packets.
There are several options:
- Structure the packet in such a way that the first 2 or 4 bytes indicate the length of the entire packet. In the loop, wait for 2/4 bytes to appear in the buffer. Read them. In the loop, wait for the N bytes read from the previous item to appear in the buffer, then pull the desired N number of bytes. Next to them will again be the dimension of the next package.
- If there are packets of only one format, and always of fixed length, you can simply wait for the required number of bytes in the buffer, and then read the required number of bytes (not more). The option is not the best, is lazy.
- You can make a large buffer in advance, where the FIFO bytes from the socket buffer are flown, and you need to pull the required number of bytes from this stack. The option is not the best - cycling, but met several times.
PS: To make sure of the received, after
IpMask = Task.split("-");
call:
System.out.println(Task + ", splits for " + IpMask.Length + "parts");
and see what happens.
exception
or how to organize a competent approach to reading the elements of an array? - voidexception
forIpMask[1]
? CansetText
try to write the wrong data somewhere? - voidIpMask[1]
element? - void