Unix Programming - UDP recvfrom unable to get senders address

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2006 > UDP recvfrom unable to get senders address





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author UDP recvfrom unable to get senders address
kiranj.email@gmail.com

2006-06-29, 1:23 am

Hi,
I have a udp local client server program and I am trying to print the
senders address. However, I donot seem to get the address. Can some one
help me with this? Thx.

I can see the data is sent properly but the address is not printed

########################################
#####################
Client Program
########################################
######################
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
int clientSocket,
remotePort,
status = 0;
struct sockaddr_un clientName;
struct sockaddr_un _dest;
char buffer[256] = "This is client buffer";
struct sockaddr_un _addr;

clientSocket = socket(AF_UNIX, SOCK_DGRAM, 0);

if (-1 == clientSocket)
{
printf("%d socket()",__LINE__);
exit(1);
}
bzero(&_addr, sizeof(_addr));
_addr.sun_family = AF_UNIX;
strcpy(_addr.sun_path,"/tmp/clt.sock");

if ( bind(clientSocket,(struct sockaddr*)&_addr,sizeof(_addr)) < 0)
{
printf("%d bind()",__LINE__);
}

bzero(&_dest, sizeof(_dest));
_dest.sun_family = AF_UNIX;
strcpy(_dest.sun_path,"/tmp/svr.sock");

sendto(clientSocket,
buffer, // buffer
strlen(buffer)+1,
0, // flags
(struct sockaddr *) &_dest, // destination
address
sizeof(_dest)); //size of destination address
(used internally)




close(clientSocket);
unlink("/tmp/clt.sock");

return 0;
}

########################################
#####################
Server Program
########################################
######################
#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

extern errno;

int main(int argc, char *argv[])
{
int sock_fd,
remotePort,
status = 0;
struct sockaddr_un clientName;
char buffer[256] = "This is server buffer";
struct sockaddr_un _addr;
struct sockaddr_un _dest;
struct sockaddr_un _clt;
int _clt_len = 0;
int sentlen = 0;

sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0);

if (-1 == sock_fd)
{
printf("%d socket()",__LINE__);
exit(1);
}
bzero(&_addr, sizeof(_addr));
_addr.sun_family = AF_UNIX;
strcpy(_addr.sun_path,"/tmp/svr.sock");

if ( bind(sock_fd,(struct sockaddr*)&_addr,sizeof(_addr)) < 0)
{
printf("%d bind()",__LINE__);
exit(1);
}

bzero(&_dest, sizeof(_dest));
_dest.sun_family = AF_UNIX;
strcpy(_dest.sun_path,"/tmp/clt.sock");

sleep(1);
sentlen = sendto(sock_fd, // socket descriptor
buffer, // buffer
strlen(buffer)+1,
0, // flags
(struct sockaddr *) &_dest, // destination
address
sizeof(_dest)); //size of destination address
(used internally)


bzero(&_clt, sizeof(_clt));
_clt.sun_family = AF_UNIX;

sentlen = recvfrom( sock_fd, // socket descriptor
buffer, // buffer to receive data
256,
0, //flags
(struct sockaddr*)&_clt, // source address
&_clt_len); // length of source address
structure

// (socklen_t*)&_clt_len); // length of source
address structure

printf("%s\n%s\n%d\n%d\n",buffer,_clt.sun_path,_clt_len,sentlen);

unlink("/tmp/svr.sock");

return 0;
}

Chris Friesen

2006-06-29, 1:23 am

kiranj.email@gmail.com wrote:

> I have a udp local client server program and I am trying to print the
> senders address. However, I donot seem to get the address. Can some one
> help me with this? Thx.


From the manpage for recvfrom():

ssize_t recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);

"The argument fromlen is a value-result parameter, initialized to the
size of the buffer associated with from, and modified on return to
indicate the actual size of the address stored there."


That should be enough for you to track down the problem.

Chris
kiranj.email@gmail.com

2006-06-29, 7:37 am

Hi Chris,
I had actually see the man page and unable to make much out if it. Can
you give little more insight to this? Deeply appreciate your help in
this regard.

Thx,
-Kiran

Chris Friesen wrote:
> kiranj.email@gmail.com wrote:
>
>
> From the manpage for recvfrom():
>
> ssize_t recvfrom(int s, void *buf, size_t len, int flags,
> struct sockaddr *from, socklen_t *fromlen);
>
> "The argument fromlen is a value-result parameter, initialized to the
> size of the buffer associated with from, and modified on return to
> indicate the actual size of the address stored there."
>
>
> That should be enough for you to track down the problem.
>
> Chris


kiranj.email@gmail.com

2006-06-29, 7:37 am

