Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Question about CAsyncSocket!

Hi All,

I am writing an client/server application , and i am trying to use CAsyncSocket to send message between them. However, I have encounter a problem is that: when i send message from either client or server to the other, for example:

send(socket, "1");
send(socket, "2");

and on the other side, it will receive "12" for once.

How can i avoid this situation???

Thanks in advance.

0
hoandrew
Asked:
hoandrew
  • 3
  • 2
  • 2
  • +2
1 Solution
 
ShaunWildeCommented:
try using TCP_NODELAY with SetSockOpt that will disable the NAGLE algorithm
0
 
ShaunWildeCommented:
BOOL bDisable=TRUE;
setsockopt(mysocket, IPPROTO_TCP, TCP_NODELAY, &bDisable, 4)
   
0
 
ShaunWildeCommented:
you might also think about breaking your message up with headers to determine length etc
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jhanceCommented:
Why shouldn't it receive "12"??  That's EXACTLY what you sent!!  Think of the socket connection as a stream.  Every call to send puts the data referenced by send into the stream in order and all of it will end up at the receiver.  Any formatting or interpretation of the stream is up to you.  If you want to keep the "1" and "2" separate, then it's up to you to put in some way of knowing that the first message is ended and the second one is starting.
0
 
jhanceCommented:
BTW, printf is the same say.  You'd never know where the lines broke if you didn't put a "\n" in the stream from time to time to format the text.
0
 
makerpCommented:
read one byte at a time at the other end, then youll get "1" then "2".
0
 
makerpCommented:
read one byte at a time at the other end, then youll get "1" then "2".
0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Socket is working perfectly alright in that case. U will get "12" only as u r sending both successively.
I suggest to define ur own protocol for communication in that case. Define a datatype of ur own, it may be just a structure. Then after reading the socket, go on mapping the character buffer to ur structure, in that way u can seperate. Say in Ur case
struct MyData
{
char chTemp;
};

While reading the data
1. Read to temporary character buffer
2. copy the character buffer contents to structure and repeat this till temporary buffer is finished.
Then u will get exactly what u have sent. Remember, while sending also u have to fill the same structure and send it, rather than sending the hard coded bytes!!!

Try it out.
VinExpert
0
 
Roshan DavisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by : VinExpert

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now