[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.6 SDLNet_TCP_Recv

int SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)

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.

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.

Returns: 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.

 
// 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:
3.3.5 SDLNet_TCP_Send, 3.3.3 SDLNet_TCP_Accept, 3.3.1 SDLNet_TCP_Open, 3.3.4 SDLNet_TCP_GetPeerAddress, 3.3.2 SDLNet_TCP_Close, 4.2 TCPsocket


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November, 3 2009 using texi2html