Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

IOCP question: ordering of data

111 views
Skip to first unread message

Ulrike Urban

unread,
Jun 18, 2007, 6:05:45 AM6/18/07
to
Hi,

I'm messing around with TCP programming using iocps (XP Pro)

If multiple iocp-worker threads are used, how data-corruption can be
avoided.

Example:

- Thread #1 WSASends a block of data.
This send could not completely be done by winsock, so the completion
notification comes back with according byte counts, (bytes_sended <
bytes_to_be_sended).

Thread #1 tries to WSASend the rest of the data-block

- just before issuing the WSASend, Thread #1 is preempted by Thread #2
Thread #2 WSASends another block of data.
Here data corruption may/will occur.

In such a scenario (it is never guaranteed that winsock is able to send
all data of a WSASend-Call, the app may be required to re-send the rest)
how can multiple threads work properly?
My understanding is that if I issue only ONE WSASend on one socket, data
integrity can be guaranteed.
This implies that multiple sends issued from multiple threads to one
socket does not make sense. Multiple sends from one thread would be ok.
But then there is no advantage using multiple threads...

Alexander Nickolov

unread,
Jun 18, 2007, 4:24:30 PM6/18/07
to
Data sequencing synchronization is up to you.

On a side note, WSASend will never return with a partial
success - it's all or nothing. This doesn't even make sense
for overlapped I/O though, since your send is not completed
yet. It's the completion notification that's all or nothing. But,
if you do get a partial completion, that means all subsequent
sends will complete with failure (the socket is toast).

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Ulrike Urban" <ULUR@fang_mich_.de> wrote in message
news:5dn3o7F...@mid.individual.net...

Skywing [MVP]

unread,
Jun 18, 2007, 9:39:34 PM6/18/07
to
It can do partial completion immediately for nonblocking-sockets mode. For
blocking sockets or for overlapped operation, it's all or nothing.

--
Ken Johnson (Skywing)
Windows SDK MVP
http://www.nynaeve.net
"Alexander Nickolov" <agnic...@mvps.org> wrote in message
news:unFwtaes...@TK2MSFTNGP02.phx.gbl...

Arkady Frenkel

unread,
Jun 19, 2007, 3:05:04 AM6/19/07
to
A good example of such is iocp sample in PSDK which use additional(s)
send(s) in advance in the same way as receive
Arkady

"Skywing [MVP]" <skywing_...@valhallalegends.com> wrote in message
news:uxIUyKhs...@TK2MSFTNGP04.phx.gbl...

Alexander Nickolov

unread,
Jun 19, 2007, 12:44:35 PM6/19/07
to
OP is using IOCP, e.g. overlapped I/O. Thus my statement.

BTW, in theory WSASend can do partial writes for blocking
sockets (non-overlapped) as well. In practice this never happens,
not even for non-blocking (non-overlapped) writes. But that's
an implementation detail not guaranteed to persist between
WinSock implementations...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Skywing [MVP]" <skywing_...@valhallalegends.com> wrote in message
news:uxIUyKhs...@TK2MSFTNGP04.phx.gbl...

Ulrike Urban

unread,
Jun 21, 2007, 7:09:39 AM6/21/07
to
Thanks folks!

I used the iocp-server sample as a reference.

--------------------------------------- start snippet ----------------
case ClientIoWrite:

//
// a write operation has completed, determine if all the data
intended to be
// sent actually was sent.
//
lpIOContext->IOOperation = ClientIoWrite;
lpIOContext->nSentBytes += dwIoSize;
dwFlags = 0;
if( lpIOContext->nSentBytes < lpIOContext->nTotalBytes ) {

//
// the previous write operation didn't send all the data,
// post another send to complete the operation
//
------------------------------------- end snippet ----------------

This is the very point of interest. My understanding was, that partial
sends can occur........

If that won't be possible there won't be any problems regarding multiple
threads either.

But who can make a safe statement?

m

unread,
Jun 21, 2007, 10:10:47 AM6/21/07
to
FYI: the problem is that the API, WSASend, is documented with the
possibility of partial writes but the implementation, at least for
overlapped IO, cannot perform a partial write. Further, it does not make
sense for overlapped or scatter / gather IO to have partial writes - it
would defeat to purpose of having it overlapped! And if the IO is going to
fail (out of memory etc.), then it just fails. The exception being
disconnect where a partial write does make sense, but since no further
writes can succeed, there is no sequencing problem.


"Ulrike Urban" <Nospam@to_me_.de> wrote in message
news:5dv4jtF...@mid.individual.net...

Ulrike Urban

unread,
Jun 25, 2007, 10:20:35 AM6/25/07
to
Thanks 4 your explanations!
0 new messages