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!!
Re: Not able to get client address
On Apr 15, 9:23 pm, Sanchit <sanchitgupt...@gmail.com> wrote:[color=blue]
> #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!![/color]
here header.h is
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <arpa/inet.h>
Re: Not able to get client address
Sanchit <sanchitgupta.1@gmail.com> writes:
[...]
[color=blue]
> socklen_t lengthptr;[/color]
[no mentionig of 'length pointer']
[color=blue]
> /* accept a connection */
> if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
> {
> perror("accept call failed");
>
> }
> getpeername (newsockfd, &addr, &lengthptr );[/color]
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.
Re: Not able to get client address
On Apr 15, 9:23 pm, Sanchit <sanchitgupt...@gmail.com> wrote:[color=blue]
> #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!![/color]
I Got the Answer
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
Re: Not able to get client address
Sanchit <sanchitgupta.1@gmail.com> writes:
[...]
[color=blue]
> OUTPUT
> First time output is always 0.0.0.0 else it is giving correct IP
> ADDRESS[/color]
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' ...
Re: Not able to get client address
In article
<29e2244f-df2a-4989-bc2d-5f6375c6d887@s33g2000pri.googlegroups.com>,
Sanchit <sanchitgupta.1@gmail.com> wrote:
[color=blue]
> Sorry.. There is lot of confusion..
>
> I am again posting the program[/color]
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?
[color=blue]
>
> #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[/color]
--
Barry Margolin, [email]barmar@alum.mit.edu[/email]
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 ***
Re: Not able to get client address
On Apr 16, 1:22 am, Barry Margolin <bar...@alum.mit.edu> wrote:[color=blue]
> In article
> <29e2244f-df2a-4989-bc2d-5f6375c6d...@s33g2000pri.googlegroups.com>,
>
> Sanchit <sanchitgupt...@gmail.com> wrote:[color=green]
> > Sorry.. There is lot of confusion..[/color]
>[color=green]
> > I am again posting the program[/color]
>
> 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?
>
>
>
>
>[color=green]
> > #include "header.h"[/color]
>[color=green]
> > void catcher(int sig);
> > int newsockfd;[/color]
>[color=green]
> > 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));[/color]
>[color=green]
> > sigaction(SIGPIPE, &act, NULL);[/color]
>[color=green]
> > /* set up the transport end point */[/color]
>[color=green]
> > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
> > {
> > perror("socket call failed");
> > exit(1);
> > }[/color]
>[color=green]
> > /* bind an address to the end point */
> > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
> > {
> > perror("bind call failed");
> > exit(1);
> > }[/color]
>[color=green]
> > /* start listening for incoming connections */[/color]
>[color=green]
> > if ( listen(sockfd, 5) == -1 )
> > {
> > perror("listen call failed");
> > exit(1) ;
> > }[/color]
>[color=green]
> > while(1)
> > {
> > /* accept a connection */
> > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
> > {
> > perror("accept call failed");[/color]
>[color=green]
> > }[/color]
>[color=green]
> > 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);}[/color]
>[color=green]
> > }
> > }[/color]
>[color=green]
> > void catcher(int sig)
> > {
> > close(newsockfd);
> > exit (0);
> > }[/color]
>[color=green]
> > OUTPUT
> > First time output is always 0.0.0.0 else it is giving correct IP
> > ADDRESS[/color]
>
> --
> 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 ***[/color]
Barry is correct.
And you need to use inet_ntoa (&sin.sin_addr)
only my 2 cents
Re: Not able to get client address
On Apr 17, 12:12 am, "skylaz...@gmail.com" <skylaz...@gmail.com>
wrote:[color=blue]
> On Apr 16, 1:22 am, Barry Margolin <bar...@alum.mit.edu> wrote:
>
>
>[color=green]
> > In article
> > <29e2244f-df2a-4989-bc2d-5f6375c6d...@s33g2000pri.googlegroups.com>,[/color]
>[color=green]
> > Sanchit <sanchitgupt...@gmail.com> wrote:[color=darkred]
> > > Sorry.. There is lot of confusion..[/color][/color]
>[color=green][color=darkred]
> > > I am again posting the program[/color][/color]
>[color=green]
> > Why? Have you changed anything?[/color]
>[color=green]
> > Anyway, you need to initialize lengthptr before calling getpeername():[/color]
>[color=green]
> > lengthptr = sizeof addr;[/color]
>[color=green]
> > BTW, why do you call this lengthptr when it's not a pointer?[/color]
>[color=green][color=darkred]
> > > #include "header.h"[/color][/color]
>[color=green][color=darkred]
> > > void catcher(int sig);
> > > int newsockfd;[/color][/color]
>[color=green][color=darkred]
> > > 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));[/color][/color]
>[color=green][color=darkred]
> > > sigaction(SIGPIPE, &act, NULL);[/color][/color]
>[color=green][color=darkred]
> > > /* set up the transport end point */[/color][/color]
>[color=green][color=darkred]
> > > if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
> > > {
> > > perror("socket call failed");
> > > exit(1);
> > > }[/color][/color]
>[color=green][color=darkred]
> > > /* bind an address to the end point */
> > > if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
> > > {
> > > perror("bind call failed");
> > > exit(1);
> > > }[/color][/color]
>[color=green][color=darkred]
> > > /* start listening for incoming connections */[/color][/color]
>[color=green][color=darkred]
> > > if ( listen(sockfd, 5) == -1 )
> > > {
> > > perror("listen call failed");
> > > exit(1) ;
> > > }[/color][/color]
>[color=green][color=darkred]
> > > while(1)
> > > {
> > > /* accept a connection */
> > > if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
> > > {
> > > perror("accept call failed");[/color][/color]
>[color=green][color=darkred]
> > > }[/color][/color]
>[color=green][color=darkred]
> > > 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);}[/color][/color]
>[color=green][color=darkred]
> > > }
> > > }[/color][/color]
>[color=green][color=darkred]
> > > void catcher(int sig)
> > > {
> > > close(newsockfd);
> > > exit (0);
> > > }[/color][/color]
>[color=green][color=darkred]
> > > OUTPUT
> > > First time output is always 0.0.0.0 else it is giving correct IP
> > > ADDRESS[/color][/color]
>[color=green]
> > --
> > 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 ***[/color]
>
> Barry is correct.
> And you need to use inet_ntoa (&sin.sin_addr)
>
> only my 2 cents[/color]
Sorry, inet_ntoa (sin.sin_addr);