Im writing a program that uses sockets for inter process communication however i have come to a road block and cant get my code to compile. i have googled all the errors with no luck finding a solution. If any one can help me it would be appreciated. Here is a more detailed description of the assignment http://voyager.cs.bgsu.edu/gzimmer/cs3270/cs3270fa11lab4.pdf

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <iostream.h>

int main(int argc,char *argv[])
{
    int sv[2]; /* the pair of socket descriptors */
    char buf[256]; /* for data exchange between processes */
    int m, n, random;
    bool loop = true;
    char check[50];
    m = atoi(argv[1]);
    n= atoi(argv[2]);

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
        perror("socketpair");
        exit(1);
    }

    if (!fork()) {  /* childm */
               while(loop){ 
       read(sv[0], &buf, 256);
       random = atoi(buf);
       if(random % 2 == 0)
       {
        check = "even";
       }
       else{
        check = "odd";}
        
        if(random > 0){
          if(n>0){
           cout << "Recieved " << random << " from p0 it is "<< check << endl;
           random = rand() % 1000;
           buf = itoa(random, buf, 10);
           cout << "Sending " << random << "to p0" << endl;
           write(sv[1], &buf, 256);
           n--;
          }
          else{
           cout << "Recieved " << random << " from p0 it is "<< check << endl;
           buf = itoa(0, buf,10);
           write(sv[1], &buf, 256);
          }
        }
        else if(random == 0){
          if(n>0){
           random = rand() % 1000;
           buf = itoa(random, buf, 10);
           cout << "Sending " << random << "to p0" << endl;
           write(sv[1], &buf, 256);
           n--;
          }
          else{
           buf = itoa(-1, buf,10);
           write(sv[1], &buf, 256);
          }
        }
        else if( random == -1){
         loop = false;
         buf = itoa(-1, buf,10);
         write(sv[1], &buf, 256);
        }

       }
       cout << "P1 is now finished"<< endl;

        
        

    } else { /* parent n*/
      if(n>0){
       random = rand() % 1000;
       buf = itoa(random, buf, 10);
       cout << "Sending " << random << "to p1" << endl;
       write(sv[0], &buf, 1);
       n--;
      }
      else{
       buf = itoa(0, buf,10);
       write(sv[0], &buf, 1);
      }
       while(loop){ 
       read(sv[0], &buf, 1);
       random = atoi(buf);
       if(random % 2 == 0)
       {
        check = "even";
       }
       else{
        check = "odd";}
        
        if(random > 0){
          if(n>0){
           cout << "Recieved " << random << " from p1 it is "<< check << endl;
           random = rand() % 1000;
           buf = itoa(random, buf, 10);
           cout << "Sending " << random << "to p1" << endl;
           write(sv[0], &buf, 1);
           n--;
          }
          else{
           cout << "Recieved " << random << " from p1 it is "<< check << endl;
           buf = itoa(0, buf,10);
           write(sv[0], &buf, 1);
          }
        }
        else if(random == 0){
          if(n>0){
           random = rand() % 1000;
           buf = itoa(random, buf, 10);
           cout << "Sending " << random << "to p1" << endl;
           write(sv[0], &buf, 1);
           n--;
          }
          else{
           buf = itoa(-1, buf,10);
           write(sv[0], &buf, 1);
          }
        }
        else if( random == -1){
         loop = false;
         buf = itoa(-1, buf,10);
         write(sv[0], &buf, 1);
        }

       }
       cout << "PO is now finished"<< endl; 
    }

    return 0;
}

Recommended Answers

All 4 Replies

For starters, you're using out-of-date headers and you don't have any namespace resolution.
The headers <stdio.h>, <stdlib.h>, <ctype.h>, <errno.h>, <string.h> and <iostream.h>. Are the old versions of the standard headers. You should use <cstdio>, <cstdlib>, <cctype>, <cerrno>, <cstring> and <iostream> respectively instead.

