473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Socket Client - bytesread never reaches zero

I am using vs 2003, below is the callback method for my socket. The way I
understand how all this works is: sockDataArrival gets started as a thread,
and reads from the server using EndReceive. It checks for bytesRead and if
there is nothing left to read, bytesRead should be zero, otherwise, another
thread is started and so on untill all data is read and bytesRead =0. The
problem I am having is that bytesRead never equals 0. In the first part of
the bytesRead if statement this line "RaiseEvent
onDataArrival(StringToBytes(state.sb.ToString), bytesRead) 'debug" just puts
data into a textbox. All my data comes back. Why is bytesRead never 0 ?
The catch expression never gets fired either so I dont think i am running
into a socket error. Can anyone spot something wrong in my code ? I need
to know when all the data has arrived so I can fire off an event and process
the data I am receiving.

Private Sub sockDataArrival(ByVal ar As IAsyncResult)
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
Dim bytesRead As Integer

Try
bytesRead = client.EndReceive(ar)
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try

Try
Dim Data() As Byte = state.buffer
If bytesRead = 0 Then
'client.Shutdown(SocketShutdown.Both)
'client.Close()
'RaiseEvent onDisconnect()
'Exit Sub
End If

If bytesRead 0 Then
state.sb.Append(Encoding.ASCII.GetString(state.buf fer, 0,
bytesRead))

'state.sb.Append(BytestoString(state.buffer)) 'save data off to the
side
ReDim state.buffer(256) 'clear buffer
'we might not be done so receive again
RaiseEvent onDataArrival(StringToBytes(state.sb.ToString),
bytesRead) 'debug
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, AddressOf
sockDataArrival, state)
Else
'try not to call data arrival until all data comes in
'data could be bigger than buffer
'data could be sent to us in chunks
'data could be smaller than buffer
'state.sb.Append(BytestoString(state.buffer)) 'save data off to the
side
Dim thing As String = state.sb.ToString
Dim thing2 As Byte() = StringToBytes(thing)
RaiseEvent onDataArrival(StringToBytes(state.sb.ToString),
bytesRead) 'should be done
End If
Catch
RaiseEvent onError(Err.Description)
Exit Sub
End Try
End Sub
Feb 6 '08 #1
0 1096

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Smith | last post by:
Dear Readers, I am in the process of implementing a non-blocking ASYNC TCP client using the .Net socket class. I am a little confused by the MSDN example. I have pasted the code here. ...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: Droopy | last post by:
Hi, I try to implement a reusable socket class to send and receive data. It seems to work but I have 2 problems : 1) I rely on Socket.Available to detect that the connection is closed (no...
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so… We made a small games called reversi, now our job...
2
by: Marty | last post by:
Hi, I hope you can help me with this one (my code is below my message). I have many clients connecting to my server. When any of them disconnect, something happen. This user get disconnect...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
3
by: Cheryl | last post by:
Hi. I am having a problem on handling asynchronous sockets in C#. I implemented a pair of client and server sockets. The connection is ok when first connected. However, when I turned off the...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets. This process proceeds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.