Uploading File from C Client to PHP Server

Detection score: 4.0

<p>I am looking for a way to receive Images on a Php client and sent from a (working) C server implemented on a camera.
So the main issue is the Php code, You can find the server code below. 
For the php, I just created a simple socket, what would you suggest?
Thanks for any help :)
The server has been tested with a Java app already.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;capture.h&gt;
#include &lt;syslog.h&gt;




char *data;
static int Freq;
FILE *Image_File;

void send_int(int number, int socket);

void send_int(int number, int socket)
{
  int32_t n_conv = htonl(number);
  char *datab = (char *) &amp;n_conv;
  int left = sizeof(n_conv);
  int sent_pack;
  do
  {
    sent_pack = write(socket, datab, left);
    if (sent_pack &lt; 0)
        exit(1);
    else
    {
        datab += sent_pack;
        left -= sent_pack;
    }
  }while (left &gt; 0);
}

void Capture_Send_Image(char spec[], int socket);
//Beginning of Capture_Send_Image function.
void Capture_Send_Image(char spec[], int socket)
{
  size_t size;  
  char read_buffer [256];
  media_frame  *frame;
  media_stream *stream;
  stream = capture_open_stream(IMAGE_JPEG, spec);//Establishes the connection with the stream.
  int j = 1;

while (j &lt;= Freq)
{
  frame = capture_get_frame(stream);//The actual capturing for the image.
  data = capture_frame_data(frame);//This is where the image will be saved.
  size = capture_frame_size(frame);//Gets the image's size

    send_int((int) size, socket);
    bzero(read_buffer, 0);
    read(socket, read_buffer, 255);
    write(socket, (char *)data, size);
    bzero(read_buffer, 0);
    if ((read(socket, read_buffer, 255)) &lt; 0)
    {
        sleep(3000);
        exit(1);
    }
    j++;
}
  capture_frame_free(frame);//Closing the frame.
  capture_close_stream(stream);//Closing the stream.


}//Capture_Send_Image function ends here. 


void Do_Processing(int socket);
//Beginning of doprocessing function.
void Do_Processing(int socket)
{
    int n;
    char read_buffer [256];
    char specifications [30];

    strcpy(specifications, "fps=");


    /*
     * Read how many frames per second should be captured, and the value sent from the client
     * and assigned into Freq variable.
     */
    bzero(read_buffer, 256);
    n = read(socket, read_buffer, 255);
    if (n &lt; 0)
    {
        perror("ERROR reading the frequency!\n");
        exit(1);
    }

    strcat(specifications, (char *)read_buffer);
    Freq = atoi((char *) read_buffer);

    /*
     * Read the resolution of the captured images, and the value sent from the client
     * and assigned into Res variable.
     */
    bzero(read_buffer, 256);
    n = read(socket, read_buffer, 255);

    if (n &lt; 0)
    {
        perror("ERROR reading the resolution!");
        exit(1);
    }

    strcat(specifications, "&amp;resolution=");
    strcat(specifications, (char *)read_buffer);
    strcat(specifications, "\nFreq = %i");

    while (1)
    {
        Capture_Send_Image(specifications, socket);
    }

}


//Beginning of Main function.
int main(void)//int argc, char *argv[])
{
    int sockfd;
    int newsockfd;
    int portno;
    int clientlen;

    struct sockaddr_in serv_addr;
    struct sockaddr_in client_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    /*First call to socket function*/
    if(sockfd &lt; 0)
    {
        perror("ERROR opening socket");
        exit(1);
    }
    /*Initialize socket structure*/
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    portno = 5001;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    /*Now bind the host address using bind() call.*/
    if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0)
    {
        perror("ERROR on binding");
        exit(1);
    }

    /* Now start listening for the clients, here process will
     * go in sleep mode and will wait for the incoming connetion 
    */

    listen(sockfd, 5);
    clientlen = sizeof(client_addr);

    /* Accept actual connection from the client*/
    while (1)
    {
        newsockfd = accept(sockfd,(struct sockaddr *) &amp;client_addr, (socklen_t *)&amp;clientlen);
        if (newsockfd &lt; 0)
        {
            perror("ERROR on accept");
            exit(1);
        }
        /* Create a child process*/
        int pid = fork();
        if (pid &lt; 0)
        {
            perror("ERROR on fork");
            exit(1);
        }
        if (pid == 0)
        {
            /* This is the client process*/
            close(sockfd);
            Do_Processing(newsockfd);
            exit(0);
        }
        else
        {
            close(newsockfd);
        }
    }
    //return 0;
}//Main function ends here.
</code></pre>
Posted 9 days ago | reported 9 days ago
Posted by Charles Lee (1)

Caught by 3 reasons: