luisro333
luisro333

Reputation: 13

Sending binary image through pipes in python to a C server

so i'm trying to send a file (in this case a .jpg image) in binary from a client in python 3 to a C server through pipes and for some reason it displays the error, broken pipe, here is the code: python:

import os,sys,errno,pipes,signal,time     

def Tuberia():
    fifo = "/tmp/fifoNombre"
    print ("conecting to a pipe...",fifo)
    file = open("/home/luisro/Pictures/64.jpg","r+b")
    f = open(fifo,'wb')
    for line in file:
        print(line)
        f.write(line)
    f.close()
    file.close()

and the C server:

void reciveFile(){
  int fn;
  char * fifoNombre = "/tmp/fifoNombre";
  // //opens the pipe for reading
  mkfifo(fifoNombre, 0666);
  unsigned char datos[MAX_BUF];
  fn = open(fifoNombre, O_RDONLY);
    read(fn, datos, MAX_BUF);
    saving(datos,"/home/luisro/Desktop/algo.jpg");
   unlink(fifoNombre);

}


void saving(unsigned char *data, char* dirDest){
    FILE *flujoArchivo = fopen(dirDest, "wb");
    if(flujoArchivo == NULL){
        printf("Error saving.\n");
        exit(-1);
    }
    int writed_size = fwrite(data, sizeof(unsigned char), MAX_BUF, flujoArchivo);

    fclose(flujoArchivo);
}

so those are the functions i don't know if is the python client or the C server where the problem is, thanks in advance

Upvotes: 1

Views: 1204

Answers (2)

luisro333
luisro333

Reputation: 13

so i solve it with out any loop this way:

def Tuberia():
print("sending file")
    fifo = "/tmp/fifoNombre"
    print ("connecting to pipe...",fifo)
    try:
        f = open(fifo,'wb')
        with open("/home/luisro/Pictures/64.jpg","r+b") as file:
            line = file.read()
            f.write(line)
            print("sending succesfully...")
    except:
        print("problem connecting to pipe\n")
        Tuberia()
    print("closing conexions....")    
    time.sleep(2)
    file.close() 
    f.close()

the other problem is the file weights 996.9 kb and when it's send it weights 8.3 mb wich is the size of MAX_BUF in the C server does any one know how to send the correct size for any file?

here is the C server:

void reciveFile(){
  int fn;
  char * fifoNombre = "/tmp/fifoNombre";
  // //opens the pipe for reading
  mkfifo(fifoNombre, 0666);
  unsigned char datos[MAX_BUF];
  fn = open(fifoNombre, O_RDONLY);
    read(fn, datos, MAX_BUF);
    saving(datos,"/home/luisro/Desktop/algo.jpg");
   unlink(fifoNombre);

}


void saving(unsigned char *data, char* dirDest){
    FILE *flujoArchivo = fopen(dirDest, "wb");
    if(flujoArchivo == NULL){
        printf("Error saving.\n");
        exit(-1);
    }
    int writed_size = fwrite(data, sizeof(unsigned char), MAX_BUF, flujoArchivo);

    fclose(flujoArchivo);
}

Upvotes: 0

TonyB
TonyB

Reputation: 947

A binary file is not "line" oriented, which is how your attempting to read it in python.... Here's a link showing how to read a binary file in python: The-link

Additionally, you may have to change your server to perform some looping when it is receiving the binary file, if it is greater than MAX_BUF.

Upvotes: 1

Related Questions