Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / January 2007



Tip: Looking for answers? Try searching our database.

vvb6 Winsock not receiving all data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RobertTheBruce - 19 Jan 2007 15:38 GMT
This problem is driving me mad, and from this forum lot's of other
people too!

I'm using Winsock to send and receive data large amount's of data
(1000's records) as a string.  The first time I do this it seems to
work fine.  But if I refresh the data several times, eventually for
some reason it can not get all the data sent.

For your info I've included my DataArrival event.

Private Sub ws1_DataArrival(ByVal bytesTotal As Long)
   Dim sDataBlock As String
   Dim temp As String

   iDataArrivals = iDataArrivals + 1

    ws1.GetData sDataBlock
    sDataBlock = Replace(sDataBlock, vbCrLf, "")

   ' I've added this code so that when it recieves the last block of
data, where iDataArrivals = 29
   ' get some more data but it just returns "".
   If iDataArrivals = 29 And bytesTotal <> 8192 Then
       ws1.GetData temp
       MsgBox temp
   End If

   data = data & sDataBlock

End Sub

Any help would be appreciated, with code samples?

Rob
NeilH - 19 Jan 2007 15:53 GMT
> This problem is driving me mad, and from this forum lot's of other
> people too!
[quoted text clipped - 30 lines]
>
> Rob

Why are you counting how many times you receive data ie "iDataArrivals = 29"
as you have no control over block size of the IP frames when presented to
you?.
If you are looking at 29, what happens if it takes 28 or 30?
What I would do is to supply a specific, unique frame when you have finished
sending data and then look for that frame when you receive the data. That
way you know when you have finished receiving data..

Neil..
RobertTheBruce - 19 Jan 2007 16:24 GMT
Rob,

I put the if block for testing purposes -please ignore if this confuses
you!

The point is that a close event is firing before all the data has been
received, and the last DataArrival event fires?

Rob

> Why are you counting how many times you receive data ie "iDataArrivals = 29"
> as you have no control over block size of the IP frames when presented to
[quoted text clipped - 5 lines]
>
> Neil..
NeilH - 19 Jan 2007 16:44 GMT
What close event?

> Rob,
>
[quoted text clipped - 15 lines]
> >
> > Neil..
RobertTheBruce - 19 Jan 2007 16:57 GMT
The one that come's the windock component

Private Sub ws1_Close()
   Dim iParts As Integer

   ws1.Close
   data = Split(data, "<%data%>")

   iParts = UBound(data)

   If iParts = 0 Then
       MsgBox "An Error Has Occured"
   Else
      data = data(1)
   End If

   recvd = True
End Sub

> What close event?
>
[quoted text clipped - 21 lines]
> > >
> > > Neil..
Bob O`Bob - 19 Jan 2007 19:20 GMT
> The one that come's the windock component
>
> Private Sub ws1_Close()
>     Dim iParts As Integer
>
>     ws1.Close

That just looks ... off.

I don't really have "my winsock hat on" right now, but I'm pretty sure
that none of my code ever calls .Close on the receiving side.  Seems
pretty pointless and possibly obstructive.

>     data = Split(data, "<%data%>")
>
[quoted text clipped - 8 lines]
>     recvd = True
> End Sub

    Bob
--
Sinna - 22 Jan 2007 07:33 GMT
>> The one that come's the windock component
>>
[quoted text clipped - 4 lines]
>
> That just looks ... off.

No, it doesn't. The MSDN Library documentation advises it:
<q>
The Close Event occurs when the remote computer closes the connection.
Applications should use the Close method to correctly close a TCP
connection.
</q>
See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mswnsk98/html/v
bevtCloseEvent.asp


<snip>
Sinna
Bob O`Bob - 22 Jan 2007 08:57 GMT
>>> The one that come's the windock component
>>>
[quoted text clipped - 16 lines]
> <snip>
> Sinna

Note that those are two separate sentences.
I'm sure it's a one-or-the-other situation.

Do both, and the event code probably gets invoked twice,
which, in the case of the code we've been looking at,
would probably screw up the data buffer.
...Perhaps even leading the OP to think some of the data never arrived...

    Bob
--
Sinna - 22 Jan 2007 09:58 GMT
>>>> The one that come's the windock component
>>>>
[quoted text clipped - 26 lines]
>
>     Bob
Well...

Speaking out of experience I do also have to call the Close routine from
the Winsock Control, otherwise the connection stays up in a kind of
tristate: State = tcpClosing.
BTW: Why should the docs state '... to correctly close ...' the
connection when documenting the Close Event.
I've also noticed that invoking the Close routine doesn't raise the
Close Event at all.

Sinna
RobertTheBruce - 22 Jan 2007 12:54 GMT
All,

Thanks for all your help!  But I still haven't managed to get to the
bottom of this problem, so any more feedback would be welcome.  I tried
a number of suggestions, but none of them worked, so I've written a
'work around'.

In the end I pass '<end>' at the end of the string of records at the
server, and now test for this in  the global variable Data, in the
close event.  If it's not present, then i request the entire data, from
scratch, again.  It's not perfect, but it does seem to work!

For your information, I've noticed the following.

1)  Some times the last DataArrival event does not always contain the
entire last chuck of data sent from the server, and the Close event
will fire.  Now from what some of you said the next DataArrival event
that occurs should contain the missing data, but it never does!

