An event for Data arrival with .NET Sockets

sampoo

Active member
Joined
Jun 12, 2004
Messages
25
Location
Belgium
Programming Experience
3-5
Hi,

I'd like to bind an event (or a sub or so) to the Socket.Arrival property, so that when this value changes (Data Received) the sub or event is called. How can I do this without using timers or polling?


Thanks a lot
 
declare mobjClient as socket and do the connections and then use the following function.


"" mobjClient.BeginReceive(marData, 0, BUFFER_SIZE, SocketFlags.None,
AddressOf ReceiveData, Nothing) ""

Here Receivedata is the sub where u can write the neccessary code.
mardata is a byte array.
buffer_size is an integer value of the size of the buffer.



 
Re:

Thanks a lot. Is there perhaps a way where i can dynamically set the buffer size (such as the size of the received data), or should it always be done in blocksizes, e.g. 1024, or so...?


thanks a lot
 
Re:

It gave me an error:
Method 'Public Sub DataReceived()' does not have the same signature as delegate 'Delegate Sub AsyncCallback(ar As System.IAsyncResult)'.

When using this code:
frmMain.ASock.BeginReceive(clientData.SetRawData, 0, 8194, SocketFlags.None, AddressOf DataReceived, Nothing)

SetRawData is a property.
 
The ReceiveData function should be like this:

Public Sub ReceiveData(ByVal ar As IAsyncResult)
Dim intCount As Integer

Try

SyncLock mobjClient

intCount = mobjClient.EndReceive(ar)

End SyncLock

If intCount < 1 Then

' RaiseEvent Disconnected(Me)
' (ur code)
Exit Sub

End If

' (ur code)
...
...
SyncLock mobjClient

' (for continous listening)
mobjClient.BeginReceive(marData, 0, BUFFER_SIZE, SocketFlags.None,
AddressOf ReceiveData, Nothing)

End SyncLock

Catch e As Exception

RaiseEvent Disconnected(Me)

End Try

End Sub

 
Sorry for the late reply.

Could you explain me how to use your sub?

I did this:

* At the point the asynchronous receive should start:

VB.NET:
  Dim ar As IAsyncResult
  
  		ReceiveData(ar)
I also tried:
VB.NET:
ReceiveData(Nothing)
, it gave me the same result

* The function itself:
VB.NET:
  Public Sub ReceiveData(ByVal ar As IAsyncResult)
  		Dim intCount As Integer
  
  		Try
  
  			SyncLock frmMain.ASock
  
  			    intCount = frmMain.ASock.EndReceive(ar)
  
  			End SyncLock
  
  			If intCount < 1 Then
  
  				' RaiseEvent Disconnected(Me)
  				' (ur code)
  				Exit Sub
  
  			End If
  
  			Call clientData.HandleData()
  
  			SyncLock frmMain.ASock
  
  				' (for continous listening)
 			 frmMain.ASock.BeginReceive(clientData.SetRawData, 0, 8194, SocketFlags.None, AddressOf ReceiveData, Nothing)
  
  			End SyncLock
  
  		Catch e As Exception
  
  		    MsgBox("An error occured while receiving data: " & e.Message)
  
  		End Try
  
  
  	End Sub


It didn't work...

thanks in advance
 
Hi,

You can't call the sub directly. You can call the delegate like this:

frmMain.ASock.BeginReceive(clientData.SetRawData, 0, 8194, SocketFlags.None, AddressOf ReceiveData, Nothing)

Thanks.
 
Back
Top