Visual Basic
This function works the same as the send () function. The main difference between send () and recv () is that the buffer pointer passed to the recv () function must point to an empty buffer, and the buffer length parameter indicates how much memory was allocated for the buffer. Like the send () function, recv () returns the number of bytes received. If an error occurs, the function returns SOCKET_ERROR. The recv () function is also a blocking function, so it will not return until the remote host sends something or the connection is lost. You can use the IsDataAvailable () method to determine if there is some data to retrieve or not.
Public Function ReceiveData(strData, lngBytesReceived) As Boolean
Const MAX_BUFF_SIZE = 10000
Dim Buff(0 To MAX_BUFF_SIZE) As Byte
Dim WSAResult As Long
WSAResult = recv(mlngSocket, Buff(0), MAX_BUFF_SIZE, 0)
If WSAResult = SOCKET_ERROR Then
SetLastErrorCode "Error in RecvData::recv"
strData = ""
lngBytesReceived = 0
ReceiveData = False
Else
lngBytesReceived = WSAResult
Buff(lngBytesReceived) = 0
strData = Left(StrConv(Buff(), vbUnicode), lngBytesReceived)
ReceiveData = True
End If
End Sub
This method retrieves all data sent by the remote host through a socket. He gets an option about where to store data and another option about where to store the number of bytes received. The method uses variant parameters, since it is designed specifically for use in scripting languages such as VBScript, where all variables are variant types.
DOWNLOAD List >>
Visual Basic Source Code. TCP Client-Server
|