Solved

Question about CAsyncSocket!

Posted on 2001-06-21
9
556 Views
Last Modified: 2013-11-20
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
Comment
Question by:hoandrew
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6217453
try using TCP_NODELAY with SetSockOpt that will disable the NAGLE algorithm
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6217469
BOOL bDisable=TRUE;
setsockopt(mysocket, IPPROTO_TCP, TCP_NODELAY, &bDisable, 4)
   
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6217471
you might also think about breaking your message up with headers to determine length etc
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 32

Expert Comment

by:jhance
ID: 6217802
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
 
LVL 32

Expert Comment

by:jhance
ID: 6217803
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
 
LVL 10

Expert Comment

by:makerp
ID: 6217886
read one byte at a time at the other end, then youll get "1" then "2".
0
 
LVL 10

Expert Comment

by:makerp
ID: 6217888
read one byte at a time at the other end, then youll get "1" then "2".
0
 
LVL 8

Accepted Solution

by:
VinExpert earned 100 total points
ID: 6222511
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490037
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 96
iSeries FTP Exit Program 8 147
sum28 challenge 31 115
either24  challenge 19 101
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question