30/09/2018, 23:01
truyền ảnh từ C/C++ client lên php server dùng http request
các pro cho em hỏi, muốn truyền 1 ảnh jpeg từ 1 client lên php server, code của em chạy ở local host thì được nhưng khi chuyển code lên host ip tĩnh free thì không chạy. em nghĩ là file .php trên host của em chua nhận được request từ client?
Đây là code client:
#include <iostream>
#include <winsock2.h>
#include <string>
#include <fstream>
#include "windows.h"
#include "stdio.h"
using namespace std;
#define PORT 80
//#define IP "themnx-001-site1.1tempurl.com"
#define IP "vitrafficesrc.freevnn.com"
//#define IP "127.0.0.1"
//#define RECEIVER "/myproject/createFD.php" //receive_data.php"
//#define RECEIVER "h:
oothome hemnx-001wwwsite1createFD.php"
#define RECEIVER "/createFD.php"
#define COMPNAME "compname"
#define PROGRAM "program"
#define FILENAME "file"
#define BOUNDARY "----------Evil_B0uNd4Ry_$"
#define DUMMY_DATA "Tung Anh Dep Trai"
#define DUMMY_FILE "TungAnh.jpg"
char* base64_encode(const char*,int,int*);
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static int mod_table[] = {0, 2, 1};
//------------------------------------
string constructBody(string args[2], string file[2]);
string readFile(string fileName);
//------------------------------------
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "WSAStartup failed.
";
system("pause");
return 1;
}
SOCKET dataSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
struct hostent *host;
host = gethostbyname(IP);//themnx-001-site1.1tempurl.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port=htons(80);
SockAddr.sin_family=AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
cout << "Connecting...
";
if(connect(dataSock,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
cout << "Could not connect";
system("pause");
return 1;
}
else cout<<"connected!!!"<<endl;
// initiate the socket!
// SOCKET dataSock;
// WSADATA wsaData;
// int error = WSAStartup(0x0202, &wsaData);
// if (error != 0) {
// WSACleanup();
// exit(1); // oh shit, this shouldn't happen!
// }
// // all internets, engage!
// SOCKADDR_IN target;
// target.sin_family = AF_INET;
// target.sin_port = htons(PORT);
// target.sin_addr.s_addr = inet_addr(IP);
// dataSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// if (dataSock == INVALID_SOCKET) {
// exit(1); // Houston, we have a problem!
// }
// connect(dataSock, (SOCKADDR*)&target, sizeof(target));
//if(connect(dataSock, (SOCKADDR*)&target, sizeof(target))!=0)
//{
// cout << "Could not connect";
// system("pause");
//return 1;
//}
string programNames[5][2] = {{"KULVERTOP", "Chrome"}, {"KULVERTOP", "Firefox"}, {"KULVERTOP", "InternetExplorer"}, {"KULVERTOP", "Opera"}, {"KULVERTOP", "Skype"}};
string file[2] = {FILENAME, "Default.txt"};
int a = sizeof(programNames)/sizeof(programNames[0]);
for (int i = 0; i < 1; i++) {
printf("Sending data for %s
", (programNames[i][1]).c_str());
string body = constructBody(programNames[i], file);
char header[1024];
sprintf(header, "POST %s HTTP 1.1
"
"Host: %s
"
"Content-Length: %d
"
"Connection: Keep-Alive
"
"Content-Type: multipart/form-data; boundary=%s
"
"Accept-Charset: utf-8
", RECEIVER, IP, strlen(body.c_str()), BOUNDARY);
int p = send(dataSock, header, strlen(header), 0);
if(p==-1){
cout<<"khong ket noi duoc den server"<<endl;
}
cout<<"p= "<<p<<endl;
int k = send(dataSock, body.c_str(), strlen(body.c_str()), 0);
cout<<"k= "<<k<<endl;
}
closesocket(dataSock);
WSACleanup();
}
string constructBody(string args[2], string file[2]) {
string body;
string body_former;
string CRLF = "
";
FILE* fpr;
char buf[1];
int ch;
int i,j;
int output_len;
char* data;
char* encode_data;
char* data_jpg;
fpr = fopen("anh2.jpg","rb");
if(fpr == NULL)
printf("Can not open the source file.
");
else
printf("Open source file successfully.
");
// first we add the args
body.append("--"+string(BOUNDARY)+CRLF);
body.append("Content-Disposition: form-data; name=""+string(COMPNAME)+"""+CRLF);
body.append(CRLF);
body.append(args[0]+CRLF);
body.append("--"+string(BOUNDARY)+CRLF);
body.append("Content-Disposition: form-data; name=""+string(PROGRAM)+"""+CRLF);
body.append(CRLF);
body.append(args[1]+CRLF);
// now we add the file
body.append("--"+string(BOUNDARY)+CRLF);
body.append("Content-Disposition: form-data; name=""+string(FILENAME)+""; filename=""+string(DUMMY_FILE)+"""+CRLF);
body.append("Content-Type: image/jpeg"+CRLF);
body.append(CRLF);
// body.append(DUMMY_DATA+CRLF);
i = 0;
while((ch = getc(fpr)) != EOF)
{
i++;
}
data_jpg = (char*)malloc(i);
cout<<"Dung luong anh: "<<i<<"
";
j = 0;
free(fpr);
fpr = fopen("anh2.jpg","rb");
while((ch = getc(fpr)) != EOF)
{
data_jpg[j++] = (char)ch;
}
data = base64_encode(data_jpg,i,&output_len);
free(data_jpg);
encode_data = (char*)malloc(output_len + 1);
// strcpy(encode_data,data);
for(int k = 0;k<output_len;k++)
encode_data[k] = data[k];
// cout<<"OK"<<"
";
encode_data[output_len] = ' ';
cout<<"Dung luong sau ma hoa:"<<output_len<<"
";
body.append(encode_data);
body.append(CRLF);
body.append("--"+string(BOUNDARY)+"--"+CRLF);
body.append(CRLF);
free(encode_data);
free(fpr);
return body;
}
char *base64_encode(const char *data, int input_length, int * output_length) {
*output_length = 4 * ((input_length + 2) / 3);
char *encoded_data = (char*)malloc(*output_length);
if (encoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (int i = 0; i < mod_table[input_length % 3]; i++)
encoded_data[*output_length - 1 - i] = '=';
return encoded_data;
}
Đây là code server:
<?php
/* ===== CONSTANTS ===== */
$ROOT_DIR = 'UPLOADS';
$COMPUTER_NAME = 'compname';
$PROGRAM = 'program';
$FILENAME = 'file';
$CHUNK_SIZE = 1024;
/* ===================== */
//=====================================
/**
Function that gets current time and formats it into pretty looking date
*/
function makeDate() {
return strftime('%Y-%m-%d, %H.%M');
}
//=====================================
// check here if the parameters are set. If it's not then it's safe to say some one is snooping around...
if (isset($_POST[$COMPUTER_NAME], $_POST[$PROGRAM], $_FILES[$FILENAME])) {
// construct a full path and create it
// $fullPath = $ROOT_DIR.'.$_POST[$COMPUTER_NAME].'.$_POST[$PROGRAM].'.makeDate();
//$fullPath = $ROOT_DIR.'.makeDate();
$fullPath = $ROOT_DIR;
//mkdir($fullPath, 0777, true);
// move the files and rename them as temporary
$filename = $_FILES[$FILENAME]['name'];
//move_uploaded_file(($_FILES[$FILENAME]['tmp_name']), $fullPath.'.$filename.'.tmp');
move_uploaded_file(($_FILES[$FILENAME]['tmp_name']), $fullPath.'.$filename.'.tmp');
// decode received files
$src = fopen($fullPath.'.$filename.'.tmp', 'rb');
$dst = fopen($fullPath.'.$filename, 'wb');
while (!feof($src)) {
//fwrite($dst, (fread($src, $CHUNK_SIZE)));
fwrite($dst, base64_decode(fread($src, $CHUNK_SIZE)));
//echo "dang ghi file";
}
fclose($dst);
fclose($src);
unlink($fullPath.'.$filename.'.tmp'); // remove the temp file after decoding it
echo 'OK!';
} else {
// if someone is snooping around...
echo '<html><center><img src="umad.jpg" /></center></html>';
// echo 'oh no :(';
}
//=====================================
?>
Có ai biết giải thích tại nguyên nhân, cách debug không ạ? có code thì càng tốt ạ.
hoặc có ai không dùng http mà chạy được cũng được ạ.
cảm ơn các pro nhiều!
Bài liên quan