Link to home
Create AccountLog in
Avatar of UltraDog
UltraDogFlag for Israel

asked on

Protecting my TCP or UDP conenction.

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 :)
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of UltraDog

ASKER

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?
Just make your header longer, e.g. as in the example above. It would be *very* coincidential that four bytes match by chance.
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.
Are you trying to protect against malice, or against an accident ?
against accidents :)
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.