1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
int Send_to_DB_via_HTTP(string filename)
{
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* server address */
unsigned short echoServPort; /* server port */
char *servIP; /* Server IP address (dotted quad) */
char *url;
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
int first_form_len=0;
int second_form_len=0;
unsigned count = 0;
char* Traveller_output_buffer;
FILE * pFile;
long lSize;
size_t result;
char* buf;
pFile = fopen ( filename.c_str(), "rb" );
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
Traveller_output_buffer = (char*) malloc (sizeof(char)*lSize);
if (Traveller_output_buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (Traveller_output_buffer,1,lSize,pFile);
//ifstream file_to_send;
unsigned data_len;
servIP = "192.168.2.20";
echoServPort = 80;
url = "http://something.dyndns.org";
data_len = sizeof(char)*lSize;
/* Create a reliable, stream socket using TCP */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
cout << " socket () failed" << endl;
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servlP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
cout<< "Error Connecting to " << servIP << endl;
else
cout << "Connection Successfull to " << servIP << endl;
first_form_len += snprintf(
&buffer1[first_form_len],
sizeof(buffer1) - first_form_len,
"Content-Disposition: form-data; name=\"flat_file\"; filename=\"new.dat\"\r\n" );
first_form_len += snprintf(
&buffer1[first_form_len],
sizeof(buffer1) - first_form_len,
"Content-Type: text/plain\r\n\r\n" );
cout << first_form_len << endl;
//Start the form off with the boundary string
second_form_len += snprintf(
&buffer1[first_form_len+second_form_len],
sizeof(buffer1) - (first_form_len+second_form_len),
"%s%s\r\n",
"--",BOUNDARY_STRING );
second_form_len += snprintf(
&buffer1[(first_form_len+second_form_len)],
sizeof(buffer1) - (first_form_len+second_form_len),
"Content-Disposition: form-data; name=\"filename\"\r\n\r\n%s_auto_end\r\n",
syssn_no );
//End of multi-part form
second_form_len += snprintf(
&buffer1[(first_form_len+second_form_len)],
sizeof(buffer1) - (first_form_len+second_form_len),
"%s%s%s\r\n",
"--",BOUNDARY_STRING,"--" );
// Put together the headers for HTTP POST
count = snprintf(
&buffer[0],
sizeof(buffer),
"POST %s HTTP/1.0\r\n",
url );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Accept-Language: en-us\r\n" );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Content-Type: multipart/form-data; boundary=%s\r\n",
BOUNDARY_STRING);
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"User-Agent: Mozilla/3.01 (compatible; %s-%s/%d.%d.%02d r%d)\r\n");
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Host: %s\r\n",
servIP);
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Pragma: no-cache\r\n" );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"Content-Length: %d\r\n",
data_len+first_form_len+second_form_len );
count += snprintf(
&buffer[count],
sizeof(buffer) - count,
"\r\n" );
cout << buffer1 << endl;
string status;
//Send the "POST" header to the HTTP server.
if (send(sock, buffer, count, 0) == -1) {
//status = perror("send");
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
else
{
cout << "data Sent" << endl;
}
//Send the first part of the form data to the HTTP server.
if (send(sock, buffer1, first_form_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
//Now send the flat file
if (send(sock, Traveller_output_buffer, data_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on message send\n" << endl;
return -1;
}
//Send the last part of the form data to the HTTP server.
if (send(sock, buffer1+first_form_len, second_form_len, 0) == -1) {
//status = CK_Get_last_error();
cout << "Stats Worker: received socket error %d on header send\n" << endl;
return -1;
}
else
{
cout << "Data has been Sent Successfully" << endl;
}
int n = recv(sock, buf, sizeof(buf), 0);
while (n > 0) {
printf(buf);
n = recv(sock, buf, sizeof(buf), 0);
}
close(sock);
}
| |