[comp.unix.questions] Another problem with recvfrom socket call

tony@artecon.artecon.UUCP (Anthony D. Parkhurst) (05/05/87)

Ok, I am now using SOCK_DGRAM, and the recvfrom call is successful,
except that it doesn't put the sender's address in the 'from' parameter.

Also, the manual says that if the from parameter is non-zero (not a null 
pointer), then it fills in the sender's address.  This implies that if
I don't want the address, I can just pass a NULL pointer, But, this
errors with "Bad Address".

( BTW, I am using Sun 3.2 )

(Oh, BTW, please don't tell me that this will only work with AF_INET and
NOT AF_UNIX, I would rather it work in the UNIX domain :-)

Here are the program frags:

Server:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>

#define SOCKNAME "/tmp/tonys"

main()
{
int s,ns; /* socket descriptors */
static struct sockaddr name = {AF_UNIX,SOCKNAME};
static struct sockaddr nname;
int nnamelen;
int imsg;

if ((s=socket(AF_UNIX,SOCK_DGRAM,0)) < 0){
	perror("cant open socket");
	exit(-1);
	}

if(bind(s,&name,sizeof(name)) < 0){
	perror("cant bind to socket");
	exit(-2);
	}

nnamelen = sizeof(nname);
if(recvfrom(s,&imsg,sizeof(imsg),0,&nname,&nnamelen) < 0){
	perror("server recvfrom error");
	exit(-5);
	}

fprintf(stderr,"Recv int: %d\n",imsg);

fprintf(stderr,"Recv family: %d, nname: %s\n",nname.sa_family,nname.sa_data);

/*       Works fine to here, gets the proper message, however, the result in
    nname is :  nname.sa_family == 1, nname.sa_data == "" (no name or address */
/* SO, I can't return a message since I don't have the sender's address */

client:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>

#define SOCKNAME "/tmp/tonys"
#define SOCKNNAME "/tmp/tonyc"

main()
{
int s,ns; /* socket descriptors */
static struct sockaddr name = {AF_UNIX,SOCKNAME};
static struct sockaddr nname = {AF_UNIX,SOCKNNAME};
int namelen;
int nnamelen;
int imsg;

if ((s=socket(AF_UNIX,SOCK_DGRAM,0)) < 0){
	perror("cant open socket");
	exit(-1);
	}

if(bind(s,&nname,sizeof(nname)) < 0){
	perror("cant bind to socket");
	exit(-2);
	}

imsg = 345; /* arbitrary */

if(sendto(s,&imsg,sizeof(imsg),0,&name,sizeof(name)) < 0){
	perror("send error");
	exit(-6);
	}

	/* works to here */
-- 
**************** Insert 'Standard' Disclaimer here:  OOP ACK! *****************
*  Tony Parkhurst -- {hplabs|sdcsvax|ncr-sd|hpfcla|ihnp4}!hp-sdd!artecon!adp  *
*                -OR-      hp-sdd!artecon!adp@nosc.ARPA                       *
*******************************************************************************

lm@cottage.WISC.EDU (Larry McVoy) (05/07/87)

In article <438@artecon.artecon.UUCP> tony@artecon.artecon.UUCP (Anthony D. Parkhurst) writes:
>
>Ok, I am now using SOCK_DGRAM, and the recvfrom call is successful,
>except that it doesn't put the sender's address in the 'from' parameter.
>
>(Oh, BTW, please don't tell me that this will only work with AF_INET and
>NOT AF_UNIX, I would rather it work in the UNIX domain :-)

Well, I think you're out of luck.  AF_UNIX has been broken for a long
time and I I'm not sure that there exists a working version anywhere.
BSD had problems up through 4.2, such as getsockname() didn't work...

This is probably something similar...

Why won't you use AF_INET sockets?  If you use "localhost" for the host
name it gives you something that ought to be very close (if not exactly)
what you are looking for.  And it works (strong incentive...)
---
Larry McVoy 	        lm@cottage.wisc.edu  or  uwvax!mcvoy

"What a wonderful world it is that has girls in it!"  -L.L.