CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2022
    Posts
    11

    Sending Posix message via pipe

    I have following file called helper.h, which I want to call in server and client files later.

    Code:
    #ifndef HELPER_H
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include<cstdio>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<climits>
    #include<fcntl.h>
    /*
     * It will iterate through all the lines in file and
     * put them in given vector
     */
    int getFileContent(std::string infileName,std::vector<std::string>& vecOfStrs)
    {
        // Open the File to read the content into buffer
        std::ifstream in(infileName.c_str());
    
        // Check if object is valid
        if (!in)
        {
            throw (std::runtime_error("Cannot open the File : " ));
    
        }
    
        std::string str;
        // Read the next line from File until it reaches the end.
        while (std::getline(in, str))
        {
            // Line contains string of length > 0 then save it in vector
            if (str.size() > 0)
                vecOfStrs.push_back(str);
        }
    
        /*for (const auto& str : vecOfStrs)
            std::cout << str << '\n';*/
    
    
    
        in.close();
        return str.size();
    
    }
    
    int writeFileContent(std::string outfileName, std::vector<std::string>& vecOfStrs){
        std::string str;
        //Open the file to output the read buffer
        std::ofstream out(outfileName.c_str());
    
        if (!out)
        {
            std::cerr << "Write operation failed.";
            return false;
        }
    
        //int result = getFileContent("sample.txt", vecOfStrs);//This line should be removed
    
    
            for (const auto& row : vecOfStrs)
                out << row << '\n';
        
        out.close();
    
        return str.size();//str is created but never used re
    
    }//Close of the write function
    
    
    #endif
    I want to call it via server named sender.cpp. I want to compare bytes_read and bytes_write and then send it to the receiver. My code is correct till pipe open. How can i proceed after that.
    Code:
    //
    // Created by pramah on 4/11/22.
    //
    
    #include "helper.h"
    
    
    
    int main(){
    
        return 0;
    }
    void send(std::string pipe_name, std::string fname) {
        std::cout << "Starting pipeSend.." << std::endl;
        unlink(name.c_str());
    
        int pd;
        int total=0;
        std::vector<char> buffer(PIPE_BUF);
        std::vector<std::string> vec_str;
        std::ifstream ifs;
        unsigned bytes_read, bytes_write;
    
        //Pipe created
        if(mkfifo(name.c_str(),0666)==-1){
            throw(std::runtime_error("Sending failed. mkfifo"));
        }
    
        //Open the pipe
        pd = open(name.c_str(),O_WRONLY);
        if(pd==-1){
            throw(std::runtime_error("Send(): Pipe onpening failed"));
        }
    
        //Read and write operation
        while(ifs){//infinite loop
            bytes_read=getFileContent(fname, vec_str);
            if(bytes_read<=0) {
                throw(std::runtime_error("Send: readFile"));
            }
        }//reading input stream done
    
        //Write to pipe
        bytes_write = write(pd,buffer.data(),bytes_read);
        if(bytes_write==-1){
            throw(std::runtime_error("send: write to pipe"));
        }
    
        if(bytes_read==bytes_write){
            total +=bytes_read;
        }
        else{
            throw(std::runtime_error("send: read write difference"));
        }
        ifs.close();
        close(pd);
        std::cout<<"File deliverd from the pipe."<<std::endl;
        /*
    
    
    }
    Last edited by perto1; April 24th, 2022 at 03:36 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,266

    Re: Sending Posix message via pipe

    Quote Originally Posted by perto1 View Post
    ...
    I want to call it via server named sender.cpp. I want to compare bytes_read and bytes_write and then send it to the receiver. My code is correct till pipe open. How can i proceed after that.
    ...
    Why do you throw the runtime_error when bytes_read > 0?
    Code:
        //Read and write operation
        while(ifs){//infinite loop
            bytes_read=getFileContent(fname, vec_str);
            if(bytes_read>0) {
                throw(std::runtime_error("Send: readFile"));
            }
        }//reading input stream done
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2022
    Posts
    11

    Re: Sending Posix message via pipe

    It should throw error when byte is not read. (Edited)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured