nitishch
nitishch

Reputation: 130

Weird functioning of read() in socket programming

I have the following server code. I took this from a website to learn socket programming.

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

#define BUFFER_SIZE 356
#define READ_SIZE 255

void error(const char *msg)
{
    std::cerr << msg;
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[BUFFER_SIZE];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if(argc < 2) error("ERROR, no port provided\n");
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) error("ERROR opening socket\n");


     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     while(true){
        newsockfd = accept(sockfd, 
                    (struct sockaddr *) &cli_addr, 
                    &clilen);
        if (newsockfd < 0) 
             error("ERROR on accept");
        bzero(buffer,BUFFER_SIZE);
        n = read(newsockfd,buffer,READ_SIZE);    
        printf("Here is the message: %s\n",buffer);
        std::cout << "hellow" << "\n";
        char *message = "HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Hello. Please don't close</h1></body></html>";
        n = write(newsockfd,message,strlen(message));
        if (n < 0) error("ERROR writing to socket");
        close(newsockfd);
     }
     close(sockfd);
     return 0; 
}

Problem appears to be with the read() function. When I run this server on a particular port and use Firefox as client, the browser reports Connection was reset.

Here is the output of tcpdump while closing the connection:

15:29:44.315802 IP (tos 0x0, ttl 64, id 7572, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.8882 > localhost.36360: Flags [R.], cksum 0xfe28 (incorrect -> 0xebb8), seq 80, ack 291, win 350, options [nop,nop,TS val 3928986 ecr 3928985], length 0

So the server is directly sending RST flag and is not following the FIN/ACK procedure.

However, if I change the value of READ_SIZE from 255 to BUFFER_SIZE-1, the code works fine.

Here is the new trace from tcpdump corresponding to connection closing:

15:30:21.353901 IP (tos 0x0, ttl 64, id 3241, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.8882 > localhost.36437: Flags [F.], cksum 0xfe28 (incorrect -> 0x20b7), seq 80, ack 302, win 350, options [nop,nop,TS val 3938245 ecr 3938245], length 0
15:30:21.354071 IP (tos 0x0, ttl 64, id 38322, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.36437 > localhost.8882: Flags [F.], cksum 0xfe28 (incorrect -> 0x20be), seq 302, ack 81, win 342, options [nop,nop,TS val 3938245 ecr 3938245], length 0
15:30:21.354093 IP (tos 0x0, ttl 64, id 3242, offset 0, flags [DF], proto TCP (6), length 52)
    localhost.8882 > localhost.36437: Flags [.], cksum 0xfe28 (incorrect -> 0x20b6), ack 303, win 350, options [nop,nop,TS val 3938245 ecr 3938245], length 0

Why does read() function cause RST flag to be sent? Why is the problem solved by increasing the amount to be read?

Note: This happens every time. This is not due to some random interruption.

EDIT: As suggested by Aif, I tried calling read() multiple times. This also gives a similar problem. Following is the loop

        while(true){
            std::cout << toread << "\n";
            readed = read(newsockfd, a, std::min(toread, 1));
        //  readed = read(newsockfd, a, 1);
            std::cout << readed << "\n";
            if(readed < 0){std::cout << "error reading"; exit(-1);}
            if(readed == 0) break;
            a += readed;
            toread -= readed;
            if(toread == 0) break;
        }

I'm reading 1 byte everytime. Now, this gives a very strange problem. It reads only 294 bytes and then stops indefinitely. I don't know how the program came up with this number. Shortly, even this repeated read doesn't work. Any ideas?

Upvotes: 0

Views: 226

Answers (1)

Aif
Aif

Reputation: 11220

Edit:

  • Check the return value of read()
  • iterate over read in case you receive more than you are reading, and keep the whole thing in a larger buffer.

Oh and by the way, this is the old way of doing sockets, you should now use getaddrinfo. For a (very very) good tutorial about socket programming, I recommand Beej's guide to skcet programming.

Edit 2:

There is indeed something I don't understand when reducing the buffer size. Anyway, I did some corrections on your code which makes it work.

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>                       // Use errno

#define BUFFER_SIZE 10                   // Very small buffer, to test the behaviour
#define READ_SIZE 255

void error(const char *msg)
{
    std::cerr << msg;
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[BUFFER_SIZE];
     std::string msg;                                // Use a string for the "large" buffer
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if(argc < 2) error("ERROR, no port provided\n");
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) error("ERROR opening socket\n");


     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     while(1){
       int pkt = 0;
       newsockfd = accept(sockfd, 
              (struct sockaddr *) &cli_addr, 
              &clilen);
       if (newsockfd < 0) 
     error("ERROR on accept");
       bzero(buffer,BUFFER_SIZE);
       //n = read(newsockfd,buffer,sizeof(buffer));
       do 
       {
           n = recv(newsockfd, buffer, sizeof(buffer), MSG_DONTWAIT);  // Use recv instead of read
           if (n>0) 
           {
              buffer[n] = 0;
              msg += buffer;                    // Actually increase the "large" buffer
              pkt++;
           }
       } while (n > 0 || ((n == -1) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)) && pkt == 0));

       if (n<0)
       {

          if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
              error("Reading error\n");
          else
              std::cout << "Noting left to read" << std::endl;
       }

       std::cout << "Here is the message: " << msg << std::endl;
       std::cout << "hellow" << "\n";
       char *message = "HTTP/1.1 200 OK\r\n\r\n<html><body><h1>Hello. Please don't close</h1></body></html>";
       n = write(newsockfd,message,strlen(message));
       if (n < 0) error("ERROR writing to socket");
       else std::cout << "Wrote " << n << " bytes" << std::endl;
       close(newsockfd);
     }
     close(sockfd);
     return 0; 
}

The main changes are:

  • I use a string to store the whole client request without any allocation issues.
  • I use recv instead of read to ease the use of non blocking behaviour
  • The loop condition used to be errno = eagain or errno = wouldblock but the default "rst" behaviour forced me to count the number of received packets.

Hope this helps.

Upvotes: 1

Related Questions