Not able to get client address - Unix

This is a discussion on Not able to get client address - Unix ; #include "header.h" void catcher(int sig); int newsockfd; int main(void) { int sockfd, option; int validNo; int fd; /*Declaring structure for communication*/ struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY}; struct sockaddr addr; socklen_t lengthptr; char ipaddress[20]; static struct sigaction act; act.sa_handler ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: Not able to get client address

  1. Not able to get client address

    #include "header.h"

    void catcher(int sig);
    int newsockfd;

    int main(void)
    {
    int sockfd, option; int validNo;
    int fd;
    /*Declaring structure for communication*/
    struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    struct sockaddr addr;
    socklen_t lengthptr;
    char ipaddress[20];
    static struct sigaction act;
    act.sa_handler = catcher;
    sigfillset(&(act.sa_mask));

    sigaction(SIGPIPE, &act, NULL);
    fd = open("./log",O_RDWR |O_CREAT , 0644);

    /* set up the transport end point */

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
    perror("socket call failed");
    exit(1);
    }

    /* bind an address to the end point */
    if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    {
    perror("bind call failed");
    exit(1);
    }

    /* start listening for incoming connections */

    if ( listen(sockfd, 5) == -1 )
    {
    perror("listen call failed");
    exit(1) ;
    }



    /* accept a connection */
    if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    {
    perror("accept call failed");

    }
    getpeername (newsockfd, &addr, &lengthptr );
    sprintf(ipaddress, "%s", inet_ntoa(addr));
    printf("IPADDRESS : %s\n", ipaddress);



    }

    void catcher(int sig)
    {
    close(newsockfd);
    exit (0);
    }


    The output of this program comes as 0.0.0.0. I am not able to get IP
    ADDRESS of my peer which is connected to me!!

  2. Re: Not able to get client address

    On Apr 15, 9:23 pm, Sanchit wrote:
    > #include "header.h"
    >
    > void catcher(int sig);
    > int newsockfd;
    >
    > int main(void)
    > {
    > int sockfd, option; int validNo;
    > int fd;
    > /*Declaring structure for communication*/
    > struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    > struct sockaddr addr;
    > socklen_t lengthptr;
    > char ipaddress[20];
    > static struct sigaction act;
    > act.sa_handler = catcher;
    > sigfillset(&(act.sa_mask));
    >
    > sigaction(SIGPIPE, &act, NULL);
    > fd = open("./log",O_RDWR |O_CREAT , 0644);
    >
    > /* set up the transport end point */
    >
    > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    > {
    > perror("socket call failed");
    > exit(1);
    > }
    >
    > /* bind an address to the end point */
    > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    > {
    > perror("bind call failed");
    > exit(1);
    > }
    >
    > /* start listening for incoming connections */
    >
    > if ( listen(sockfd, 5) == -1 )
    > {
    > perror("listen call failed");
    > exit(1) ;
    > }
    >
    > /* accept a connection */
    > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > {
    > perror("accept call failed");
    >
    > }
    > getpeername (newsockfd, &addr, &lengthptr );
    > sprintf(ipaddress, "%s", inet_ntoa(addr));
    > printf("IPADDRESS : %s\n", ipaddress);
    >
    > }
    >
    > void catcher(int sig)
    > {
    > close(newsockfd);
    > exit (0);
    >
    > }
    >
    > The output of this program comes as 0.0.0.0. I am not able to get IP
    > ADDRESS of my peer which is connected to me!!


    here header.h is

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

  3. Re: Not able to get client address

    Sanchit writes:

    [...]

    > socklen_t lengthptr;


    [no mentionig of 'length pointer']


    > /* accept a connection */
    > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > {
    > perror("accept call failed");
    >
    > }
    > getpeername (newsockfd, &addr, &lengthptr );


    Consider reading the documentation next time. The third argument to
    getpeername must initially contain the amount of space available at
    the address passed as second argument.

  4. Re: Not able to get client address

    On Apr 15, 9:23 pm, Sanchit wrote:
    > #include "header.h"
    >
    > void catcher(int sig);
    > int newsockfd;
    >
    > int main(void)
    > {
    > int sockfd, option; int validNo;
    > int fd;
    > /*Declaring structure for communication*/
    > struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    > struct sockaddr addr;
    > socklen_t lengthptr;
    > char ipaddress[20];
    > static struct sigaction act;
    > act.sa_handler = catcher;
    > sigfillset(&(act.sa_mask));
    >
    > sigaction(SIGPIPE, &act, NULL);
    > fd = open("./log",O_RDWR |O_CREAT , 0644);
    >
    > /* set up the transport end point */
    >
    > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    > {
    > perror("socket call failed");
    > exit(1);
    > }
    >
    > /* bind an address to the end point */
    > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    > {
    > perror("bind call failed");
    > exit(1);
    > }
    >
    > /* start listening for incoming connections */
    >
    > if ( listen(sockfd, 5) == -1 )
    > {
    > perror("listen call failed");
    > exit(1) ;
    > }
    >
    > /* accept a connection */
    > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > {
    > perror("accept call failed");
    >
    > }
    > getpeername (newsockfd, &addr, &lengthptr );
    > sprintf(ipaddress, "%s", inet_ntoa(addr));
    > printf("IPADDRESS : %s\n", ipaddress);
    >
    > }
    >
    > void catcher(int sig)
    > {
    > close(newsockfd);
    > exit (0);
    >
    > }
    >
    > The output of this program comes as 0.0.0.0. I am not able to get IP
    > ADDRESS of my peer which is connected to me!!



    I Got the Answer

  5. Re: Not able to get client address

    Sorry.. There is lot of confusion..

    I am again posting the program

    #include "header.h"

    void catcher(int sig);
    int newsockfd;

    int main(void)
    {
    int sockfd, option; int validNo;
    int fd;
    /*Declaring structure for communication*/
    struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    struct sockaddr_in addr;
    socklen_t lengthptr;
    char ipaddress[20];
    static struct sigaction act;
    act.sa_handler = catcher;
    sigfillset(&(act.sa_mask));

    sigaction(SIGPIPE, &act, NULL);

    /* set up the transport end point */

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
    perror("socket call failed");
    exit(1);
    }

    /* bind an address to the end point */
    if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    {
    perror("bind call failed");
    exit(1);
    }

    /* start listening for incoming connections */

    if ( listen(sockfd, 5) == -1 )
    {
    perror("listen call failed");
    exit(1) ;
    }

    while(1)
    {
    /* accept a connection */
    if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    {
    perror("accept call failed");

    }

    addr.sin_addr.s_addr=0;
    if(getpeername (newsockfd, &addr, &lengthptr )==0);
    {sprintf(ipaddress, "%s", inet_ntoa(addr.sin_addr));
    printf("IPADDRESS : %s\n", ipaddress);}


    }
    }

    void catcher(int sig)
    {
    close(newsockfd);
    exit (0);
    }


    OUTPUT
    First time output is always 0.0.0.0 else it is giving correct IP
    ADDRESS

  6. Re: Not able to get client address

    Sanchit writes:

    [...]

    > OUTPUT
    > First time output is always 0.0.0.0 else it is giving correct IP
    > ADDRESS


    After the first call, the variable you didn't initially set as you
    should have done will contain the correct value, which is generated by
    getpeername as an output.

    BTW, if this aversion of yours regarding the extremly fine manual
    continues, you'd be better off spending your time on something other
    than 'programming for UNIX(*)-systems' ...

  7. Re: Not able to get client address

    In article
    <29e2244f-df2a-4989-bc2d-5f6375c6d887@s33g2000pri.googlegroups.com>,
    Sanchit wrote:

    > Sorry.. There is lot of confusion..
    >
    > I am again posting the program


    Why? Have you changed anything?

    Anyway, you need to initialize lengthptr before calling getpeername():

    lengthptr = sizeof addr;

    BTW, why do you call this lengthptr when it's not a pointer?

    >
    > #include "header.h"
    >
    > void catcher(int sig);
    > int newsockfd;
    >
    > int main(void)
    > {
    > int sockfd, option; int validNo;
    > int fd;
    > /*Declaring structure for communication*/
    > struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    > struct sockaddr_in addr;
    > socklen_t lengthptr;
    > char ipaddress[20];
    > static struct sigaction act;
    > act.sa_handler = catcher;
    > sigfillset(&(act.sa_mask));
    >
    > sigaction(SIGPIPE, &act, NULL);
    >
    > /* set up the transport end point */
    >
    > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    > {
    > perror("socket call failed");
    > exit(1);
    > }
    >
    > /* bind an address to the end point */
    > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    > {
    > perror("bind call failed");
    > exit(1);
    > }
    >
    > /* start listening for incoming connections */
    >
    > if ( listen(sockfd, 5) == -1 )
    > {
    > perror("listen call failed");
    > exit(1) ;
    > }
    >
    > while(1)
    > {
    > /* accept a connection */
    > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > {
    > perror("accept call failed");
    >
    > }
    >
    > addr.sin_addr.s_addr=0;
    > if(getpeername (newsockfd, &addr, &lengthptr )==0);
    > {sprintf(ipaddress, "%s", inet_ntoa(addr.sin_addr));
    > printf("IPADDRESS : %s\n", ipaddress);}
    >
    >
    > }
    > }
    >
    > void catcher(int sig)
    > {
    > close(newsockfd);
    > exit (0);
    > }
    >
    >
    > OUTPUT
    > First time output is always 0.0.0.0 else it is giving correct IP
    > ADDRESS


    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  8. Re: Not able to get client address

    On Apr 16, 1:22 am, Barry Margolin wrote:
    > In article
    > <29e2244f-df2a-4989-bc2d-5f6375c6d...@s33g2000pri.googlegroups.com>,
    >
    > Sanchit wrote:
    > > Sorry.. There is lot of confusion..

    >
    > > I am again posting the program

    >
    > Why? Have you changed anything?
    >
    > Anyway, you need to initialize lengthptr before calling getpeername():
    >
    > lengthptr = sizeof addr;
    >
    > BTW, why do you call this lengthptr when it's not a pointer?
    >
    >
    >
    >
    >
    > > #include "header.h"

    >
    > > void catcher(int sig);
    > > int newsockfd;

    >
    > > int main(void)
    > > {
    > > int sockfd, option; int validNo;
    > > int fd;
    > > /*Declaring structure for communication*/
    > > struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    > > struct sockaddr_in addr;
    > > socklen_t lengthptr;
    > > char ipaddress[20];
    > > static struct sigaction act;
    > > act.sa_handler = catcher;
    > > sigfillset(&(act.sa_mask));

    >
    > > sigaction(SIGPIPE, &act, NULL);

    >
    > > /* set up the transport end point */

    >
    > > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    > > {
    > > perror("socket call failed");
    > > exit(1);
    > > }

    >
    > > /* bind an address to the end point */
    > > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    > > {
    > > perror("bind call failed");
    > > exit(1);
    > > }

    >
    > > /* start listening for incoming connections */

    >
    > > if ( listen(sockfd, 5) == -1 )
    > > {
    > > perror("listen call failed");
    > > exit(1) ;
    > > }

    >
    > > while(1)
    > > {
    > > /* accept a connection */
    > > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > > {
    > > perror("accept call failed");

    >
    > > }

    >
    > > addr.sin_addr.s_addr=0;
    > > if(getpeername (newsockfd, &addr, &lengthptr )==0);
    > > {sprintf(ipaddress, "%s", inet_ntoa(addr.sin_addr));
    > > printf("IPADDRESS : %s\n", ipaddress);}

    >
    > > }
    > > }

    >
    > > void catcher(int sig)
    > > {
    > > close(newsockfd);
    > > exit (0);
    > > }

    >
    > > OUTPUT
    > > First time output is always 0.0.0.0 else it is giving correct IP
    > > ADDRESS

    >
    > --
    > Barry Margolin, bar...@alum.mit.edu
    > Arlington, MA
    > *** PLEASE post questions in newsgroups, not directly to me ***
    > *** PLEASE don't copy me on replies, I'll read them in the group ***


    Barry is correct.
    And you need to use inet_ntoa (&sin.sin_addr)

    only my 2 cents

  9. Re: Not able to get client address

    On Apr 17, 12:12 am, "skylaz...@gmail.com"
    wrote:
    > On Apr 16, 1:22 am, Barry Margolin wrote:
    >
    >
    >
    > > In article
    > > <29e2244f-df2a-4989-bc2d-5f6375c6d...@s33g2000pri.googlegroups.com>,

    >
    > > Sanchit wrote:
    > > > Sorry.. There is lot of confusion..

    >
    > > > I am again posting the program

    >
    > > Why? Have you changed anything?

    >
    > > Anyway, you need to initialize lengthptr before calling getpeername():

    >
    > > lengthptr = sizeof addr;

    >
    > > BTW, why do you call this lengthptr when it's not a pointer?

    >
    > > > #include "header.h"

    >
    > > > void catcher(int sig);
    > > > int newsockfd;

    >
    > > > int main(void)
    > > > {
    > > > int sockfd, option; int validNo;
    > > > int fd;
    > > > /*Declaring structure for communication*/
    > > > struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
    > > > struct sockaddr_in addr;
    > > > socklen_t lengthptr;
    > > > char ipaddress[20];
    > > > static struct sigaction act;
    > > > act.sa_handler = catcher;
    > > > sigfillset(&(act.sa_mask));

    >
    > > > sigaction(SIGPIPE, &act, NULL);

    >
    > > > /* set up the transport end point */

    >
    > > > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    > > > {
    > > > perror("socket call failed");
    > > > exit(1);
    > > > }

    >
    > > > /* bind an address to the end point */
    > > > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
    > > > {
    > > > perror("bind call failed");
    > > > exit(1);
    > > > }

    >
    > > > /* start listening for incoming connections */

    >
    > > > if ( listen(sockfd, 5) == -1 )
    > > > {
    > > > perror("listen call failed");
    > > > exit(1) ;
    > > > }

    >
    > > > while(1)
    > > > {
    > > > /* accept a connection */
    > > > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
    > > > {
    > > > perror("accept call failed");

    >
    > > > }

    >
    > > > addr.sin_addr.s_addr=0;
    > > > if(getpeername (newsockfd, &addr, &lengthptr )==0);
    > > > {sprintf(ipaddress, "%s", inet_ntoa(addr.sin_addr));
    > > > printf("IPADDRESS : %s\n", ipaddress);}

    >
    > > > }
    > > > }

    >
    > > > void catcher(int sig)
    > > > {
    > > > close(newsockfd);
    > > > exit (0);
    > > > }

    >
    > > > OUTPUT
    > > > First time output is always 0.0.0.0 else it is giving correct IP
    > > > ADDRESS

    >
    > > --
    > > Barry Margolin, bar...@alum.mit.edu
    > > Arlington, MA
    > > *** PLEASE post questions in newsgroups, not directly to me ***
    > > *** PLEASE don't copy me on replies, I'll read them in the group ***

    >
    > Barry is correct.
    > And you need to use inet_ntoa (&sin.sin_addr)
    >
    > only my 2 cents


    Sorry, inet_ntoa (sin.sin_addr);


+ Reply to Thread