-2

server side child func: recv client data,then shutdown the socket

server side child func:
                char line[1024];
                bzero(line,sizeof(line));
                int ret =  recv(fd,line,sizeof(line),0);
                if(ret > 0){
                        cout << line << endl;
                }else if(ret < 0){
                        cout << "recv error" << endl;
                }else if(ret == 0){
                        cout << "client close" << endl;
                        break;
                }
                shutdown(fd,SHUT_WR);

client side main func

char line[] = "ds2d2d2d2d21dwq";
        send(sockfd,line,sizeof(line),0); //send to server
        //server side the child func has exit
        sleep(20);
        cout << "write to server" << endl;
        //write to server again
        ret = send(sockfd,line,sizeof(line),0);
        perror("write...."); //write success
        //according to unp book ,the server has send the RST msg to client
        bzero(line,sizeof(line));
        sleep(5);
        //recv should return error and the error code should ECONNRESET 
        //but not appear , the recv success return 0(EOF)
        //then execute recv success return 0(EOF)
        ret = recv(sockfd,line,sizeof(line),0);
        cout << ret <<line << endl;
        perror("recv....");
        ret = recv(sockfd,line,sizeof(line),0);
        perror("recv....");

I don't know Is it right? Kernel improvements. I found the recv system don't have ECONNRESET error code

TResponse
  • 3,940
  • 7
  • 43
  • 63

2 Answers2

2

From the "man recv" page:

RETURN VALUES

  These calls return the number of bytes received, or -1 if an error
  occurred.

  For TCP sockets, the return value 0 means the peer has closed its half
  side of the connection.

So when the server closes the TCP connection, it's expected that recv() on the client will start returning zero (only after it has finished returning any data that was send()'d by the server, of course)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • thanks but i have another question why the read/recv system call man doc don't have the ECONNRESET error code? the unp book have the error code – zhangyifei Jan 27 '15 at 05:39
0

There is nothing here that will provoke a reset. What you should get in the client is a zero return value from recv(), after you've read all the data. Your expectation is incorrect.

Other issues:

  • What does 'kernel improvements' mean?
  • You are calling perror() in the client when you don't know whether there has been an error or not. You're only supposed to call it directly after after a system call has returned -1.
  • You aren't calling it in the server when you do know that.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • @downvoter Please explain to us what it is in the OP's code that will provoke a reset, or what other issue you have with this answer. – user207421 Jan 26 '15 at 06:29
  • It's a bad question description i admit, but don't complain in the answer area. vote down the question if you like. If you want to answer, answer something that is meaningful. – D3Hunter Jan 26 '15 at 06:38
  • @juji I don't know what you mean. If the question isn't meaningful why did you answer it yourself? And if you're claiming that this answer isn't meaningful, or consists entirely of 'complaints', you're mistaken. Read the first paragraph. – user207421 Jan 26 '15 at 06:41
  • I didn't say `the question isn't meaningful`, i say it has `a bad question description`. – D3Hunter Jan 26 '15 at 06:44
  • @juji So what exactly *is* your objection to this answer? You said something incomprehensible about 'answer something that is meaningful' that I'm trying to unpick. What *does* it mean? – user207421 Jan 26 '15 at 07:12