As mentioned in above comments, you need to
- be sure about format of the string returned from
recv()
call
- Tokenize the received string
Be aware since any strtok()
family function, including my implementation below, relies on static variables, they are not thread safe! Pay extra care here...
#include <stdio.h>
#include <stdlib.h>
/* a dummy recv() implementation that returns the size
* of received buffer */
size_t recv_(int s, void *buf, size_t len, int flags) {
len=0;
char *tmp=buf;
for (; *tmp; ++tmp, ++len)
;
return len;
}
/* Modified version of zstring_strtok
* first call -> zstrint_strtok(char *str, char *delim, int len)
* consecutive calls -> zstrint_strtok(NULL, char *delim,0)
*/
char *zstring_strtok(char *str, const char *delim, int len) {
static char *static_str=0; /* var to store last address */
int index=0, strlength=len; /* integers for indexes */
int found = 0; /* check if delim is found */
/* delimiter cannot be NULL
* if no more char left, return NULL as well
*/
if (delim==0 || (str == 0 && static_str == 0))
return 0;
if (str == 0)
str = static_str;
/* get length of string */
if (len==0)
while(str[strlength])
strlength++;
else
strlength=len;
/* find the first occurance of delim */
for (index=0;index<strlength;index++)
if (str[index]==delim[0]) {
found=1;
break;
}
/* if delim is not contained in str, return str */
if (!found) {
static_str = 0;
return str;
}
/* check for consecutive delimiters
*if first char is delim, return delim
*/
if (str[0]==delim[0]) {
static_str = (str + 1);
return (char *)delim;
}
/* terminate the string
* this assignmetn requires char[], so str has to
* be char[] rather than *char
*/
str[index] = '\0';
/* save the rest of the string */
if ((str + index + 1)!=0)
static_str = (str + index + 1);
else
static_str = 0;
return str;
}
int main()
{
char response[]="GET /file HTTP/1.1";
char *buf;
int recv_msg_size;
buf=malloc(256);
if((recv_msg_size=recv_(0,response,0,0)) > 0){
buf=zstring_strtok(response," ",recv_msg_size);
do{
printf("%s\n",buf);
} while(buf=zstring_strtok(NULL," ",0));
}
return 0;
}
The zstring_strtok()
function is a modified version of zstring_strtok()
in the zString library, a BSD licensed string processing library for C.
the recv_()
function above is a dummy replacement of the recv()
system call, just to make the exmaple code more readible for you. On my system (FreeBSD 10.2-RELEASE), recv(3) is defined as
ssize_t
recv(int s, void *buf, size_t len, int flags);
Output of the above example is
% ./recv
GET
/file
HTTP/1.1