user800799
user800799

Reputation: 3013

read handle error

            int dataLength;
            int readLength = read(conn->getFd(), &dataLength, HEADER_SIZE);
            printf("Header read %d \n", readLength);

            if(readLength == 0){
                removeClient(conn);
                close(conn->getFd());
            }
            else {
                /*
                 * Do not parse anything here.
                 * Pass the raw data to the work handler.
                 */
                char *buf = new char[dataLength+1];
                int dataReadLength = read(conn->getFd(), buf, dataLength);
                printf("Data read %d \n", dataReadLength);

                printf("ServerManager::eventAcceptLoop, A message has been received \n");
                addWork(conn->getFd(), buf, dataReadLength);

I am working on a network programming and I have this code. It works fine in localhost (no error occur) but I am sure it is going to blow up if I try this on real world network. The problem is that I am not an expert of network programming and not sure which error handle I am supposed to add. It was also had to find a good example or tutorial including specific error handling.

I will be very appreciated if you guys can provide me a good example or tutorials that might help to get the error handling.

Thanks in advance.

Upvotes: 1

Views: 142

Answers (1)

Vinicius Kamakura
Vinicius Kamakura

Reputation: 7778

You need to analyze the case where read() returns -1.

Check this tutorial/reference material: Beej's Guide to Network Programming It's my go-to reference material for network programming.

Upvotes: 1

Related Questions