Post image from linux (using c without curl) to php server -
i using v4l2 library on linux, take picture , want send php server via c program. want using socket it. don't know how pass image request . sample code:
int portno = 80; struct sockaddr_in serv_addr; int sockfd, bytes, sent, received, total; sockfd = socket(af_inet, sock_stream, 0); char message[1024],response[4096]; if (sockfd < 0){ printf("error opening socket"); } memset(&serv_addr,0,sizeof(serv_addr)); serv_addr.sin_family = af_inet; serv_addr.sin_port = htons(portno); if(inet_pton(af_inet, const_domain, &serv_addr.sin_addr)<=0){ printf("\n inet_pton error occured\n"); return 1; } if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { printf("error connecting"); } char content[1024]; char *contenttemp="image_name=%s"; sprintf(content,contenttemp,imagename); char *headertemp="post %supload.php http/1.0\r\nhost: %s\r\ncontent-type: application/x-www-form-urlencoded\r\ncontent-length: %d\r\n\r\n%s"; sprintf(message,headertemp,service_path,service_host,strlen(content),content); write(sockfd,message,strlen(message));
can using way post image server (include name) ? suggest me ? ps: sorry english skill.
you including file name. have include whole image file contents post data stream. forms submitting binary data post
request should use multipart/form-data
content type. can't use application/x-www-form-urlencoded
type.
from html 4.01 specification:
the content type "application/x-www-form-urlencoded" inefficient sending large quantities of binary data or text containing non-ascii characters. content type "multipart/form-data" should used submitting forms contain files, non-ascii data, , binary data.
you adjust code this:
char *filename="file.jpg"; // example uses jpeg // optionally load file filesystem // though think have in buffer, don't you? file *file = fopen(filename, "rb"); char binary[1024]; // adjust buffer size needs size_t filesize = fread(binary, 1, sizeof(binary), file); // check error here make sure read succeeded fclose(file); // multipart/form-data post header const char *headertemp = "post %supload.php http/1.0\r\n" "host: %s\r\n" "content-type: multipart/form-data; boundary=boundary\r\n" "content-length: %lu\r\n" "\r\n"; // first , part beginning const char *bodytemp = "--boundary\r\n" "content-disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "content-type: image/jpeg\r\n" "content-transfer-encoding: binary\r\n" "\r\n"; // , ending const char body2[] = "\r\n" "--boundary--\r\n"; char body1[1024]; // adjust buffer size needs // calculate body size, included in content-length header size_t body_size = strlen(body1) + strlen(body2) + filesize; snprintf(header, 1024, headertemp, service_path, service_host, body_size); snprintf(body1, 1024, bodytemp, filename); // should add checking each write return value write(sockfd, header, strlen(header)); write(sockfd, body1, strlen(body1)); write(sockfd, binary, filesize); write(sockfd, body2, strlen(body2));
after sending data should read server response, example:
while (1) { ssize_t result = recv(sockfd, response, sizeof(response), 0); if (result == 0) { break; } else if (result < 0) { perror("reading socket failed"); break; } printf("%s\n", response); } close(sockfd);
if close socket without waiting response server may complain , return error. should check if response confirms valid request.
Comments
Post a Comment