client not connecting with server C (Socket programming)

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 amn");
    /* 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 statsn");
    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:%dn", server, port);
    /* Attempt a connection */
     int con =  connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
     printf("Error %dn", 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 successn");

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


    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?


Source: c#

Leave a Reply