Hi!
I have a client and a server which communicate through TCP and UDP.
I'd like to prevent receiving messages from other processes that may try to connect to my ports.
I take the stream of bytes and make an object out of it, but if this is just a random stream - the program will crash.
How can I do it?
thanks :)
I already have some header bytes, but what if by some coincidence the other process has the same header?
suppose I put in my header the letter 'A' and the length of the message and so does the other process?
jkr
Just make your header longer, e.g. as in the example above. It would be *very* coincidential that four bytes match by chance.
mrjoltcola
In any case, you need to make your protocol implementation robust enough to handle bad packets.
At minimum use a giant try/catch block to discard message and close the connection. It shouldnt be possible to crash your app with a bad packet.
I'd rather combine the two ideas - that is, if the header does not match, you can skip calculating a checksum, but if it does, a matching checksum will make absolutely sure that the packet originated from a trustworthy source.
suppose I put in my header the letter 'A' and the length of the message and so does the other process?