SocketWrench .NET Edition

InternetServer.OnRead Event

Occurs when data is available to be read from the client.

[Visual Basic]
Public Event OnRead As OnReadEventHandler
[C#]
public event OnReadEventHandler OnRead;

Event Data

The event handler receives an argument of type InternetServer.ReadEventArgs containing data related to this event. The following InternetServer.ReadEventArgs property provides information specific to this event.

Property Description
Handle Gets a value that specifies the socket handle for the client session.

Remarks

The OnRead event is generated when the client sends data to the server. The Handle event argument property specifies the handle to the client socket which can be used with the Read or ReadLine methods to read the data that was sent.

When this event fires, it guarantees that data can be read from the specified client without causing the current thread to enter a blocked state. However, calling this method multiple times inside the event handler may cause the current thread to block when there is no more data available to read. The IsReadable property can be used to determine if there is additional data available to be read.

User interface controls can only be accessed from the UI thread that created them, and attempting to update a control from another thread can result in the program becoming non-responsive or terminating abnormally. Because this event is generated in the context of the client thread, not the thread that created the class instance, you cannot directly modify a control from within this event handler. Instead, you must create a delegate and use the Invoke method to marshal invocations to the associated UI thread. For more information, refer to the documentation for the control.

Example


[Visual Basic]
Private Sub Server_OnRead(ByVal sender As Object, ByVal e As System.EventArgs) Handles Socket.OnRead
    Dim strBuffer As String
    Dim nRead As Integer
 
    ' Read up to m_nBufferSize bytes of data from the client
    nRead = Server1.Read(e.Handle, strBuffer, m_nBufferSize)
 
    If nRead > 0 Then
        ' Process the data that has been read from the client
        ProcessData(strBuffer)
    End If
End Sub
    

See Also

InternetServer Class | SocketTools Namespace