0

I have a very simple client and server code:

client

int sockfd = 0;
    int bytesReceived = 0;
    char recvBuff[BUFSIZE];
    memset(recvBuff, '0', sizeof(recvBuff));
    struct sockaddr_in serv_addr;
    printf("here i am\n");
    /* Create a socket first */
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0){
        printf("\n Error : Could not create socket \n");
        return 1;
    }
    fprintf(stderr, "connection stats\n");
    fprintf(stderr, "connecting to %d - %s \n",port,server);
    /* Initialize sockaddr_in data structure */
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port); // port
    serv_addr.sin_addr.s_addr = server;
    printf(" attempting connection %s:%d\n", server, port);
    /* Attempt a connection */
     int con =  connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
     printf("Error %d\n", con);
    exit(-1);

And server

   int listenfd = 0;
    int connfd = 0;
    struct sockaddr_in serv_addr;
    char sendBuff[BUFSIZE];
    int numrv;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);

    printf("Socket retrieve success\n");

    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff));
    printf("here 2\n");
    serv_addr.sin_family = AF_INET;
    printf("here 3\n");
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    printf("here 4\n");
    serv_addr.sin_port = htons(port);
    printf("here 5\n");
    int optval = 1;
    printf(" opening port %d\n", port);
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
               (const void *) &optval, sizeof(int));
    bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    printf("binded\n");


    while (1) {
        connfd = accept(listenfd, (struct sockaddr *) NULL, NULL);
        printf("connfd %d", connfd);


        close(connfd);
        sleep(1);


    }

Client output:

here i am
connecting to 8888 - localhost 
 attempting connection localhost:8888

Server output

Socket retrieve success
here 2
here 3
here 4
here 5
 opening port 8888
binded

And after like few minutes of wait.. I observe the following: Client:

here i am
connecting to 8888 - localhost 
 attempting connection localhost:8888
Error -1

Server:

Socket retrieve success
here 2
here 3
here 4
here 5
 opening port 8888
binded
connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd 

which by -1, I can guess that the connection has failed. I am not sure what I am missing?

8
  • You should put a \n after connfd %d, or else printf's output will be buffered and you won't see it right when it happens. You could also call fflush to print buffered output Commented May 26, 2015 at 8:22
  • @Eregrith: You are correct.. so basically, the connect fails instantaneously.. But I am not sure.. why server is not accepting client connection.. Commented May 26, 2015 at 8:28
  • Have a look at errno and perror maybe that can help, cross it with the connect() manual Commented May 26, 2015 at 8:32
  • In theory connect should block, but this depends on your environment. Try not exiting from the client immediately, but send some data and wait some time (100ms) in a loop. Commented May 26, 2015 at 8:32
  • @vbence you can't send some data if connect returns -1. Commented May 26, 2015 at 8:33

1 Answer 1

2

The server misses to call listen() on the bound socket prior to calling accept() on it.


Also this

serv_addr.sin_addr.s_addr = server;

should have issued a warning by the compiler. You cannot assign a C-"string" (what I assume server is) to an in_addr_t (what serv_addr.sin_addr.s_addr is).

Fix this by doing:

serv_addr.sin_addr.s_addr = inet_addr(server);

or better add error checking to this, like so:

{
  in_addr_t in_addr = inet_addr(server);
  if (INADDR_NONE == in_addr)
  {
    perror("inet_addr() failed");
    abort(); /* or whatever error handling you choose. */
  }

  serv_addr.sin_addr.s_addr = in_addr;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yepp.. that did solves.. so now the server is waiting for incoming connections.. but client is not able to connect to it.. am i missing anything on my client code?
Ok.. So I am able to make this work.. Instead of localhost, if I have serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); Then it works fine? But I actually have string localhost? Is there a way to initialize localhost..instead of 127.0.0.1
I get PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.011 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.018 ms But when I use localhost instead of 127.0.0.1, the client connection fails (returns -1)
@Fraz: You need to go via getaddrinfo() to pull the ip-address for a symbolic name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.