TcpClient Send & Recieve issue


Gonna try and break this down....

I have made a DLL for comms between server/client app i've made. It works fine except a minor issue.

I'm using TcpClient & TcpListener to do the talking.

I have a standard format for all of the messages i send back and forth.

I have message types. Just some random ENUM i put together. For exmaple i have: MessageType.Message which
represents a standard text message.

So say i want to send "Hello" and then the MessageType. So end up with a string "Hello8" (lets just say MessageType.Message has a int value of 8

It would be converted to a byte array using Encoding.Ascii.GetBytes("Hello8")

Result array would look like this:

[0]H = 72
[1]e = 101
[2]l = 108
[3]l = 108
[4]o = 111
[5]8 = 56

That would be sent and recieved just fine. But if i sent it twice (lets just say for arguemnts sake, i like to say hello twice.)
If i dont leave a time delay, enough time for the other end to recieve from the buffer, it gets added on and then the other end.
Then the message recieved would be "Hello8Hello8"
Which is annoying. Now i could add a a byte to the array the start so it contains the length of bytes to read. For a single
message or add a termination by on the end of each message which is fine. I i just don't know which is best and if there is a
solution for this already available?

I hope i have made sense.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ron MalmsteadInformation Services ManagerCommented:
You need a delimiter for message and message type, or put your message type in the first blocks as a static length and have a delimter for EOM (end of message); or make your message lenth static.

You need to give yourself a way to programatically determine, without a doubt, the message part, the end of message, and the type.

Example... which is how XML and HTML sort of works.

Message type / message / EOM

You could also just process strings one at a time, and send one at a time, rather than sending multiple commands/strings concatenated on the same "send".
FalcubarAuthor Commented:
OK makes send.

I wouldn't be sending the strings "Hello8Hello8" it would be


but yes, i get what your saying.

I think i will just stick with my idea of counting the number of bytes in the string and setting the first byte as the string length then read from [1] element in the array to the number contained in the [0] element (string length). I just wondered if there was a way to do this so i didn''t... reinvent the wheel if you like.

i'm still open to suggestion.

Thanks Sage
Ron MalmsteadInformation Services ManagerCommented:
You can do seperate sends for this, or create a way to "parse" your multiple commands in single packets.

When creating server/client tcp apps, you are basically making your own "language" that they will use to communicate.  As long as you create "rules" and follow them, then you shouldn't have any unexpected messages / bugs in the app.

So if your rule is to use the first byte for length or type, or whatever... that's fine as long as your rules are always followed and cannot deviate.  This way on the "server" end, there is no confusion in processing your message parts from type parts, start/end...etc.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FalcubarAuthor Commented:
"When creating server/client tcp apps, you are basically making your own "language" that they will use to communicate."

That is the answer i really wanted.

Thank you :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.