That's exactly how I use it..
I'm asking if the Download method will return by itself after all data is
received or should I take care to check if all data was received by parsing
and interpreting the data..
Data is sent by a server I designed with TCP sockets following some MS
sample.
However, in the server, I receive data request in multiple steps. First I
receive only the headers, and in a second receive I have the body if the
method of WebClient was UploadData...
I may miss something at the server side anyway, there should be some length
that I can check against received buffer but I cant figure out now.
I cant find that length
The BeginReceive of the receive socket have the following function as
callback:
Private Sub Receive(ByVal ar As IAsyncResult) 'este apelata cand au ajuns
date
Try
Dim content As [String] = [String].Empty
Dim state As StateObject = CType(ar.AsyncS tate, StateObject) '
Retrieve the state object and the handler socket from the async state
object.
Dim handler As Socket = state.workSocke t 'this is client socket
Dim bytesRead As Integer = handler.EndRece ive(ar) '
Read data from client socket.
If bytesRead > 0 Then
'state.sb.Appen d(Encoding.ASCI I.GetString(sta te.buffer, 0,
bytesRead))
' There might be more data, so store the data received so
far.
Dim lastLength As Integer
If state.received Is Nothing Then
lastLength = 0
Else
lastLength = UBound(state.re ceived)
End If
ReDim Preserve state.received( lastLength + bytesRead - 1)
Array.Copy(stat e.buffer, 0, state.received, lastLength,
bytesRead)
content = Chr(state.recei ved(state.recei ved.Length - 1))
Dim bc As BitConverter
'Dim lenBuff As Long = bc.ToInt64(stat e.buffer, 0)
If content = state.endTransm ition Then
' All the data has been read from the client
Dim utilData(state. received.Length - 102 -
state.endTransm ition.Length) As Byte 'pana la 100 sunt headere ultimul e
terminator
Array.Copy(stat e.received, 101, utilData, 0,
state.received. Length - 101 - state.endTransm ition.Length)
'If lenBuff = bytesRead Then
'Request received; processing ...
Dim response As Byte()
If Not myGame Is Nothing Then
response = myGame.ProcessC lientRequest(ut ilData)
Else
response = myManager.Proce ssClientRequest (utilData)
End If
'"Accept-Language:en-us" + vbCrLf +
"Accept-Encoding:gzip, deflate" + vbCrLf +
'Dim response As Byte() =
Encoding.ASCII. GetBytes("<html ><head></head><body>Noth ing to see
here!</body></html>")
allBytesRead += bytesRead
RaiseEvent BytesReceived(a llBytesRead)
Send(handler, response)
Else
handler.BeginRe ceive(state.buf fer, 0,
StateObject.Buf ferSize, 0, New AsyncCallback(A ddressOf Receive), state) '
Not all data received. Get more.
End If
Else
'nothing to read: client closed
handler.Shutdow n(SocketShutdow n.Both)
handler.Close()
openConnections -= 1
RaiseEvent NewConnection(o penConnections)
End If
Catch e As SocketException
'client close
openConnections -= 1
RaiseEvent NewConnection(o penConnections)
End Try
End Sub 'ReadCallback
--
Ceers,
Crirus
------------------------------
If work were a good thing, the boss would take it all from you
------------------------------
"Cor" <no*@non.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
Hi Crirus,
The webclient is in my idea the best placed in a seperated thread, then
you could abort it also, I think that will be your next problem if you have
not done that already.
I do use it with a try, catch end block an then empty string is a good
result.
Public Function download(Uri, Filename) as string
Dim browser As New System.Net.WebC lient
Try
browser.Downloa dFile(Uri, Filename)
Return ""
Catch Ex As Exception
Return Uri & " " & Ex.Message
End Try
End Function
Cor
I use a webClient for requesting data from a server of mine.
Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a terminator sign in the
data in order to realise in the client that all data arrived?