after I search in how to debug my code and try gdb I find that there
is an awesome tools "UNDER LINUX" called
ltrace and strace it is very good to detect run time actions of any program

but After the program runs well firt time
it give error in bind() in the second time I ran  the same program

THE SERVER CODE :
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netdb.h>

#define SERVER_ERROR(msg,exit_code) \
        do { fprintf(stderr,msg);exit(exit_code);}while(0)
#define BUF_SIZE 500

// This code will represent a simple server that can response to any
http request and load simple module called "MYMOD"
// This will take two args $ $PROGRAM_NAME SOCKET_NUMBER
int main( int argc, char* argv[])
{
        int sfd;
        char msg[100];
        struct addrinfo hints,*res;     

        if ( argc != 2){
                sprintf(msg,"USAGE: $%s PortNumber \n",argv[0]);
                SERVER_ERROR(msg,100);
        }
        // Create addr info data to use in socket() Creation
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = 0;
        if ( getaddrinfo("localhost", "1111" ,&hints ,&res) )
                SERVER_ERROR("getaddrinfo",99);
        if ( (sfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) 
== -1 )
                SERVER_ERROR("socket()",101);
        if ( bind(sfd, res->ai_addr, res->ai_addrlen) )
                SERVER_ERROR("bind()",102);
        freeaddrinfo(res);
        if ( listen(sfd ,5) )
                SERVER_ERROR("listen()",103);
        
        struct sockaddr_in cs;
        socklen_t slen;
        puts("WE ARE LISTENING NOW .....");
        if (accept(sfd ,&cs ,&slen) == -1)
                SERVER_ERROR("accept()",104);
        puts("Client trying to connect  now ...");
        close(sfd);
        return 0;
}


it run in the first time good but in second it through bind() exeption

why ????

Reply via email to