0

I have an assignment to combine MPI and Socket programming for searching a file. MPI will be used to create processes which search into folders whereas sockets will be used for sending and receiving messages. My only problem right now is how I notify my master process if no file was found. Everything else is working fine.

Here is my code:

int main(int argc, char** argv) 
{   
    int rank, size;
    DIR *d;
    FILE *fp;
    struct dirent *dir;

    if (argc < 2) {
        error("Ju lutem jepni emrin e file.");
    }

    printf("Fillon kerkimi...\n\n");

    // Initialize the MPI environment.
    MPI_Init(&argc, &argv);

    // Get the number of processes
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // Get the rank of the process
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (rank == 0) {
        server();
    } else {
        char str[100];
        sprintf(str, "./%d/%s", rank, argv[1]);

        //if file is found
        if ((fp = fopen(str, "r")) != NULL) {
            char buffer[4096];
            int i=0;
            char c;
            while((c = getc(fp)) != EOF)
            {
                 buffer[i++] = c;
            }
            buffer[i]='\0';

            client(buffer);
        }
    }

    MPI_Finalize();

    return 0;
}

The server() and client() functions are used for sending and receiving using sockets. Didn't post the code because it is working fine.

Any ideas?

EDIT:

Server code:

void server() {

    int sockfd, newsockfd, clilen;
    char buffer[MAX_LEN];
    struct sockaddr_in serv_addr, cli_addr;
    int n;

    struct timeval timeout;      
    timeout.tv_sec = 3;
    timeout.tv_usec = 0;


    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
    {
        error("ERROR opening socket");
    }



    //clear
    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(PORTNO);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    {
        error("ERROR on binding");
    }

    if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
            sizeof(timeout)) < 0) {;
        printf("File nuk u gjend!");
        return;
    }

    listen(sockfd,5);;
    clilen = sizeof(cli_addr);

    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

    if (newsockfd < 0)
    {
        error("ERROR on accept");
    }

    bzero(buffer, 256);

    if (read(newsockfd, buffer, 255) < 0)
    {
        error("ERROR reading from socket");
    }

    close(newsockfd);
    close(sockfd);

    printf("File output: \n%s\n", buffer);

    return;
}

Client code:

void client(char* msg[]) {

    int sockfd, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    strcpy(buffer, msg);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)
    {
        error("ERROR opening socket");
    }

    server = gethostbyname("localhost");

    if (server == NULL) 
    {
        fprintf(stderr, "ERROR no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    bcopy(  (char *) server->h_addr, 
            (char *) &serv_addr.sin_addr.s_addr, 
            server->h_length
            );
    serv_addr.sin_port = htons(PORTNO);


    while(1)
    {
        if (connect(sockfd, &serv_addr, sizeof(serv_addr)) >= 0)
            break;
    }   

    if (write(sockfd, buffer, strlen(buffer)) < 0)
    {   
        error("ERROR writing to socket");
    }


    close(sockfd);
    return;

}

Note: I'm using setsockopt() on the server function which seems to work well for a timeout and do the job, though it doesn't let me print my own message to the console.

user3484582
  • 557
  • 1
  • 6
  • 22
  • Is there any reason why you would want to use sockets for communication? After all you are talking about the **Message Passing Interface**. It's like *"Use a car to go to the city, but you have to push it with your hands."* – Zulan May 31 '16 at 21:01
  • Well, that's what I was wondering, bur for some reason that's how the assignment is. Use MPI to create the processes and sockets for communication. It is supposed to simulate a client-server communication and we are supposedly searching for the file in different computers. – user3484582 May 31 '16 at 21:04
  • I would advise you to question the assignment. In any case - if you aren't allowed to use MPI for the communication - and that's what you are struggling with, then this question is not about MPI. Anyway, please post your `server()` and `client()` code. – Zulan May 31 '16 at 21:18
  • BTW: [Do not use `sprintf` like that](http://stackoverflow.com/q/3662899/620382). – Zulan May 31 '16 at 21:20
  • Thank you! I will definitely bring that up to the professor. Check out my edit. Will also check `sprintf` – user3484582 May 31 '16 at 21:26

0 Answers0