wwsocket::Receive

Receives data from a socket.

To receive large amounts of data you will have to use Receive() in a loop until you've received the expected content size. There's no way to know when content is complete if neither a size or terminating string is available. In this situation you have to keep reading until you get a timeout. Most protocols (HTTP, SMPT, POP3 etc) provide either terminating character for commands and content (SMTP,POP3 etc.) or provide a content size (HTTP) that allows you read an exact amount of characters from the socket.

o.receive(lnSize)

Return Value

string of content - empty string on error or no data.

Parameters

lnSize
The size of the input buffer that reads data from the server. This size determines the maximum size of the buffer returned to you. It's not guaranteed that this buffer will be filled with data to this size however as the content may be shorter or the socket request may send the data in smaller chunks.

If you're sending large content use a large value to minimize the VFP overhead while reading data. If you need to track socket data very closely for incoming bytes set the buffersize to 1 to control all aspects of the socket on your own. This option gives pretty close control of how data is returned to you.

Remarks

Depending on the lStripNulls setting this buffer will return with CHR(0)s to the full size of the buffer.

If you want to receive binary data that contains Nulls make sure you set lStripNulls to .F. You need to use code like the following to retrieve binary data:

oSock = CREATEOBJECT("wwSocket")
oSock.lStripNulls = .F.
oSock.Connect("localhost",88)

? oSock.Send("Test" + CHR(0) + " More testing is required following that null")
lcResult = oSock.Receive(150)
? oSock.nLastReadSize  && 93

*** extract just the 'real buffer'
lcResult =  SUBSTR(lcResult,1,oSock.nLastReadSize)
? lcresult

A good example of usage for Receive() and WaitFor() is provided in the wwPop3 class.

Example

*** SMTP Server Logon
loIP = CREATE( "wwSocket")
loIP.lStripNulls = .T.
loIP.Connect("mail.somemailserver.net",25)

*** Retrieve the welcome message
loIP.Receive() && Welcome Message

*** Send username
loIP.Send("helo Rick" + CRLF)

*** Echos back the mail servers welcome message
? loIP.Receive()

*** Close the connection
loIP.Disconnect()

See also:

Class wwsocket | wwsocket::nBufferSize | wwsocket::WaitFor | wwsocket::WaitForSize

© West Wind Technologies, 2023 • Updated: 11/13/01
Comment or report problem with topic