Why can't get the first message's source address when using recvfrom - Linux

This is a discussion on Why can't get the first message's source address when using recvfrom - Linux ; Hi, I am using recvfrom in UNIX domain socket to get the sender's source address, but I found a strange behavior that I can't get the first message's source address, what's wrong? Below is my two little test programs: usage: ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Why can't get the first message's source address when using recvfrom

  1. Why can't get the first message's source address when using recvfrom

    Hi,

    I am using recvfrom in UNIX domain socket to get the sender's source
    address, but I found a strange behavior that I can't get the first
    message's source address, what's wrong? Below is my two little test
    programs:

    usage:
    ../client /tmp/testme aabbccddee

    client.c:

    #include
    #include
    #include
    #include
    #include

    int main(int argc,char** argv)
    {
    int lg, sock,i;
    char buf[4096];
    struct sockaddr_un adr;
    struct sockaddr_un myadr;
    unsigned long cmd;

    if(argc < 3)
    {
    return 1;
    }
    if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    {
    perror("start: socket failed");
    exit(-1);
    }
    adr.sun_family = AF_UNIX;
    strcpy(adr.sun_path,argv[1]);

    myadr.sun_family = AF_UNIX;
    strcpy(myadr.sun_path, "/tmp/client");

    unlink("/tmp/client");
    if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    perror("bind error.\n");
    exit(0);
    }

    memset(buf,0,sizeof(buf));

    for(i=2;i {
    strcat(buf,argv[i]);
    strcat(buf," ");
    }
    if (sendto(sock,buf,strlen(buf),0,(struct sockaddr *)
    &adr,sizeof(adr)) ==-1)
    {
    perror("start: sendto failed");
    exit(-1);
    }
    return 0;
    }

    server.c:
    #include
    #include
    #include
    #include
    #include

    int main(int argc,char** argv)
    {
    int lg, sock,i;
    char buf[4096];
    struct sockaddr_un adr;
    struct sockaddr_un myadr;
    unsigned long cmd;
    int len;

    if((sock = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    {
    perror("start: socket failed");
    exit(-1);
    }

    myadr.sun_family = AF_LOCAL;
    strcpy(myadr.sun_path,"/tmp/testme");

    unlink("/tmp/testme");
    if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    perror("bind error.\n");
    exit(0);
    }

    memset(buf,0,sizeof(buf));

    recvfrom(sock, buf, 4096, 0, &adr, &len);

    printf("adr path = %s\n", adr.sun_path);

    recvfrom(sock, buf, 4096, 0, &adr, &len);

    printf("2 adr path = %s\n", adr.sun_path);


    return 0;
    }

    Thanks in advance.
    Bin Chen


  2. Re: Why can't get the first message's source address when using recvfrom



    On 1月4日, 下午12时34分, "Bin Chen"
    wrote:
    > Hi,
    >
    > I am using recvfrom in UNIX domain socket to get the sender's source
    > address, but I found a strange behavior that I can't get the first
    > message's source address, what's wrong? Below is my two little test
    > programs:
    >
    > usage:
    > ./client /tmp/testme aabbccddee
    >


    I found the recvfrom can't get the source address in random as well,
    very confusing...

    > client.c:
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(int argc,char** argv)
    > {
    > int lg, sock,i;
    > char buf[4096];
    > struct sockaddr_un adr;
    > struct sockaddr_un myadr;
    > unsigned long cmd;
    >
    > if(argc < 3)
    > {
    > return 1;
    > }
    > if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    > {
    > perror("start: socket failed");
    > exit(-1);
    > }
    > adr.sun_family = AF_UNIX;
    > strcpy(adr.sun_path,argv[1]);
    >
    > myadr.sun_family = AF_UNIX;
    > strcpy(myadr.sun_path, "/tmp/client");
    >
    > unlink("/tmp/client");
    > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > perror("bind error.\n");
    > exit(0);
    > }
    >
    > memset(buf,0,sizeof(buf));
    >
    > for(i=2;i > {
    > strcat(buf,argv[i]);
    > strcat(buf," ");
    > }
    > if (sendto(sock,buf,strlen(buf),0,(struct sockaddr *)
    > &adr,sizeof(adr)) ==-1)
    > {
    > perror("start: sendto failed");
    > exit(-1);
    > }
    > return 0;
    >
    > }server.c:
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(int argc,char** argv)
    > {
    > int lg, sock,i;
    > char buf[4096];
    > struct sockaddr_un adr;
    > struct sockaddr_un myadr;
    > unsigned long cmd;
    > int len;
    >
    > if((sock = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    > {
    > perror("start: socket failed");
    > exit(-1);
    > }
    >
    > myadr.sun_family = AF_LOCAL;
    > strcpy(myadr.sun_path,"/tmp/testme");
    >
    > unlink("/tmp/testme");
    > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > perror("bind error.\n");
    > exit(0);
    > }
    >
    > memset(buf,0,sizeof(buf));
    >
    > recvfrom(sock, buf, 4096, 0, &adr, &len);
    >
    > printf("adr path = %s\n", adr.sun_path);
    >
    > recvfrom(sock, buf, 4096, 0, &adr, &len);
    >
    > printf("2 adr path = %s\n", adr.sun_path);
    >
    > return 0;
    >
    > }Thanks in advance.
    > Bin Chen



  3. Re: Why can't get the first message's source address when using recvfrom

    Bin Chen wrote:
    > Hi,
    >
    > I am using recvfrom in UNIX domain socket to get the sender's source
    > address, but I found a strange behavior that I can't get the first
    > message's source address, what's wrong? Below is my two little test
    > programs:
    >
    > usage:
    > ./client /tmp/testme aabbccddee
    >
    > client.c:
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(int argc,char** argv)
    > {
    > int lg, sock,i;
    > char buf[4096];
    > struct sockaddr_un adr;
    > struct sockaddr_un myadr;
    > unsigned long cmd;
    >
    > if(argc < 3)
    > {
    > return 1;
    > }
    > if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    > {
    > perror("start: socket failed");
    > exit(-1);
    > }
    > adr.sun_family = AF_UNIX;
    > strcpy(adr.sun_path,argv[1]);
    >
    > myadr.sun_family = AF_UNIX;
    > strcpy(myadr.sun_path, "/tmp/client");
    >
    > unlink("/tmp/client");
    > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > perror("bind error.\n");
    > exit(0);
    > }
    >
    > memset(buf,0,sizeof(buf));
    >
    > for(i=2;i > {
    > strcat(buf,argv[i]);
    > strcat(buf," ");
    > }
    > if (sendto(sock,buf,strlen(buf),0,(struct sockaddr *)
    > &adr,sizeof(adr)) ==-1)
    > {
    > perror("start: sendto failed");
    > exit(-1);
    > }
    > return 0;
    > }
    >
    > server.c:
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(int argc,char** argv)
    > {
    > int lg, sock,i;
    > char buf[4096];
    > struct sockaddr_un adr;
    > struct sockaddr_un myadr;
    > unsigned long cmd;
    > int len;
    >
    > if((sock = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    > {
    > perror("start: socket failed");
    > exit(-1);
    > }
    >
    > myadr.sun_family = AF_LOCAL;
    > strcpy(myadr.sun_path,"/tmp/testme");
    >
    > unlink("/tmp/testme");
    > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > perror("bind error.\n");
    > exit(0);
    > }
    >
    > memset(buf,0,sizeof(buf));

    len must be initialized to the size of the address
    len = sizeof addr;

    > recvfrom(sock, buf, 4096, 0, &adr, &len);
    >
    > printf("adr path = %s\n", adr.sun_path);
    >
    > recvfrom(sock, buf, 4096, 0, &adr, &len);
    >
    > printf("2 adr path = %s\n", adr.sun_path);
    >
    >
    > return 0;
    > }
    >
    > Thanks in advance.
    > Bin Chen
    >



    --
    Nils O. Sel氓sdal
    www.utelsystems.com

  4. Re: Why can't get the first message's source address when using recvfrom



    On Jan 4, 3:47 pm, "Nils O. Sel錽dal" wrote:
    > Bin Chen wrote:
    > > Hi,

    >
    > > I am using recvfrom in UNIX domain socket to get the sender's source
    > > address, but I found a strange behavior that I can't get the first
    > > message's source address, what's wrong? Below is my two little test
    > > programs:

    >
    > > usage:
    > > ./client /tmp/testme aabbccddee

    >
    > > client.c:

    >
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int main(int argc,char** argv)
    > > {
    > > int lg, sock,i;
    > > char buf[4096];
    > > struct sockaddr_un adr;
    > > struct sockaddr_un myadr;
    > > unsigned long cmd;

    >
    > > if(argc < 3)
    > > {
    > > return 1;
    > > }
    > > if((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
    > > {
    > > perror("start: socket failed");
    > > exit(-1);
    > > }
    > > adr.sun_family = AF_UNIX;
    > > strcpy(adr.sun_path,argv[1]);

    >
    > > myadr.sun_family = AF_UNIX;
    > > strcpy(myadr.sun_path, "/tmp/client");

    >
    > > unlink("/tmp/client");
    > > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > > perror("bind error.\n");
    > > exit(0);
    > > }

    >
    > > memset(buf,0,sizeof(buf));

    >
    > > for(i=2;i > > {
    > > strcat(buf,argv[i]);
    > > strcat(buf," ");
    > > }
    > > if (sendto(sock,buf,strlen(buf),0,(struct sockaddr *)
    > > &adr,sizeof(adr)) ==-1)
    > > {
    > > perror("start: sendto failed");
    > > exit(-1);
    > > }
    > > return 0;
    > > }

    >
    > > server.c:
    > > #include
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int main(int argc,char** argv)
    > > {
    > > int lg, sock,i;
    > > char buf[4096];
    > > struct sockaddr_un adr;
    > > struct sockaddr_un myadr;
    > > unsigned long cmd;
    > > int len;

    >
    > > if((sock = socket(AF_LOCAL, SOCK_DGRAM, 0)) == -1)
    > > {
    > > perror("start: socket failed");
    > > exit(-1);
    > > }

    >
    > > myadr.sun_family = AF_LOCAL;
    > > strcpy(myadr.sun_path,"/tmp/testme");

    >
    > > unlink("/tmp/testme");
    > > if (bind(sock, &myadr, sizeof(myadr)) < 0) {
    > > perror("bind error.\n");
    > > exit(0);
    > > }

    >
    > > memset(buf,0,sizeof(buf));
    > > > len must be initialized to the size of the address

    Thank you!
    > len = sizeof addr;
    >
    > > recvfrom(sock, buf, 4096, 0, &adr, &len);

    >
    > > printf("adr path = %s\n", adr.sun_path);

    >
    > > recvfrom(sock, buf, 4096, 0, &adr, &len);

    >
    > > printf("2 adr path = %s\n", adr.sun_path);

    >
    > > return 0;
    > > }

    >
    > > Thanks in advance.
    > > Bin Chen--

    > Nils O. Sel錽dalwww.utelsystems.com



  5. Re: Why can't get the first message's source address when using recvfrom

    Bin Chen wrote:
    >
    > On Jan 4, 3:47 pm, "Nils O. Sel錽dal" wrote:


    >>>>len must be initialized to the size of the address

    >
    > Thank you!


    Did you really need to quote THE ENTITE POSTING just to add these TWO WORDS?
    (Same applies to Nils, btw).

    --
    Josef M鰈lers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


+ Reply to Thread