TCP, CAsyncSocket

I am using CAsyncSocket with SOCK_STREAM option (using 
TCP). I noticed that in most cases the messages I received 
from the other end were ok, but sometimes two messages 
were appended together, and sometimes a message was 
chopped into two separate messages. 

Is that a problem of using CAsyncSocket? Anyway I can fix 
it? Do I get the same problem if I don't use CAsyncSocket? 
Any suggestions?

Thanks!

0
anonymous (74718)
1/8/2004 1:44:46 AM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
984 Views

Similar Articles

[PageSpeed] 32

dev wrote:

> I am using CAsyncSocket with SOCK_STREAM option (using 
> TCP). I noticed that in most cases the messages I received 
> from the other end were ok, but sometimes two messages 
> were appended together, and sometimes a message was 
> chopped into two separate messages. 
> 
> Is that a problem of using CAsyncSocket? Anyway I can fix 
> it? Do I get the same problem if I don't use CAsyncSocket? 
> Any suggestions?
> 
> Thanks!
> 

It is not broken.  This is normal for all winsock streams, with or 
without CAsyncSocket.  The number of bytes returned from 
CAsyncSocket::Read can be absolutely any number up to the buffer size 
you specify.  You receive a stream of bytes with message boundaries 
(sometimes) ignored.  You must implement a buffer manager that can 
accept absolutely anything and put your messages back together.

-- 
Scott McPhillips [VC++ MVP]

0
scottmcp8856 (490)
1/8/2004 2:12:33 AM
Thanks Scott.

So, which means that there's no shortcut to workaround 
this problem? I have to reorganize the messages myself?

I am pretty new to networking programming. Besides winsock 
streams, are there any other options that can avoid this 
problem?

Thanks!


>-----Original Message-----
>dev wrote:
>
>> I am using CAsyncSocket with SOCK_STREAM option (using 
>> TCP). I noticed that in most cases the messages I 
received 
>> from the other end were ok, but sometimes two messages 
>> were appended together, and sometimes a message was 
>> chopped into two separate messages. 
>> 
>> Is that a problem of using CAsyncSocket? Anyway I can 
fix 
>> it? Do I get the same problem if I don't use 
CAsyncSocket? 
>> Any suggestions?
>> 
>> Thanks!
>> 
>
>It is not broken.  This is normal for all winsock 
streams, with or 
>without CAsyncSocket.  The number of bytes returned from 
>CAsyncSocket::Read can be absolutely any number up to the 
buffer size 
>you specify.  You receive a stream of bytes with message 
boundaries 
>(sometimes) ignored.  You must implement a buffer manager 
that can 
>accept absolutely anything and put your messages back 
together.
>
>-- 
>Scott McPhillips [VC++ MVP]
>
>.
>
0
anonymous (74718)
1/8/2004 2:32:18 AM
dev wrote:
> Thanks Scott.
> 
> So, which means that there's no shortcut to workaround 
> this problem? I have to reorganize the messages myself?
> 
> I am pretty new to networking programming. Besides winsock 
> streams, are there any other options that can avoid this 
> problem?
> 
> Thanks!

The only networking shortcut to this problem is to use UDP with short 
messages.  This is not a reliable method, and is suitable only when you 
would rather lose data than wait for it to be retransmitted.

There are several programming shortcuts: i.e. use someone else's code to 
do the message parsing.  One of them is MFC's 
CSocket/CSocketFile/CArchive, although I do not recommend it (it seems 
to be buggy).

But if you need to or wish to have full control of your message formats 
you must do the message reassembly yourself.

-- 
Scott McPhillips [VC++ MVP]

0
scottmcp8856 (490)
1/8/2004 4:55:39 AM
Sometimes you can get away with just 'peeking' at the data in your
overridden ::OnReceive from the socket, and if your marker for 'end of
transmission' is found, pull it out.  Otherwise, let the data stay in the
socket's buffer and keep arriving.

    WTH


0
spamsucks (94)
1/8/2004 5:51:05 AM
"dev" <anonymous@discussions.microsoft.com> wrote in message
news:072d01c3d588$ff1e9a10$a301280a@phx.gbl...
> I am using CAsyncSocket with SOCK_STREAM option (using
> TCP). I noticed that in most cases the messages I received
> from the other end were ok, but sometimes two messages
> were appended together, and sometimes a message was
> chopped into two separate messages.
>
> Is that a problem of using CAsyncSocket? Anyway I can fix
> it? Do I get the same problem if I don't use CAsyncSocket?
> Any suggestions?
>
> Thanks!
>
dev,

    That is the very nature of async sockets.  They grab as much data as
