Falcubar
asked on
TcpClient Send & Recieve issue
Hello
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("H ello8")
Result array would look like this:
byte[]
[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.
Thanks
Steve
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("H
Result array would look like this:
byte[]
[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.
Thanks
Steve
ASKER
OK makes send.
I wouldn't be sending the strings "Hello8Hello8" it would be
Tcp.Send("Hello8")
Tcp.Send("Hello8");
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
I wouldn't be sending the strings "Hello8Hello8" it would be
Tcp.Send("Hello8")
Tcp.Send("Hello8");
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
"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 :)
That is the answer i really wanted.
Thank you :)
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
<H&08>Hello</H&08><H&08>He
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".