2)  I've noticed that during normal office hours the problem's occur,
more frequently than outside!

3)  On the server side I have PHP file that is uses an ODBC driver to
run a SQL query to get the record's I required, and then pass then back
to the client app, written in VB6, to displayed in an MSFlexGrid.

Rob
Thorsten Albers - 22 Jan 2007 16:24 GMT
RobertTheBruce <robert_bruce@hargreaveslansdown.co.uk> schrieb im Beitrag
<1169470447.438816.192830@m58g2000cwm.googlegroups.com>...
> 1)  Some times the last DataArrival event does not always contain the
> entire last chuck of data sent from the server, and the Close event
[quoted text clipped - 7 lines]
> run a SQL query to get the record's I required, and then pass then back
> to the client app, written in VB6, to displayed in an MSFlexGrid.

As far as I can see your PHP script closes the connection as soon as it has
sent the last data packet. I don't know anything about the machine on which
you PHP script runs - but maybe this closing prevents the remaining data
which are still in the queue from being sent. Instead try this:
- After PHP has sent the last package of data it should enter a kind of
'listen mode' and wait for an acknowledgement of you VB application that it
has recieved all data. If the acknowledgement isn't received within a
certain time interval, the PHP script should close the connection terminate
with an error. If it got the receipt it should close the connection and
terminate successfull.
- You VB application should acknowledge the receipt of all data by sending
the PHP script a certain data sequence ('<ack>' or the like). If it hasn't
recieved the data within a certain time interval it should close the
connection and terminate with an error.

Signature

----------------------------------------------------------------------
THORSTEN ALBERS                       Universität Freiburg
                                               albers@
                                                      uni-freiburg.de
----------------------------------------------------------------------

Neil.. - 19 Jan 2007 19:35 GMT
Sorry. I realised after sending the post that there was a close event!.
It is something I have never used.

It also appears that you have more code than you originally said. It would
be better if you supplied ALL your code being controlled by Winsock as
events can interact, Also what is resetting various parameters (Such as
"data")?
What is "data(1)" in the close event below ?

> The one that come's the windock component
>
[quoted text clipped - 42 lines]
>> > >
>> > > Neil..
Thorsten Albers - 19 Jan 2007 21:49 GMT
RobertTheBruce <robert_bruce@hargreaveslansdown.co.uk> schrieb im Beitrag
<1169225840.961610.204860@a75g2000cwd.googlegroups.com>...
> Private Sub ws1_Close()
>     Dim iParts As Integer
[quoted text clipped - 8 lines]
>     recvd = True
> End Sub

Are you really sure that Close-Event can't be fired before the last
DataArrival-Event has been fired? The Close-Event is fired when the remote
computer closed the connection and not when the last byte of the data has
been recieved.
Maybe you should try this:

Private Sub ws1_Close()
Dim iParts As Integer
Dim sDataBlock As String

' Insert something like this:
If ws1.BytesReceived Then
 ws1.GetData sDataBlock
 sDataBlock = Replace(sDataBlock, vbCrLf, "")
 data = data & sDataBlock
End If

ws1.Close

data = Split(data, "<%data%>")
iParts = UBound(data)
If iParts = 0 Then
 MsgBox "An Error Has Occured"
Else
 data = data(1)
End If

recvd = True

End Sub

And ws1.Close is not necessary since the connection has already been
closed. If it wouldn't have been closed the Close event wouldn't have been
fires.

Signature

----------------------------------------------------------------------
THORSTEN ALBERS                       Universität Freiburg
                                               albers@
                                                      uni-freiburg.de
----------------------------------------------------------------------

Bob Butler - 19 Jan 2007 15:58 GMT
> This problem is driving me mad, and from this forum lot's of other
> people too!
<cut>
>     ' I've added this code so that when it recieves the last block of
> data, where iDataArrivals = 29
>     ' get some more data but it just returns "".
>     If iDataArrivals = 29 And bytesTotal <> 8192 Then

If you are sending 8192 bytes of data then that's what you need to wait for.
It may come in 1 DataArrival or 8192 DataArrivals or anything in between and
it can vary from run to run.  Lose the requirement for 29 of them.

Signature

Reply to the group so all can participate
VB.Net: "Fool me once..."

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.