they can in a small amount of time so that they do not block (take forever).
If you used blocking sockets then you can get the functionality that you are
looking for.  However, with blocking sockets you need to be careful that you
do not block for too long (always use select before recv so you do not
block).  People typically put blocking sockets calls in their own thread so
they do not slow the GUI down.


0
trevor8021 (184)
1/8/2004 1:54:28 PM
Reply:

Similar Artilces:

Named Piped and TCP/IP Protocols
Why are the SQL Server Client Network Utility Named Piped and TCP/IP Protocols supposed to be enabled? I need to know specifically and not generally speaking. I cannot find any supporting KB articles providing this information only on how to setup a ODBC connection which we are already versed on. Thank you! Kerri Kerri, In the installation manual for GP (page 13 for GP 10.0) the recommendation is to limit your network to one protocol. Since TCP/IP is required for GP, that's typically all you need. -- Victoria Yudin Dynamics GP MVP Flexible Solutions - home of GP Reports http://...

TCP connections from a listening port
Hi, I would like to know the active TCP connections established from a listening port. How can I get that? I got the active TCP connectionss using IPGlobalProperties.GetActiveTcpConnections. I got the active TCP listeners using IPGlobalProperties.GetActiveTcpListeners But I cannot link the two items Thanks! ...

CAsyncSocket & notifications
I derived a class from CAsyncSocket called CAppSocket and instatiated an object within a user thread. A client using CSocket attempts to connect (and returns TRUE), but the CAppSocket object never seems to receive the FD_ACCEPT message. The IP addresses and port numbers seem to add up. Anybody have any ideas. Bryan wrote: > > I derived a class from CAsyncSocket called CAppSocket and > instatiated an object within a user thread. A client > using CSocket attempts to connect (and returns TRUE), but > the CAppSocket object never seems to receive the FD_ACCEPT > message...

How can TCP host get the IP of TCP client?
i make a TCP host program. It can work well. some of my program is as follows void CListen::OnAccept(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class CSevenSimuView* pLView; CListView4* pRViewK; CMainFrame* pMainFrame; SOCKADDR SockAddr; int lpSockAddrLen; pMainFrame = (CMainFrame*)AfxGetMainWnd(); CClient *pSocket=new CClient(); pMainFrame->m_pListening->Accept(*pSocket, (SOCKADDR*)&SockAddr, &lpSockAddrLen); pRViewK->ShowInfo("TCP connected"); CSocket::OnAccept(nErrorCode); } my qu...

TCP, CAsyncSocket
I am using CAsyncSocket with SOCK_STREAM option (using TCP). I noticed that in most cases the messages I received from the other end were ok, but sometimes two messages were appended together, and sometimes a message was chopped into two separate messages. Is that a problem of using CAsyncSocket? Anyway I can fix it? Do I get the same problem if I don't use CAsyncSocket? Any suggestions? Thanks! dev wrote: > I am using CAsyncSocket with SOCK_STREAM option (using > TCP). I noticed that in most cases the messages I received > from the other end were ok, but sometimes t...

Exchange 2007 Stepping on TCP Ports 5718 and 5719
We can't seem to get Exchange 2007 from grabbing TCP ports 5718 and 5719 and disabling the DPM agent. The only guidance we could find is as follows: http://support.microsoft.com/kb/947682 http://support.microsoft.com/kb/812873/ Strangely, this seems to apply only to Windows Server 2000 and 2003, not 2008. Furthermore, it hasn't stopped Exchange from grabbing these ports. Any advice? As mentioned in the KB, the Exchange System Attendant service might be grabbing these ports. Have you tried the SetAgentCfg tool to change DPM ports as mentioned in the support artic...

Excel TCP port number
Hi folks, Does anyone know which TCP/IP port number Excel uses? None. What are you asking? state wrote: > > Hi folks, Does anyone know which TCP/IP port number Excel > uses? -- Dave Peterson ec35720@msn.com ...

CAsyncSocket datagram reading with ReceiveFrom()
Hi, I need some help with this from MSDN (CAsyncSocket::ReceiveFrom): "For datagram sockets, data is extracted from the first enqueued datagram, up to the size of the buffer supplied. If the datagram is larger than the buffer supplied, the buffer is filled with the first part of the message, the excess data is lost, and ReceiveFrom returns a value of SOCKET_ERROR with the error code set to WSAEMSGSIZE." Suppose my socket received 4 packets (distinct) of 256 bytes each before I begin to read the data and I use a buffer of 512 bytes with ReceiveFrom(). Is ReceiveFrom will return wit...

