I have a project for the university where I have to implement an Iterative server.
The server uses a protocol given by the professor and in a few words the client has to send a message in a specific form and my server has to make some parsing in order to save some data in a global struct.
I use the function read()
in order to receive the message and store it in a char array with fixed size. My problem is that the message some times might be bigger than the size of the buffer I use to store it. NOTE: I am not able to send the size of the message first in order to bypass this problem.
I would like to know if there is a way to make this happen.
Bellow is some of the code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "keyvalue.h"
#include <errno.h>
int main ( int argc, char *argv[])
{
char keyvaluebuffer[4096];
int num;
//socket()
//bind()
//listen()
while(1)
{
//accept()
while((num = read(acceptfd, keyvaluebuffer, 4096) ) > 0 )
{
//At this point I use the keyvaluebuffer to parse it and store the data.
}//while (read > 0) end
if (num < 0){close(accept);}
close(accept);
}//while(1) end
close(socket);
}//main end