tags:

views:

891

answers:

2

The example code below works as as a server process. But when I add the line

pid_t childpid;

below

struct sockaddr_in servaddr, clientaddr;

it fails at line

connectfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientaddrlen);

with the error code 22, EINVAL - invalid argument. I'm new to sockets in C and I couldn't understand the problem, can you help me about this?

Thanks.

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h> 
#include <linux/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

extern int errno;

int main()
{
    int clientaddrlen, listenfd, connectfd, bytes_rcvd, listen_queue_size=1;
    short int port_no = 2000;
    char buffer[1000];
    struct sockaddr_in servaddr, clientaddr;

    printf("Server running at port #%d\n", port_no);

    // Create server socket.
    if ( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        fprintf(stderr, "Cannot create server socket! errno=%d \n", errno);
        exit(-1);
    }
    printf("Server socket created\n");

    // Bind (attach) this process to the server socket.
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(port_no);
    bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
    printf("Server socket is bound to port #%d\n", port_no);

    // Turn 'listenfd' to a listening socket. Listen queue size is 1.
    listen(listenfd,listen_queue_size);
    printf("Server listening with a queue of size %d. \n", listen_queue_size);

    // Wait for connection(s) from client(s).
    while (1)
    {
        connectfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientaddrlen);
        printf("A client has connected\n");
        if (recv(connectfd, buffer, sizeof(buffer), 0 ) > 0)
            printf("Received message: %s\n", buffer);
        close(connectfd);
        printf("Server closed connection to client\n");
    }

    close(listenfd);
    return 0;
}
+2  A: 

Adding an unused variable declaration should under normal circumstances not cause accept to fail. Select is not broken.

The code you posted cannot behave as you describe; you aren't checking the return value of accept(), so how do you know it fails?. Remember that Unix system/libc calls typically do not set errno unless an error occurs, so unless accept() returns -1, errno can contain anything.

That said; if you've verified that accept() fails, and errno is set to EINVAL, there are two possibilities according to the man page:

  • Socket is not listening for connections. (Did you check the return code from listen()?)
  • Addrlen is invalid (i.e. negative)

EDIT: most importantly: post a complete example which compiles and demonstrates your problem. Otherwise we can only guess as to what the problem is.

JesperE
I checked the errno with another code I wrote, this a simple version of that one. I apologize for the misinformation.listen() returns 0, so it's ok. But clientaddrlen is negative. What can be the cause of that?
newth
Thanks for the direction, I checked the man page of accept() and it says "The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned." I initialized clientaddrlen and problem solved!Still I don't understand why declaring a variable causes accept() to fail. I don't expect a bug, but I want to learn why it fails anyway. Thanks!
newth
As @Neil commented, adding a variable will change the stack layout to clientaddrlen actually becomes initialized to a non-negative value, and thereby accepted by accept(). But it is junk nonetheless.
JesperE
+2  A: 

I don't see where you initialize clientaddrlen. This is an in/out parameter. You must tell accept() how big the buffer for the address is.

Aaron Digulla
Adding the line "clientaddrlen = sizeof( (struct sockaddr *) " did the trick. Thanks!
newth