I am aware that TCP sockets are stream based and a single write may not send all the data. Is this the case with recv as well ?
I am in process of deciding a protocol to handle communication. I wanted some tips as to handle transactions. The data sent / received would be fixed length. Hence i am toying with the idea of discarding messages on receive do not conform to a length. The data sent is less then 100 bytes , so i am not sure how many times will i encounter a scenario where the send fails to fire it in one go or receive fails to read in one go.
Any other approaches to send /receive data ?
Since multiple receives are possible (via select call) I am not sure how to handle incomplete data from two more different clients. ( perhaps via some sort of buffer created per connection)
More importantly , if i have about 2-3 sockets from which i am receiving data , is the conventional approach to block till the complete packet(i.e. message) from one is received ?
PS : These are unix domain sockets for IPC , so no flow over n/w
How much data of the request gets sent is under the control of the kernel. Usually for anything under ~1000 bytes you can send( ) and recv( ) everything in one go.
One way -
set socket to NON-BLOCKING, sample the input with MSG_PEEK.
int sample(int s, char *msg)
{
int len=100;
int nbytes=0;
memset(msg, 0x0, 100);
nbytes= recv(s, msg, len, MSG_PEEK);
if(nbytes<0 && errno== EWOULDBLOCK)
return EWOULDBLOCK * -1;
else
return nbytes;
}
This does not have error handling - note. msg now has what recv probably thinks is a message. Go back and recv() nbytes.
Good practice in something like this is to have your own protocol "header"
like
00098DT001234
00098 is length of the packet including your header
DT might mean it is data. HS might mean a heartbeat. Whatever.
The last number is a sequence that goes from 000000 -> 999999, and rolls over.
This way you can keep track of separate packets. So all you would do is sample
13 bytes, and then you know how many bytes to do a full recv() on. And you would know what to expect in the data part of the packet if any.
int main(void)
{
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
ofstream outfile("/root/log.txt");
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0)
{
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
int s, s2, len;
socklen_t t;
struct sockaddr_un local, remote;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(s, (struct sockaddr *)&local, len) == -1)
{
perror("bind");
exit(1);
}
if (listen(s, 5) == -1)
{
perror("listen");
exit(1);
}
pid_t pidf,pidc;
int stat=0;
int execReturn =0;
int n;
char str[100];
vector<int> pidV;
while (1)
{
stat =0;
execReturn=0;
s2 = accept(s, (struct sockaddr *)&remote, &t);
if(s2== -1)
{
perror("accept");
outfile<<strerror(errno)<<endl;
exit(1);
}
pidc = fork();
if(pidc!=0)
pidV.push_back(pidc);
if(pidc == 0)
{
close(s);
n = recv(s2, str, 100, 0);
str[n]='\0';
outfile<<"Message is "<<str;
pidf = fork();
if (pidf == 0)
{
close(s2);
execReturn =execl("/root/cpp/test.sh","test.sh",NULL);
outfile<<"Errror"<<endl;
_exit(-1);
}
else if (pidf > 0)
{
int id = waitpid(pidf,&stat,0);
int sat = WEXITSTATUS(stat);
outfile<<"status is "<<sat<<"pid is "<<pidf<<endl;
char stg[32];
sprintf(stg,"%d",sat);
send(s2,stg,strlen(stg), 0);
close(s2);
_exit(0);
}
}
else if(pidc > 0)
{
close(s2);
//REAP ALL CHILDREN
/*
vector<int>::iterator beg = pidV.begin();
while(beg!=pidV.end())
{
int id = waitpid(*beg,&stat,WNOHANG);
if(id!=0)
{
beg = pidV.erase(beg);
}
else
{
++beg;
}
}
*/
sleep(5); /* wait 30 seconds */
}
exit(EXIT_SUCCESS);
}
This is the complete code. perhaps exec is closing my listen socket descriptor. The failure is on accept call with bad file descriptor.... I am not touching/closing the listen socket any where... this is driving me crazy!
If i do not reap the child processes, it is fine... with zombies though..... but if they are reaped... the listen socket goes boom!