TCP/IP printing
Hello, I have a networking question. I have a print device with a static IP address on one network. A computer on another network can connect to the printer (NBT is enabled and so is LPR) and the drivers are installed but cannot print. I know this is correct behavior but want to know in detail why it seems to half work. I am guessing it has to do with port 138,139 and 515 but not positive. Probably a better question to ask a networking forum but hey. Thanks can you access the printer through the printer IP address? -- ashvin :D Just chill -pls post your feedback on...

TCP/IP Error
I recently unistalled McAfee and installed Norton Internet Security. Everytime I try to send/recieve mail in Outlook I get a TCP/IP error that says I have been unexpectedly terminated. I can get on the internet and get to my email through our internet provider page. Does anyone have any suggestions? I have already tried working with Norton and everything they have suggested does not work. Thanks. Very likely that you may have some Macafee remnants left over. You should contact Macafee or find a friendly neighborhood computer geek who can safely go thru your system and registry to c...

CAsyncSocket send
Hi, I have a problem sending a string to a server using the CAsyncSocket OnSend function. I was using CSocket and transferring strings to the server using the writestring function of CArchive. Now because of problems with transferring data I have rewritten my code to use Casyncsocket. It all seems to work except that I don't been able to transfer any strings to the server. The server just don't react. Does the function Writestring of CArchive writes any ending characters so the server knows when it end and when it starts? I have tried to add carriage return and line feeds but with n...

Static TCP Port
Do we really need to configure both the registry keys for restricting AD Replication to specific ports? Wouldn't TCP/IP Port reg key alone wont' suffice? What is DCTcpipPort used for? Registry key 1 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NTDS\Parameters Registry value: TCP/IP Port Value type: REG_DWORD Value data: (available port) Back to the top Registry key 2 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters Registry value: DCTcpipPort Value type: REG_DWORD Value data: (available port) begin 666 uparrow.gif M1TE&.#EA&qu...

Adding IP's to 'Advanced TCP/IP Settings'
Hi all, I want to write a script in powershell that will add IP's to a nic. i.e. when added they will appear on the IP Addresses section on 'Advanced TCP/IP Settings' of the TCP/IPv4 properties in 'Local Area Connection Properties'. I've been looking at Win32_NetworkAdapterConfiguration however all the examples I have seen so far just talk about getting IP addresses and adding a static IP. Another consideration.....is there any best practices for adding hundreds of IP's all in one go? Will doing them all in one go give any kind of performance h...

