SOCK.Receive

Previous  Next

 

ret = SOCK.Receive( nSocket  AS INTEGER,

                    data     AS REFERENCE|ARRAY|STRING,

                   [nLength  AS INTEGER=SL_USE_DEFAULT],

                   [rTimeout AS REAL=0]                  ) AS INTEGER

 


 

Receives data from a connected server.  If nLength is omitted, then the length of the data buffer is used..  The timeout is specified in seconds.  If timeout is set to 0, it will not timeout.

 

The function returns the number of bytes received.

 

Example:

 

' Reads one frame of 3D data from a RT3DServer

 

OPTION BASE 1

 

$LIBRARY "MyBASIC2_ExtLib"

$LIBRARY "MyBASIC2_SockLib"

 

DIM nSocket   AS INTEGER

DIM header(2) AS INTEGER

DIM nReceived AS INTEGER

DIM nMarkers  AS INTEGER

DIM nFrameNo  AS INTEGER

DIM i         AS INTEGER

 

' Establich a connection with a local RT3DServer

nSocket = SOCK.Connect( "127.0.0.1", 1221 )

 

IF nSocket > 0 THEN

  PRINT "Connect to IP: ";SOCK.GetServerName( nSocket );", Port: ";SOCK.GetPort( nSocket ),

 

  ' Request a frame from RT3DServer

  SOCK.Send( nSocket, "S" )

 

  ' Get frame header with a 10 second timeout

  nReceived = SOCK.Receive( nSocket, header, 8, 10 )

 

  IF nReceived = 8 THEN

 

     ' The header is formatted as

     '

     ' Byte 0:    Type '3'

     ' Byte 1:    Version '1'

     ' Byte 2..5: Frame Number

     ' Byte 6..7: No of Markers

     '

     nMarkers = WORD.HI( header(2) )

     nFrameNo = LONG.Make( WORD.HI( header(1) ), WORD.LO( header(2) ) )

 

     PRINT "Data Type:     "; CHR( BYTE.LO( WORD.LO( header(1) ) ) ),

     PRINT "Data Version:  "; CHR( BYTE.HI( WORD.LO( header(1) ) ) ),

     PRINT "Frame Number:  "; nFrameNo,

     PRINT "No Of Markers: "; nMarkers,

 

     ' Create a frame buffer using the number of markers from header

     DIM frame( nMarkers, 3 ) AS REAL

 

     ' Read the 3D frame from server

     nReceived = SOCK.Receive( nSocket, frame )

 

     FOR i = 1 TO nMarkers

        PRINT FORMAT( "Marker %d: %8.1f, %8.1f, %8.1f", i, frame(i,1), frame(i,2), frame(i,3) ),

     ENDFOR

  ELSE

     PRINT "ERROR: Receiving Frame Header",

  ENDIF

 

  ' Done, close down the connection

  SOCK.Close( nSocket )

ELSE

  PRINT "ERROR: Unable to connect to Server",

ENDIF

 

END