C :: Unable To Send Binary Data Via HTTP Response
Feb 7, 2013
I'm trying to send some binary data, such as an animated gif, via an HTTP response for a simple web server.
I am having issues having the browser close the connection after receipt of the data.
I create the header such as:
"HTTP/1.1 200 OK
Content-Length: <file size>
Content-Type: <file type>
<binary data>"
This is stored in a malloc'ed char *. To add the binary data I'm using fread: fread(request + strlen(request), 1, size, fp)
I then write 'total' bits via connfd:
write(connfd, request, total)
where total = strlen(headerRequest) + size + 3 (for ending
).
The writing seems to be okay however the image doesn't load and the browser still seems to be waiting for data.
If I added "Connection: close" to the header and closed connfd myself the page loads fine. Obviously, for efficiency purposes, I'd rather only close connfd once read() returns 0.
The header creation code is below:
======= Code:
/* Try to get file */
strcat(cwd, file);
if(endsWith (cwd, ".html") || endsWith(cwd, ".htm")) {
strcpy(fileType, "text/html");
binary = 0;
} else if (endsWith(cwd, ".txt")) {
strcpy(fileType, "text/plain");
binary = 0;
[Code]...
View 7 Replies
ADVERTISEMENT
May 18, 2014
I make a simple c++ http post request using socket and it is working fine. But how to get only the message of the response. Here is my receive code:
char buffer[1028];
recv(socket, buffer, sizeof(buffer), 0);
printf("%s", buffer);
Then here is the response that the http send back.
HTTP/1.1 200 OK
Data: Mon, 19 May 2014 12:46:36 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/0.9.8y PHP/5.4.27
X-Powered-By: PHP/5.4.27
Coneten-Length: 28
Content-Type: text/html
All done! Do some stuff now. So what I want to try is get 3 things in this response and put them to string vector but how to do this. What I need is to parse the response and the 200 OK, 28 and All done! Do some stuff now. then put them to string vector.
expected result
response[0] = 200 OK;
response[1] = 28
response[2] = All done! Do some stuff now
View 2 Replies
View Related
Nov 19, 2013
Is there anyway to make when browser request some file from server than browser to load another file. Example:
I open [URL] ....
So now the browser send http request to load image [URL] ....
so when browser want to load that image to load this image [URL] ....
I do this now with [URL] ....
I think this can be done with Wininet.dll and WinHTTP.dll
View 3 Replies
View Related
Apr 9, 2013
I'm connecting to my website through sockets, like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <string.h>
int main(int argc, char *argv[]) {
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2,2), &wsadata) == -1)
[Code]...
I want to send GET to a certain web page. How can I do that?
View 2 Replies
View Related
Sep 3, 2014
I am writing a client-server program in C++ using C libraries. The server listens for a connection and when one is found, it is supposed to send a binary file(an image in my case) to the client. The server is sending the binary file perfectly after opening it and reading its contents into a buffer that is dynamically allocated in the SERVER file.
The problem begins when the client file is supposed to recv() the file, I do not know how I can allocate a buffer big enough for the file to be received. I know how to use malloc() and new, I prefer malloc() for executable size customization. Assuming the file being sent is 11000 bytes(note that this is an assumption and the client can not know the file size because the file size is dynamically generated by server).
How can the client generate a dynamically allocated buffer big enough to hold 11000 bytes?
OR
How can the client store the data in a buffer using recv() without knowing the file size.
Below is part of the code that does the send()'ing and recv()'ing.
SERVER
FILE *img = fopen("img.jpg", "rb");
fseek(img, 0, SEEK_END);
unsigned long filesize = ftell(img);
char *buffer = (char*)malloc(sizeof(char)*filesize);
rewind(img);
// store read data into buffer
[Code] ....
This outputs all the binary code perfectly but it is a bug because it can't be written to a file. Is there a way to dynamically increase the buffer until all filesize is received? Certainly, buffer needs to be dynamically allocated or program crashes ...
Also one more thing, when I telnet the server from command line, telnet displays all the output perfectly as well
Maybe telnet is storing data into a buffer behind the scenes, if so, how?
View 9 Replies
View Related
Oct 5, 2014
I found the following code in [URL] ....., that send .TXT files perfectly to php script in my server using Wininet, but when I insert a .BMP file, this file (.BMP) is correctly created and named in server side, but it is empty! I read that is necessary implement base64 encode for work properly, so how would?
PHP Code:
#include <windows.h>
#include <wininet.h>
#include <iostream>
#define ERROR_OPEN_FILE 10
#define ERROR_MEMORY 11
#define ERROR_SIZE 12
[Code] ....
View 12 Replies
View Related
Oct 6, 2014
My user requires that I add function to an already working C server that will send to another host via http post and existing xml file. I have googled and found C# and C++ on how to do this but nothing using C. Where do I start?
View 1 Replies
View Related
Dec 26, 2014
I have Build code for sending/receiving TCP Traffic over c Socket . Now I want t add HTTP Protocol to it .
After searching out on google ,one method I found which is by GET method to send a query to a server like google.com and request a page .
But ,Suppose I saved a HTML page in my directory how can I send HTTP packet via client/server in socket using this HTML page .
View 4 Replies
View Related
Dec 27, 2014
I have code for sending/receiving TCP traffic over C socket. Now I want to add HTTP protocol to it. After searching the web, one method I found which is by GET method to send a query to a server like google.com and request a page.
But, suppose I saved a HTML page in my directory how can I send HTTP packet via client/server in socket using this HTML page.
Server side code:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr,&clilen);
while (1)
[Code] .....
View 1 Replies
View Related
Jan 12, 2015
In my MainWindow I have a UserControl called CamAccessLevel1. This will be one of a few different UserControls which will have more and more functionality to a device as the Level goes up.
Each of those functionalities are themselves a UserControl, so that I don't have to keep remaking them for each different "Level" that I make.
So... I have a UserControl in a UserControl in my Mainwindow. My first goal is to have a button in one of those sections send an event back to the MainWindow to close the application. This sections is called "ProgramControl", as it has functions that relate to the program itself.
I can't seem to set a datacontext within another datacontext -> There is an error in UIViewModel below...
Note that I have replaced my company name with [snip]
In my Mainwindow.xaml
<views:CamAccessLevel1 x:Name="UserInterface"/>
Mainwindow.xaml.cs
public partial class MainWindow : Window {
#region MyModel
[code]....
View 10 Replies
View Related
Jun 11, 2014
I am trying to send the maximum long value through function call, but i didn't print proper value..
[
#include <stdio.h>
long long floatToInteger_old(float value){
printf("float val : %e",value);
}
int main(){
float value = 340168346567;
long long finalhexVal = floatToInteger_old(value);
return 0;}
]
my actual output is 340168346567, but it is printing 3.401683e+011 or 340168343552.000000.
how can print exact value what i am sending.
View 7 Replies
View Related
Apr 26, 2014
I have a situation where I am accessing an ASP.NET Generic Handler to load data using JQuery. But since data loaded from Javascript is not visible to the search engine crawlers, I decided to load data from C# and then cache it for JQuery. My handler contains a lot of logic that I don't want to apply again on code behind. Here is my Handler code:
public void ProcessRequest(HttpContext context) {
JavascriptSerializer jsonSerializer = new JavascriptSerializer();
string jsonString = string.Empty;
context.Request.InputStream.Position = 0;
using (var inputStream = new System.IO.StreamReader(context.Request.InputStream))
[Code] ....
I can call the function : getUserControlMarkup() from C# but I will have to apply some URL based conditions while calling it. The contentType.typeOfContent is actually based on URL parameters.
If possible to send JSON data to this handler then how to do that. I am trying to access the handler like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Common.host + "Handlers/SideContentLoader.ashx?typeOfContent=1&UCArgs=cdata");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
But its giving NullReferenceException in Handler code at line:
ContentType contentType = jsonSerializer.Deserialize<ContentType>(jsonString);
View 1 Replies
View Related
Sep 6, 2013
I am writing an MFC app in Visual Studio 2012 that will open a JPG file as binary and read all the contents to a CString.
I am able to read it to a std::Vector, but that doesn't work much as i need to pass all the binary content as a MFC CString to another function.
More Update:
Let me explain the problem a little more deeper.
I am trying to call a JavaScript (JS) function in a HTML page and then want to pass the binary date. The C++ function that calls the JS is given below.
bool CallJavaScript(const CString strFunc,CComVariant* pVarResult);
The 1st argument is the JS Function Name and 2nd is the one to pass the Binary Data. 1st argument works fine as i am able to call the JS fucntion called "LoadImage" without any problem. Problem is with 2nd argument that's supposed to take the Binary data of the JPG file.
If i try to pass a std::Vector or std::string then it will give me an error.
But it's happy if i pass CString. But then with CString there's a problem with NULL characters.
Actually my plan is to pass the binary of a JPG to a JS function and let it display the JPG in the HTML page.
Can i typecast a Vector or std::string to a CComVariant*?
View 3 Replies
View Related
Jan 16, 2014
Following is the code for inserting elements in a tree and then retrieving them back using inorder traversal technique elements are getting inserted just fine,but the code doesn't displays the elements of the tree while performing inorder traversal..
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *right;
struct node *left;
}*z,*t,*root=NULL,*x=NULL,*y=NULL;
[Code]....
View 2 Replies
View Related
Mar 6, 2015
how to send datapackets. When we send a data packet from device to another, say I have a "string". How Do I send it? Are data sent as a hex or binary number?If so do i first convert the "string" into hex or binary and then send it over the communication medium(say UART)??
I have two hex numbers to be sent using the UART, do i wrap it in some specific form and send it and retrieve it on the other side?Say if I have a data structure, in that case how do i send it? Sending data packets is very cruicial in an embedded system.
View 3 Replies
View Related
Oct 26, 2014
i am making a client server application but the problem is that i cannot send data from CLIENT to SERVER , but i can receive data from SERVER to CLIENT ... Below is mt client and Server code and i also have attached both client and server files...
CLIENT CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace asynclient {
[code]....
View 3 Replies
View Related
Sep 8, 2014
I'm trying to send a simple text string using httpsendrequest, everything works fine, the string is sent but I don't know how to retrieve it in the PHP server. Also when I use internetreadfile, the dwBytesRead is 0 meaning that there is no response from the server though the application works fine. This is the code that I have so far.
#define _WIN32_WINNT 0x600
#include <stdio.h>
#include <wininet.h>
#define BUFLEN 200
static const char *acceptTypes[] = {"application/x-www-form-urlencoded", NULL};
static const char *postData = "teststr=Hello+world&testval=42";
int main() {
HINTERNET hSession, hConnect, hFile;
[code].....
View 1 Replies
View Related
Mar 27, 2014
I am trying to make a streambuf for a socket, which will use either WinSock or POSIX sockets depending on OS. I understand about how to send and receive data via sockets, but I don't really understand how to put that into a 'streambuf'. What functions do I need to override?
View 7 Replies
View Related
Aug 13, 2014
private void button1_Click(object sender, EventArgs e) {
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
[Code]...
In my project i have a serialport control and a button, there is no error but i want to know how actually this works, i want to send and recieve the data from the serial port don't know how to do it..
View 9 Replies
View Related
Sep 4, 2014
Whats wrong with my code, I wasn't able to input for Lastname field.
#include <iostream.h>
#include <windows.h>
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For gotoXY() function
COORD CursorPosition;
[Code]....
View 1 Replies
View Related
Sep 12, 2013
I have these two pointers and I want to copy pointer1 data to pointer2. I tried to use memcpy and it did what I wanted but when I accessed pointer2 outside of the method that I did The memcpy in it didn't contain pointer1 data. Below is code similar to the code that I used:
CBaseInitBlock1 *pInitBlock1Saved = pSavedInitBlock1;// pointer1
CBaseInitBlock1 *pInitBlock1New = pNewInitBlock1;//pointer2
memcpy(&pInitBlock1New,&pInitBlock1Saved, sizeof(pInitBlock1Saved));
However, when doing the following pointer2 contained pointer1 data every time I accessed it:
*pInitBlock1New = *pInitBlock1Saved
At this point I wanted to you what is the best way to execute what I am trying to accomplish.
View 3 Replies
View Related
Mar 13, 2013
Is there any standard USB protocol which i can follow to send data to my embedded board(and vice versa). I have no clue on USB programming using c,is there any example code i could follow,
View 4 Replies
View Related
May 12, 2014
I am having trouble in reading data from my text file and print it out exactly like how it looks like in the text file. The problem im having is it will not read the last y Coordinates of the point. it keep reading the second last point for y coordinates which is wrong.
my text file is
0.0 0.0
0.0 1.0
1.0 0.0(but it read until here)
0.0 0.0(suppose to read the last point which is here)
For your information, this is my 1st year degree assignment in C programming. It is to create a program which can read text file (manually create) and print it out in a program and calculate the area for the polygon using ADT function ( .c and .h files)
*This is the code for my read file function*
Basically this accepts a Polygon and a file pointer as parameters. It reads the polygon point data from the file, pass the read data to plg_new() to create a new Polygon and returns the new Polygon created.
Code:
polygon *plg_read(polygon *New_polygon, FILE *Coord) {
int i;
int numberofvertices=0;
int count=0;
char filename[50];
double xCoor[50], yCoor[50];
[Code]....
This is the second function my polygon new code. This ADT function basically creates a new Polygon with malloc(), initialize all ADT data members with its parameter values and returns the Polygon.
Code:
polygon *plg_new(double *xCoordinates, double *yCoordinates, int numberOfVertices) {
int x;
polygon *New_poly = (polygon *)malloc(sizeof (polygon));
if(New_poly->xCoordinates == NULL || New_poly->yCoordinates == NULL) {
free(New_poly);
[Code]....
This is the rest of the code if you need to refer to other codings.
Code:
/**
*@file polygon.c
*@brief Functions for polygon / Struct has polygon numberOfVertices, polygon *xCoordinates and polygon *yCoordinates
*@author: Tan Xian Yao
*@id: 4323440
*@date: 22/04/2014
*/
[Code]...
View 6 Replies
View Related
Apr 27, 2014
I am new to C++ and I am having an issue reading in a 2 Dimensional array from a data file. I am very close to reading it in perfectly except for one issue, the loop is ignoring the first value from the data file.
This is the code I have so far:
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <functional>
#include <conio.h>
#include <iomanip>
#include <string>
#include <fstream>
[Code]...
This is the values from the data file:
300 450 500 210
510 600 750 400
627 100 420 430
530 621 730 530
200 050 058 200
100 082 920 290
When the Program shows my array it comes up as:
450 500 210 510
600 750 400 627
100 420 430 530
621 730 530 200
050 058 200 100
082 920 290 0
Every array location is moved up one, like the program just completely ignored the 300 at the beginning.
View 1 Replies
View Related
Apr 14, 2014
I'm having a problem trying to modify my patient's data. It seems to work, but once the block of code ends it does not save to the linked list.
Problem located in case M.
linked list template header: [URL] ...
patient header: [URL] ...
patient implementation: [URL] ...
#include <iostream>
#include "listTemplate.h"
#include "PatientRecord.h"
using namespace std;
char menu() {
char input
[Code]...
View 1 Replies
View Related
Mar 26, 2013
I'm having issues trying to input data from a .txt file into my array program. The whole program takes the name, # of goals and assists, adds them together to get points, and then puts in a given + or - rating. The program works except for the data input.
Also, this is an alphabetical "Unsorted" list, but is there a way to order the list based on point value and be able to display that as well as a separate "Ordered" list?
#include <iostream>
#include <iomanip>
#include <fstream>
#define NUM_PLAY 30
using namespace std;
int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
[Code]...
And this is the .txt file. Order is Players Name, Goals, Assists, Plus/Minus rating. The points are added when points = goals + assists (Should be in the main coding above).
Bryan_Bickell 2 5 +2
Brandon_Bollig 0 0 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 0 -1
Daniel_Carcillo 0 1 +3
[Code]...
View 5 Replies
View Related