Receive data from a connected socket
[Visual Basic]
Public Shared Function SDLNet_TCP_Recv( _
ByVal
sock As
TCPsocket, _
ByVal
data As
IntPtr, _
ByVal
maxlen As
Integer _
) As
Integer
Parameters
-
sock
- This is a valid, connected, TCPsocket.
-
data
- This is a pointer to the buffer that receives the data from sock.
-
maxlen
- This is the maximum length (in bytes) that will be read into data.
Return Value
the number of bytes received. If the number returned is less than or equal to zero, then an error occured, or the remote host has closed the connection.
Remarks
Receive data of exactly length maxlen bytes from the socket sock, into the memory pointed to by data. This routine is not used for server sockets. Unless there is an error, or the connection is closed, the buffer will read maxlen bytes. If you read more than is sent from the other end, then it will wait until the full requested length is sent, or until the connection is closed from the other end. You may have to read 1 byte at a time for some applications, for instance, text applications where blocks of text are sent, but you want to read line by line. In that case you may want to find the newline characters yourself to break the lines up, instead of reading some inordinate amount of text which may contain many lines, or not even a full line of text.
Binds to C-function call in SDL_net.h:
extern DECLSPEC int SDLCALL SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
Example
// receive some text from sock
//TCPsocket sock;
#define MAXLEN 1024
int result;
char msg[MAXLEN];
result=SDLNet_TCP_Recv(sock,msg,MAXLEN);
if(result<=0) {
// An error may have occured, but sometimes you can just ignore it
// It may be good to disconnect sock because it is likely invalid now.
}
printf("Received: \"%s\"\n",msg);
See Also
SdlNet Class | Tao.Sdl Namespace | SDLNet_TCP_Open | SDLNet_TCP_Close | SDLNet_TCP_Accept | SDLNet_TCP_Send | SDLNet_TCP_GetPeerAddress | SdlNet.TCPsocket