Help me understand why

This is a discussion on Help me understand why within the C Programming forums, part of the General Programming Boards category; Can someone help me understand why nothing from client_2 is getting printed. I have a logging process and it takes ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Help me understand why

    Can someone help me understand why nothing from client_2 is getting printed. I have a logging process and it takes more than one connection. The first connection works fine but nothing from the second is getting printed

    Code:
     #include <sys/types.h>
     #include <sys/socket.h>
     #include <sys/wait.h>
     #include <stdio.h>
     #include <stdlib.h>
     #include <time.h>
    
    void server_process(int);
    void client_1(void);
    void client_2(int);
    void client_3(void);
    void client_4(void);
    
    int  main(void)
    {
            int s1;
            int s2;
            int s3;
            int s4;
            int status;
            struct sockaddr server = {AF_UNIX, "theserver"};
            s1 = socket (AF_UNIX, SOCK_STREAM, 0);
            bind(s1, &server, sizeof(server));
            listen(s1,4);
            struct sockaddr AB = {AF_UNIX, "ABSOCKET"};
    //      unlink(AB.sa_data);
            s2 = socket(AF_UNIX, SOCK_STREAM,0);
            bind(s2, &AB, sizeof(AB));
            listen(s2, 1);
            struct sockaddr BC =  {AF_UNIX, "BCSOCKET"};
            s3 = socket(AF_UNIX, SOCK_STREAM,0);
            bind(s3, &BC, sizeof(BC));
            listen(s3, 1);
            struct sockaddr BD =  {AF_UNIX, "BDSOCKET"};
            s4 = socket(AF_UNIX,  SOCK_STREAM,0);
            bind(s4, &BD, sizeof(BD));
            listen(s4,1);
    
            printf("right before call to server process\n");
            if(!fork()) {server_process (s1); _exit(0);}
            printf("right after call to server process and before call\n");
            if(!fork()) {client_1(); _exit(0);}
            if(!fork()) {client_2(s2); _exit(0);}
            //if(!fork()) {client_3(); _exit(0);}
            //if(!fork()) {client_4(); _exit(0);}
            wait (&status); wait(&status); //  wait(&status); wait(&status);
            //wait(&status);
            unlink ("theserver");
            unlink("ABSOCKET");
            unlink("BCSOCKET");
            unlink("BDSOCKET");
    
            return 0;
    }
    void cat(char times[])
    {
            char *timetoday;
            time_t rawtime;
            time(&rawtime);
            strcpy(times, ctime(&rawtime));
            return;
    }
    
    void server_process(int ls)
    {
            int sc1, sc2, sc3, sc4;
            int rmask;
            int i;
            char buf[100];
            char time[27];
            struct sockaddr who;
            int len;
            len = sizeof(who);
            sc1 = accept(ls, &who, &len);
            printf("reached this POINT\n");
            printf("connection1 from %s on socket #%d\n",  who.sa_data, sc1);
            len = sizeof(who);
            sc2 = accept(ls, &who, &len);
            printf("connection2 from %s on socket #%d\n", who.sa_data, sc2);
            //len = sizeof(who);
            //sc3 = accept(ls, &who, &len);
            // printf("connection from %s on socket #%d\n", who.sa_data, sc3);
            //len = sizeof(who);
            // sc4 = accept(ls, &who, &len);
            //printf("connection from %s on socket #%d\n", who.sa_data, sc4);
            close(ls);
            for( i=0; i<30; i++)
            {
                    rmask = (1<<sc1) | (1<<sc2) |(1<<sc3) | (1<<sc4);
                    select(32, &rmask, 0,0,0);
                    if(rmask & (1<<sc1))
                    {
                            printf("READ FROM SC1\n");
                            read(sc1, buf, 100);
                    }
                    else if (rmask & (1<<sc2))
                    {
                            read(sc2, buf, 100);
                    }
                    else if(rmask & (1<<sc3))
                    {
                            read(sc3, buf, 100);
                    }
                     else if(rmask & (1<<sc4))
                     {
                             read(sc4, buf, 100);
                     }
                    else
                    {
                            printf("NOTHING DOING\n");
                    }
                     cat(time);
                      printf(" %s %s ", buf, time);
            }
             read(sc2, buf, 100);  //delete
            close(sc1);
            close(sc2);
            close(sc3);
            close(sc4);
    }
    void client_1(void)
    {
            struct sockaddr me = {AF_UNIX, "client1"};
            struct sockaddr there = {AF_UNIX,  "theserver"};
            struct sockaddr socket2 = {AF_UNIX, "client2"};
            struct sockaddr mom = {AF_UNIX, "ABSOCKET"};
            char msgbuf[100];
            int s, i, a, b;
            s = socket(AF_UNIX, SOCK_STREAM, 0);
            printf("%d\n", s);
            if(bind(s, &me, sizeof(me))<0)
            {
                    printf("failed bind1\n");
            }
    
            connect (s, &there, sizeof(there));
            b= socket(AF_UNIX, SOCK_STREAM, 0);
            unlink(socket2.sa_data);
            if(bind(b, &socket2, sizeof(socket2))<0)
            {
                    perror("bind:error");
                    printf("failed bind2\n");
            }
            connect(b, &mom, sizeof(mom));
    
            int w=0;
            for(i=0; i<10; i++)
            {
                    char astring[3];
                    char message[20];
                    int randomNumber = rand() %2;
                    if(randomNumber ==0)
                    {
                            astring[0] = 'D';
                            astring[1] = w+48;
                            astring[2] = '\0';
                    }
                    if(randomNumber == 1)
                    {
                            astring[0]= 'C';
                            astring[1] = w+48;
                            astring[2]= '\0';
                    }
                    strcpy(message, "A sends B ");
                    strcat(message, astring);
                    write(b, astring, 3);
                    write(s, message, 100);
                    w++;
            }
            sleep(30);
            close (s);
            unlink("client1");
            close (b);
            unlink("client2");
    }
    
    void client_2(int ls)
    
    {
            printf("REACHED CLIENT2\n");
            int sc1;
            char buffer[3];
            char buff[60];
            char buffD[60];
            buff[0]= '\0';
            buffD[0] = '\0';
            struct sockaddr who;
            int len;
            len =sizeof(who);
            sc1 = accept(ls, &who, &len);
            if( sc1<0)
            {
                    perror("server:accept");
            }
            int x = 0;
            while(x<10)
            {
                    if((read(sc1, buffer, 3))>0)
                    {
                            if(buffer[0] == 'C')
                            {
                                    strcat(buff, buffer);
                            }
                            if(buffer[0] == 'D')
                            {
                                    strcat(buffD, buffer);
                            }
    
                             x++;
                    }
    
            }
            //***************************************************
            printf("END FOR LOOP\n");
            char mybuf[5] = "blow";
            struct sockaddr me = {AF_UNIX, "client3"};
            struct sockaddr there = {AF_UNIX,  "theserver"};
            int s, i, a, b;
            unlink(me.sa_data);
            s = socket(AF_UNIX, SOCK_STREAM, 0);
             if(bind(s, &me, sizeof(me))<0)
             {
                      perror("bind:error");
                       printf("failed bind2\n");
             }
             if( connect (s, &there, sizeof(there)) <0)
             {
                     perror("client2:connect");
             }
              x =0;
             for(x; x<100; x++)
             {
                    if(write(s, mybuf, strlen(mybuf) + 1) <0)
                    {
                             perror("client2:write");
                    }
                    printf("AFTER WRITE\n");
             }
    
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    You're not exactly checking many error return values there...at least, not in main(). Maybe something there is failing and your program is totally oblivious.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    got 2 connections to work but a third messes

    I was able to get 2 connections to work but the third connection back to the logging process messes up messages send from Client_2. Any ideas??

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void server_process(int);
    void client_1(void);
    void client_2(int);
    void client_3(int);
    void client_4(int);
    
    void main(void)
    {
            int s1;
            int s2;
            int s3;
            int s4;
    
            int status;
            struct sockaddr server = {AF_UNIX, "theserver"};
            s1 = socket (AF_UNIX, SOCK_STREAM, 0);
            bind(s1, &server, sizeof(server));
            struct sockaddr AB = {AF_UNIX, "ABSOCKET"};
            s2 = socket(AF_UNIX, SOCK_STREAM,0);
            unlink(AB.sa_data);
            bind(s2, &AB, sizeof(AB));
            struct sockaddr AC = {AF_UNIX, "ACSOCKET"};
            s3 = socket (AF_UNIX, SOCK_STREAM, 0);
            unlink(AC.sa_data);
            bind(s3, &AC, sizeof(AC));
            struct sockaddr AD = {AF_UNIX, "ADSOCKET"};
            s4 = socket(AF_UNIX, SOCK_STREAM, 0);
            unlink(AD.sa_data);
            bind(s4, &AD, sizeof(AD));
    
    
    
            listen(s1, 3);
            listen(s2, 1);
            listen(s3, 1);
            listen(s4, 1);
    
    
            if(!fork()) {server_process (s1); _exit(0);}
            if(!fork()) {client_1(); _exit(0);}
            if(!fork()) {client_2(s2); _exit(0);}
            if(!fork()) {client_3(s3); _exit(0);}
            if(!fork()) {client_4(s4); _exit(0);}
    
            wait (&status);  wait (&status);  wait (&status); wait (&status);
            wait (&status);
            unlink("theserver");
            unlink("ABSOCKET");
            unlink("ACSOCKET");
            unlink("ADSOCKET");
    
    }
    
    void server_process(int ls)
    {
            int sc1, sc2, sc3, sc4;
            int rmask;
            int i;
            long now;
            char buf[100];
            struct sockaddr who;
            int len;
            len = sizeof(who);
            sc1 = accept(ls, &who, &len);
            printf("Connection from %s on socket #%d\n", who.sa_data, sc1);
            len = sizeof(who);
            sc2 = accept(ls, &who, &len);
            printf("Connection from %s on socket #%d\n", who.sa_data, sc2);
            len = sizeof(who);
            sc3 = accept(ls, &who, &len);
             printf("Connection from %s on socket #%d\n", who.sa_data, sc3);
    
            close(ls);
            for(i=0; i<10; i++)
            {
                    rmask = (1<<sc1) | (1<<sc2) | (1<<sc3);
                    select (32, &rmask, 0,0,0);
                    if (rmask & (1<< sc1))
                            read(sc1, buf, 100);
                    else if(rmask & (1<< sc2))
                            read (sc2, buf, 100);
                    else if(rmask & (1<<sc3))
                            read(sc3, buf, 100);
                    time(&now);
                    printf("%s %s", buf, ctime (&now));
            }
            close(sc1);
            close(sc2);
    }
    void client_1(void)
    {
            struct sockaddr me = {AF_UNIX, "client1"};
            struct sockaddr there = {AF_UNIX, "theserver"};
            struct sockaddr socket2 = {AF_UNIX, "client2"};
            struct sockaddr mom = {AF_UNIX, "ABSOCKET"};
            char msgbuf[100];
            int s, i, a, b;
            s = socket(AF_UNIX, SOCK_STREAM, 0);
            bind(s, &me, sizeof(me));
            connect(s, &there, sizeof(there));
            b= socket(AF_UNIX, SOCK_STREAM, 0);
            unlink(socket2.sa_data);
            if(bind(b, &socket2, sizeof(socket2))<0)
            {
                    perror("bind:error");
                    printf("failed bind2\n");
            }
            connect(b, &mom, sizeof(mom));
            int w=0;
            for( i=0; i<5; i++)
            {
                    char astring[3];
                    char message[20];
                    int randomNumber = rand() %2;
                    if(randomNumber ==0)
                    {
                            astring[0] = 'D';
                            astring[1] = w+48;
                            astring[2] = '\0';
                    }
                    if(randomNumber == 1)
                    {
                            astring[0]= 'C';
                            astring[1] = w+48;
                            astring[2]= '\0';
                    }
                    strcpy(message, "A sends B ");
                    strcat(message, astring);
                    write(b, astring, 3);
                    write(s, message, 100);
                    w++;
                    //sprintf(msgbuf, "Message %d from CLient1 ", i);
                    //write(s, msgbuf, strlen(msgbuf) +1);
                    sleep(1);
            }
            sleep(30);
            close(s);
            unlink("client1");
    }
    
    void client_2(int ls)
    {
            int sc1;
            char buffer[3];
            char buff[60];
            char buffD[60];
            buffer[0]= '\0';
            buff[0]='\0';
            buffD[0]='\0';
            struct sockaddr me = {AF_UNIX, "client2"};
            struct sockaddr there = {AF_UNIX, "theserver"};
            char msgbuf[100];
            int s, i;
            s = socket (AF_UNIX, SOCK_STREAM, 0);
            bind(s, &me, sizeof(me));
            connect(s, &there, sizeof(there));
            struct sockaddr who;
            int len;
            len =sizeof(who);
            sc1 = accept(ls, &who, &len);
            printf("connection1 from %s on socket #%d\n",  who.sa_data, sc1);
            for(i=0; i<5; i++)
            {
                    if((read(sc1, buffer, 3))>0)
                    {
                            if(buffer[0] == 'C')
                            {
                                    strcat(buff, buffer);
                                    sprintf(msgbuf, " B sends to C %s ", buffer);
                                      write(s, msgbuf, strlen(msgbuf) +1);
    
                            }
                            if(buffer[0] == 'D')
                            {
                                     strcat(buffD, buffer);
                                     sprintf(msgbuf, " B sends to D %s ", buffer);
                                     write(s, msgbuf, strlen(msgbuf) +1);
                            }
                    }
                    sleep (1);
            }
    
            int Y, R;
            struct sockaddr me2 = {AF_UNIX, "client3"};
            struct sockaddr there2 = {AF_UNIX, "ACSOCKET"};
            Y = socket(AF_UNIX, SOCK_STREAM,0);
            unlink(me2.sa_data);
            connect(Y, &there2, sizeof(there2));
            write(Y, buff, strlen (buff) +1);
            close(Y);
            unlink("client3");
    
            struct sockaddr me3 = {AF_UNIX, "client4"};
            struct sockaddr there3 = {AF_UNIX, "ADSOCKET"};
            R= socket(AF_UNIX, SOCK_STREAM, 0);
            unlink(me3.sa_data);
            connect(R, &there3, sizeof(there3));
            write(R, buffD, sizeof(buffD) +1);
            close(R);
            unlink("client4");
    
    
    
    
            close(s);
            unlink("client2");
            printf("%s\n", buff);
            printf("%s\n", buffD);
    
    }
    
    void client_3(int ls)
    {
            int sc1;
            char buff[100];
            struct sockaddr who;
            int len;
            len = sizeof(who);
            sc1 = accept(ls, &who, &len);
            if(sc1<0)
            {
                    perror("client4:accept");
            }
            read(sc1, buff, 100);
            printf("%s\n", buff);
            struct sockaddr server = {AF_UNIX, "theserver"};
            struct sockaddr me = {AF_UNIX, "client5"};
            int s = socket (AF_UNIX, SOCK_STREAM, 0);
            unlink(me.sa_data);
            if(bind(s, &me, sizeof (me))<0)
            {
                    perror("BIND ERROR");
            }
            connect(s, &server, sizeof(server));
    
    }
    
    
    void client_4(int ls)
    {
            int sc1;
            char buff[100];
            struct sockaddr who;
            int len;
            len = sizeof(who);
            sc1 = accept(ls, &who, &len);
            if(sc1<0)
            {
                    perror("client4:accept");
            }
            read(sc1, buff, 100);
            printf("%s\n", buff);
    }

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Why are you still not checking for errors? If you're not wise enough to add them in the first place then it should definitely be the first step in debugging.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > rmask = (1<<sc1) | (1<<sc2) |(1<<sc3) | (1<<sc4);
    > select(32, &rmask, 0,0,0);
    This isn't even close to correct use of select()

    > void main(void)
    Oh dear, back to basic for you I'm afraid.

    > read(sc1, buff, 100);
    > printf("%s\n", buff);
    No account of the return result from read(), and no account of where the \0 is likely to be
    a) your declaration doesn't initialise the buffer
    b) even if you send a \0, there's no guarantee that read() will get that \0 on the first attempt.

    Use maximum warnings
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2  hello.c
    hello.c:14: warning: return type of `main' is not `int'
    hello.c: In function `main':
    hello.c:24: warning: ISO C89 forbids mixed declarations and code
    hello.c:26: warning: implicit declaration of function `unlink'
    hello.c:28: warning: ISO C89 forbids mixed declarations and code
    hello.c:32: warning: ISO C89 forbids mixed declarations and code
    hello.c:45: warning: implicit declaration of function `fork'
    hello.c:45: warning: implicit declaration of function `_exit'
    hello.c:51: warning: implicit declaration of function `wait'
    hello.c: In function `server_process':
    hello.c:70: warning: pointer targets in passing arg 3 of `accept' differ in signedness
    hello.c:73: warning: pointer targets in passing arg 3 of `accept' differ in signedness
    hello.c:76: warning: pointer targets in passing arg 3 of `accept' differ in signedness
    hello.c:79: warning: implicit declaration of function `close'
    hello.c:83: warning: implicit declaration of function `select'
    hello.c:85: warning: implicit declaration of function `read'
    hello.c:62: warning: unused variable `sc4'
    hello.c: In function `client_1':
    hello.c:115: warning: ISO C89 forbids mixed declarations and code
    hello.c:133: warning: implicit declaration of function `strcpy'
    hello.c:134: warning: implicit declaration of function `strcat'
    hello.c:135: warning: implicit declaration of function `write'
    hello.c:138: parse error before '/' token
    hello.c:140: warning: implicit declaration of function `sleep'
    hello.c:102: warning: unused variable `msgbuf'
    hello.c:103: warning: unused variable `a'
    hello.c: In function `client_2':
    hello.c:156: warning: ISO C89 forbids mixed declarations and code
    hello.c:163: warning: ISO C89 forbids mixed declarations and code
    hello.c:166: warning: pointer targets in passing arg 3 of `accept' differ in signedness
    hello.c:176: warning: implicit declaration of function `strlen'
    hello.c:189: warning: ISO C89 forbids mixed declarations and code
    hello.c:199: warning: ISO C89 forbids mixed declarations and code
    hello.c: In function `client_3':
    hello.c:225: warning: pointer targets in passing arg 3 of `accept' differ in signedness
    hello.c:232: warning: ISO C89 forbids mixed declarations and code
    hello.c: In function `client_4':
    hello.c:252: warning: pointer targets in passing arg 3 of `accept' differ in signedness

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Oh really you can't use void main(void) ??? Hmm professor has handed out several that use it. Gee looks like you are just trying to dick me around.

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Salem is correct about the use of void main , do a search on this forum why it is not "correct" to use void main.

    We´ve had the discussion about void main several times here so a search should give you enough literature to see the reasons behind Salem´s answer concerning the void main thingy .

    Greets,

    Ganglylamb.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Or one could just read the holy FAQ.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Note especially:
    Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21