TCP Optimizer problem
I've used TCP Optimizer utility to "optimize" my network connection, after applying changes and playing a little, I decided to revert changes back to original settings, clicked "Restore Windows Default Settings", and after this I can't get internet connection working anymore. I tried restart cable modem, release/renew the IP address, restart PC, but this not help. Cable modem itself is very sensitive to power interruptions and to the signal level. What I think, possibly TCP Optimizer has not fully reverted registry changes back to original settings? [...

Message Queue CAsyncSocket
Hello, I'm using CAsyncSocket to create a client that communicates with a server. I have used two CList<CByteArray*, CByteArray*> as inqueue and outqueue. All Received data ends up in inqueue which I loop thru processing each command in the list. Each 'answer' ends up in outqueue which I also loop thru sending each item to the server. What complicates this is that every 12 seconds I need to send a KeepAlive message. I have tried this (and failed) The normal messages are added to outqueue using AddTail but the KeepAlive message is added to the outqueue using AddHead. T...

reading and writing xml using a tcp stream
Hi, I am writing a client/server application with a desktop and a pocketPC which transfers a xml string which is to be loaded by a dataset on the client. the server uses this code to prepare and send the xml: Dim stringbuilder As New StringBuilder Dim textwriter As StringWriter = New StringWriter(stringbuilder) Dim xmltextwriter As New Xml.XmlTextWriter(textwriter) xmltextwriter.Formatting = Xml.Formatting.Indented Dataset.WriteXml(xmltextwriter, XmlWriteMode.WriteSchema) 'this is the connection to the client Dim w As New IO.StreamWriter(mobjClient.GetStream) ' the &qu...

tcp-ip configuration
Hi, How can I found if a tcp\ip is install in my computer??? How can I read the tcp\ip properties? How can I change them??? What I need is to change the Internet Protocol(TCP\IP) property install on my computer network connection from "obtain an ip address automatically" to "use the following ip address" and set ip number and subnet mask, I need to do that from my mfc application, there is an mfc class for that or some api`s? Can you send me any code example? Thanks! tcp-ip configuration I think you can call GetTcpStatistics() to find out if it is available or not. ...

how to detect TCP disconnects
Hi All, I am using winsock api. When a client disconnects her socket, how can we detect in the server to re-connect to the client once connected. Is there any api function. Thanks. Jr. http://tangentsoft.net/wskfaq/newbie.html#normalclose Sreeram vda wrote: > Hi All, > > I am using winsock api. When a client disconnects her socket, how can we > detect in the server to re-connect to the client once connected. Is there > any api function. > > Thanks. > Jr. > > Thank you very much. It helped. Jr. "Sreeram" <sreeram0425@netscape.net> w...

read from TCP/IP socket with blocking
So, I currently have code for reading in TCP/IP messages that looks something like this: StreamReader reader = new StreamReader(new NetworkStream(mySocket))); while (true) { string inputString = reader.ReadLine(); _newMessages.Enqueue(inputString); } This is one thread in my application, and as it is a non-blocking infinite loop, it is causing my application to consume 100% of my CPU. Is there a simple method of rewriting this to cause it to block when there's nothing else to read? (thereby freeing up CPU) I attempted to use StreamReader.ReadBlock(), but then rea...

XmlTextReader TCP Socket
Hi all, This is repost from dotnet.general, I have only just found this forum. I am writting a Jabber-esque client-server application and have run into an issue with the XmlTextReader constructor when passing a NetworkStream - It hangs forever, apparently this is an issue with SP1. Anyway I have the code below as a work around (using bool keepalives as it must run on the CF). I realise there is refactoring to do, as I said this was a quick and dirty workaround but the question holds. I am concerned that a very large file coming in very slowly could be a problem. Could this code empty the u...

TCP Send with Response
I have to send a TCP stream to a device. The device returns a response and I cannot figure out how to catch the response. I know the device response properly because if I connect to it using HyperTerminal I can send the command and immediately the response is displayed back to me in HyperTerminal. Now I need to do this programmatically. The send portion of the code below works because the device response properly but for some reason I'm unable to catch the response. I've looked through MANY examples online and they all seem to look like this code block. Please he...

Block TCP 25 to CLient Computers NOT WORKING
Hi All, SBS 2003 R2 , 2 NICs, ISA Installed... I need to close port 25 on all client computers and followed this article http://technet.microsoft.com/en-us/library/aa998122(EXCHG.65).aspx I created the policy and while testing it from my Windows 7 client machine / Windows Live Mail ... I could send out mail without any issues..!? Is this correct? I was under the impression it would block outbound 25 on all workstations.. Thanks for all the help Dan Since you have it why not use ISA to block port 25 outbound from the workstations? "Daniel Jewel" <cyber...

Exhcnage 2003 TCP port requirement
I have all these ports opened on the firewall, and still am unable to get mails on my Exchange Server. But when i open all the ports for TCP on firewall, i get emails ... can any one help me if i need more ports 25 SMTP 80 HTTP 80 RVP 102 X.400 110 POP3 119 NNTP 135 MS-RPC 143 IMAP4 379 LDAP 389 LDAP 390 LDAP 443 HTTP/SSL 465 SMTP/SSL 563 NNTP/SSL 636 LDAP/SSL 691 SMTP/LSA 993 IMAP4/SSL 994 IRC/SSL 995 POP3/SSL 3268 LDAP 3269 LDAP/SSL 6667 IRC/IRCX 1503 T.120 552 ULS 1720 H.323 1731 Audio 53 DNS ...

Debug assertion failed trying to CAsyncSocket::Connect()
Hi, I have a client (locally) trying to connect to a server locally. If i connect (using CAsyncSocket::Connect()) with the IP that IPconfig gives, i get a debug assertion failed. After pressing the RETRY button on the debug assertion failed dialog the source goes to : /* * If this ASSERT fails, a bad pointer has been passed in. It may be * totally bogus, or it may have been allocated from another heap. * The pointer MUST come from the 'local' heap. */ _ASSERTE(_CrtIsValidHeapPointer(pUserData)); in dbgheap.cpp If i connect on 127.0.0.1 e...

Large TCP packet size. #2
My programs have always used relatively small sized TCP packets. I am now considering using a packet that is about 75,000 bytes long. Is that to long? TonyG wrote: > My programs have always used relatively small sized TCP packets. I am now > considering using a packet that is about 75,000 bytes long. > > Is that to long? It will probably make no difference. Your calls only affect a local buffer size, not the actual network packet size. -- Scott McPhillips [VC++ MVP] It would have made sense, if he tried to increase the tcp window size. -- Vipin Aravind http://www.ex...