Avatar of UltraDog
UltraDog
Flag 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 :)
C++

Avatar of undefined
Last Comment
jkr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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?
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Infinity08

Are you trying to protect against malice, or against an accident ?
UltraDog

ASKER
against accidents :)
SOLUTION
Infinity08

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jkr

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.