Also,
When I try to use the same address received to send the data back, it
seems to go fine. But when I try to print the address is when I see the
problem.. So, the problem seems to be in the way I am trying to print
the received address.

Thx,


kiranj.em...@gmail.com wrote:[vbcol=seagreen]
> Hi Chris,
> I had actually see the man page and unable to make much out if it. Can
> you give little more insight to this? Deeply appreciate your help in
> this regard.
>
> Thx,
> -Kiran
>
> Chris Friesen wrote:

Maxim Yegorushkin

2006-06-29, 7:37 am


kiranj.email@gmail.com wrote:
> Hi,
> I have a udp local client server program and I am trying to print the
> senders address. However, I donot seem to get the address. Can some one
> help me with this? Thx.
>
> I can see the data is sent properly but the address is not printed
>


[]

> clientSocket = socket(AF_UNIX, SOCK_DGRAM, 0);


You are not using UDP sockets.

By definition of UNIX sockets the other peer is always localhost. This
is why getpeername() and recvfrom() does not report you the other host
address.

Logan Shaw

2006-06-29, 7:37 am

kiranj.email@gmail.com wrote:
> Chris Friesen wrote:
[vbcol=seagreen]
[vbcol=seagreen]
[vbcol=seagreen]
> I had actually see the man page and unable to make much out if it. Can
> you give little more insight to this? Deeply appreciate your help in
> this regard.


Read it again. What does it say that the parameter should be initialized
to? The size of the buffer. And what are you initializing it to? Zero.
Is zero the size of your buffer? If so, why would you expect the call to
be able to write any data into a zero-length buffer?

By the way, I fixed your top-posting.

- Logan
Nils O. Selåsdal

2006-06-29, 7:37 am

Maxim Yegorushkin wrote:
> kiranj.email@gmail.com wrote:
>
> []
>
>
> You are not using UDP sockets.
>
> By definition of UNIX sockets the other peer is always localhost. This
> is why getpeername() and recvfrom() does not report you the other host
> address.


Unix sockets have a source and destination address too.
Barry Margolin

2006-06-30, 1:20 am

In article <44a393ad@news.broadpark.no>,
"Nils O. Selåsdal" <NOS@Utel.no> wrote:

> Maxim Yegorushkin wrote:
>
> Unix sockets have a source and destination address too.


AFAIK, it makes no sense for a Unix socket client to bind() at his end.
A single socket special file represents the whole connection, there's no
distinct local and remote sockets as there are on the network.

--
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 ***
zhy

2006-06-30, 1:20 am

hi..
Try getsockname().

kiranj.email@gmail.com =E5=86=99=E9=81=93=EF=BC=9A
[vbcol=seagreen]
> Also,
> When I try to use the same address received to send the data back, it
> seems to go fine. But when I try to print the address is when I see the
> problem.. So, the problem seems to be in the way I am trying to print
> the received address.
>
> Thx,
>
>
> kiranj.em...@gmail.com wrote:
he[vbcol=seagreen]
one[vbcol=seagreen]

Barry Margolin

2006-06-30, 1:24 pm

In article <1151646262.566226.135900@b68g2000cwa.googlegroups.com>,
"zhy" <yzhang90003@gmail.com> wrote:

> Try getsockname().


That gets the local socket's address, not the remote 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 ***
zhy

2006-07-01, 1:27 pm

hi Barry!

You are right.
I made a mistake..

zhy


Barry Margolin =E5=86=99=E9=81=93=EF=BC=9A

> In article <1151646262.566226.135900@b68g2000cwa.googlegroups.com>,
> "zhy" <yzhang90003@gmail.com> wrote:
>
>
> That gets the local socket's address, not the remote 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 ***


Nils O. Selåsdal

2006-07-02, 1:44 pm

Barry Margolin wrote:
> In article <44a393ad@news.broadpark.no>,
> "Nils O. Selåsdal" <NOS@Utel.no> wrote:
>
>
> AFAIK, it makes no sense for a Unix socket client to bind() at his end.
> A single socket special file represents the whole connection, there's no
> distinct local and remote sockets as there are on the network.

There can be.
If a client doesn't bind, it uses an unnamed socket - it's address is
unspecified by posix(Many unixes gives it an address outside the
filesystem namespace in this case.)

If the client does bind(2), getpeername fetches that
address, if the client doesn't bind - it depends on your unix.



Barry Margolin

2006-07-03, 1:27 am

In article <44a7e561$1@news.wineasy.se>,
"Nils O. Selåsdal" <noselasd@asgaard.homelinux.org> wrote:
> If the client does bind(2), getpeername fetches that
> address, if the client doesn't bind - it depends on your unix.


OK, but that "if" is a pretty big one. I can't imagine WHY a
Unix-domain socket client would call bind().

--
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 ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com