Perhaps this will explain most of your problems:

Portability
This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be sprintf:
sprintf(str,"%d",value) converts to decimal base.
sprintf(str,"%x",value) converts to hexadecimal base.
sprintf(str,"%o",value) converts to octal base.

What compiler are you using?

Fixed the with your suggestions and now it compiles. However the program doesn't seem to be working properly. I only get 1 random number and the program stops outputting is there any thing you can see in my code that causes it, because when i go through it seems like it should work.

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cerrno>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
    int sv[2]; /* the pair of socket descriptors */
    char buf[256]; /* for data exchange between processes */
    int m, n, random;
    bool loop = true;
    string check;
    m = atoi(argv[1]);
    n= atoi(argv[2]);

    if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
        perror("socketpair");
        exit(1);
    }

    if (!fork()) {  /* childm */
               while(loop){ 
       read(sv[0], &buf, 256);
       random = atoi(buf);
       if(random % 2 == 0)
       {
        check = "even";
       }
       else{
        check = "odd";}
        
        if(random > 0){
          if(n>0){
           cout << "Recieved " << random << " from p0 it is "<< check << endl;
           random = rand() % 1000;
           sprintf(buf, "%d", random);
           cout << "Sending " << random << "to p0" << endl;
           write(sv[1], &buf, 256);
           n--;
          }
          else{
           cout << "Recieved " << random << " from p0 it is "<< check << endl;
           sprintf(buf, "%d", 0);
           write(sv[1], &buf, 256);
          }
        }
        else if(random == 0){
          if(n>0){
           random = rand() % 1000;
           sprintf(buf, "%d", random);
           cout << "Sending " << random << "to p0" << endl;
           write(sv[1], &buf, 256);
           n--;
          }
          else{
           sprintf(buf, "%d", -1);
           write(sv[1], &buf, 256);
          }
        }
        else if( random == -1){
         loop = false;
         sprintf(buf, "%d", -1);
         write(sv[1], &buf, 256);
        }

       }
       cout << "P1 is now finished"<< endl;

        
        

    } else { /* parent n*/
      if(n>0){
       random = rand() % 1000;
       sprintf(buf, "%d", random);
       cout << "Sending " << random << "to p1" << endl;
       write(sv[0], &buf, 1);
       n--;
      }
      else{
       sprintf(buf, "%d", 0);
       write(sv[0], &buf, 1);
      }
       while(loop){ 
       read(sv[0], &buf, 1);
       random = atoi(buf);
       if(random % 2 == 0)
       {
        check = "even";
       }
       else{
        check = "odd";}
        
        if(random > 0){
          if(n>0){
           cout << "Recieved " << random << " from p1 it is "<< check << endl;
           random = rand() % 1000;
           sprintf(buf, "%d", random);
           cout << "Sending " << random << "to p1" << endl;
           write(sv[0], &buf, 1);
           n--;
          }
          else{
           cout << "Recieved " << random << " from p1 it is "<< check << endl;
           sprintf(buf, "%d", 0);
           write(sv[0], &buf, 1);
          }
        }
        else if(random == 0){
          if(n>0){
           random = rand() % 1000;
           sprintf(buf, "%d", random);
           cout << "Sending " << random << "to p1" << endl;
           write(sv[0], &buf, 1);
           n--;
          }
          else{
           sprintf(buf, "%d", -1);
           write(sv[0], &buf, 1);
          }
        }
        else if( random == -1){
         loop = false;
         sprintf(buf, "%d", -1);
         write(sv[0], &buf, 1);
        }

       }
       cout << "PO is now finished"<< endl; 
    }

    return 0;
}

I notice you're using the RNG. Have you seeded it? I don't see a call to srand()...

I don't program with sockets, so I'm afraid I probably can't help beyond what I have.

Hopefully, someone else can.

Ahh well thanks for the help i appreciate it. Good catch on the srand() :s

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.