0

I just wrote a program that uses local socket to communicate between two processes

if client send one message to server, and then close the connection, server would only receive one message

clent:

   send(srvfd,data,size,0)
   close(srvfd)

server:

   n=recv(fd,buf,size,0)

however, if client send one message, and server also send one message (any string) back to client, then client close the connection, server would receive the older message that client sends

client:

   send(srvfd,data,size,0)
   n=recv(srvfd,buf,size,0)
   close(srvfd)

server:

   n=recv(fd,buf,size,0)
   send(fd,"response",8,0)
   n=recv(fd,buf,size,0)   //receive the first message again

here is my initialize code:

struct sockaddr_un srvAddr;
int listenFd = socket(PF_UNIX, SOCK_STREAM, 0);
if (listenFd < 0) {
    perror("cannot create communication socket");
    throw runtime_error("cannot create communication socket");
}
srvAddr.sun_family = AF_UNIX;
strncpy(srvAddr.sun_path, sockFile.c_str(), sockFile.size());
unlink(sockFile.c_str());

int ret = bind(listenFd, (struct sockaddr*) &srvAddr, sizeof(srvAddr));
if (ret == -1) {
    perror("cannot bind server socket");
    close(listenFd);
    unlink(sockFile.c_str());
    throw runtime_error("cannot bind server socket");
}

ret = listen(listenFd, BACKLOG);
if (ret < 0) {
    perror("cannot listen the client connect request");
    close(listenFd);
    unlink(sockFile.c_str());
    throw runtime_error("cannot listen the client connect request");
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jun
  • 35
  • 1
  • 8
  • It doesn't seem possible that the first message is received again; the connection is closed by then, and the 2nd `recv` should return 0. Do you check for this? – Kenney Sep 17 '15 at 16:29
  • 2
    you failed to clear the input buffer after receiving the first message, so it is still in the buffer, when the second recv() fails. However, the posted code did not check the returned value from recv() so the code does not know it is still looking at the first message – user3629249 Sep 17 '15 at 17:24
  • You don't need to clear buffers. You do need to take notice of return codes. @user3629249 – user207421 Sep 17 '15 at 18:39
  • @EJP, While true that the OP does not need to clear the buffer (and does need to pay attention to the returned values) The question ask why seeing the same data twice. – user3629249 Sep 17 '15 at 19:21
  • @user3629249 And the answer is that he needs to pay attention to the return code. Not that he needs to clear the buffer. I've already said all that. – user207421 Sep 17 '15 at 21:23

1 Answers1

2
send(fd,"response",8,0)
n=recv(fd,buf,size,0)   //receive the first message again

No you didn't. What you got got was n == 0, meaning end of stream. It also means that zero bytes were transferred into the buffer, so none of what's now in the buffer is now meaningful.

Don't ignore return codes.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I checked the return code, if n<=0, it would close the fd – Jun Sep 18 '15 at 00:52
  • @Jun And not use the contents of the buffer? If you're claiming that TCP delivers data twice, you're mistaken. If you're claiming your code is perfect and you've really observed what you claim, you're also mistaken. – user207421 Sep 18 '15 at 09:48