Read Blocking in C

0

I am trying to make a server in C. After receiving the client connection the purpose is to wait for client information on the socket, for this I use the following code.

/*Read Messages*/
        close(fd);
        nread = read(fd, msg, BUFFERSIZE-1);
        close(fd);
        msg[nread] = '
/*Read Messages*/
        close(fd);
        nread = read(fd, msg, BUFFERSIZE-1);
        close(fd);
        msg[nread] = '%pre%';
        printf("%s\n", msg);
'; printf("%s\n", msg);

The close(fd) function will close / clear the socket before reading. But the problem happens in reading, read() . My question is if the close(fd) are set correctly and if this is the consequence of read read garbage.

    
asked by anonymous 24.03.2014 / 23:22

2 answers

0

When the server accepts a client, it creates a process or thread that is in charge of working with that client. Before delivering work to this process, we close the fd of the client. For example:

while (1) {
     client = accept(fd,(struct sockaddr *)&client_addr,&client_addr_size);
     if (client > 0) {
        if (fork() == 0) {
            close(fd);
            process_client(client);
            exit(0);
        }
    close(client);
    }
}

The process_client function will be the worker of this process. In other words, the function is in charge of waiting for information on the socket.

void process_client(int client_fd)
{
    int nread = 0;
    char buffer[BUF_SIZE];
    nread = read(client_fd, buffer, BUF_SIZE-1);
    buffer[nread] = '
while (1) {
     client = accept(fd,(struct sockaddr *)&client_addr,&client_addr_size);
     if (client > 0) {
        if (fork() == 0) {
            close(fd);
            process_client(client);
            exit(0);
        }
    close(client);
    }
}
'; printf("%s", buffer); fflush(stdout); close(client_fd); }

If the purpose is to always be waiting for information on the socket coming from the client, just put the whole function in a While(1)

    
24.03.2014 / 23:58
4
  

This statement is not always true:

A socket may or may not be "blocking", it merely depends on whether you set this property in the stream.

What may be happening, in your case, is the read is blocking, but upon receiving new data, it usually returns the value and exits the lock state (this is expected).

Try using something like the following code to evaluate what's going on in your stream to see if the correct data is coming in, and what you're getting is control data and / or special characters (remember by some criterion in the code to exit the infinite loop, if you can not break the code;)).

while ((numread = read(sock_fd, &buff, 1)) > 0) {
   write(out_fd, &buff, 1);
}

Here's a function to configure blocking behavior in both linux and windows:

#import <fcntl.h>

/** Retorna true se funcionou, ou false se houve algum erro
  * fd é o file descriptor em que for aplicar o parâmetro
  * blocking é true ou false, definindo se o socket vai ser "bloqueante" ou não
  */
bool SetSocketBlockingEnabled(int fd, bool blocking)
{
   if (fd < 0) return false;

#ifdef WIN32
   unsigned long mode = blocking ? 0 : 1;
   return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
#else
   int flags = fcntl(fd, F_GETFL, 0);
   if (flags < 0) return false;
   flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
   return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
#endif
}

Original code in this SOen response

    
24.03.2014 / 23:29