sockets: how to send html file to web browser that display it
Feb 5, 2010 at 11:14am Feb 5, 2010 at 11:14am UTC
What i have done so far:
I have coded a program that makes a socket and allows a client to connect to it. To connect to the program i type in the web browser "
http://localhost:8080/
I have tried to connect to this program with both firefox and IE. It works fine.
The part I need help with is how to send the html page to the webbrowser so that the browser displays it. I have tried to look at the binary section in:
http://www.cplusplus.com/doc/tutorial/files/
code in the tutorial
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
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.bin" , ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory" ;
delete [] memblock;
}
else cout << "Unable to open file" ;
return 0;
}
I tried to send the memblock variable with the sockets send() function but the browser wont react. I guess i am doing something wrong =( could you Pleeaas help me out here?
I have a large comment in the code that shows what part i need help with
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
#include <winsock2.h>
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
//WSASTARTUP
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0)
{
printf("error WSAStartup()\n" );
}
else
{
printf("WSAStartup() is ok!\n" );
}
//SOCKET
SOCKET socketOne;
socketOne = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketOne == INVALID_SOCKET)
{
printf("error socket()\n" );
}
else
{
printf("socket() is OK!\n" );
}
//BIND
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(8080);
SOCKET errorCheckingSocket;
errorCheckingSocket = bind(socketOne, (SOCKADDR*)&server, sizeof (server));
if (errorCheckingSocket == SOCKET_ERROR)
{
printf("bind() failed!\n" );
closesocket(socketOne);
return 0;
}
else
{
printf("bind() is OK!\n" );
}
//LISTEN
errorCheckingSocket = listen(socketOne, 5);
if (errorCheckingSocket == SOCKET_ERROR)
{
printf("error listen()\n" );
}
else
{
printf("listen() is OK!\n" );
}
//ACCEPT
SOCKET acceptSocket;
printf("Server: Waiting for a client to connect...\n" );
printf("***Hint: Server is ready...run your client program...***\n" );
while (1)
{
acceptSocket = SOCKET_ERROR;
while (acceptSocket == SOCKET_ERROR)
{
acceptSocket = accept(socketOne, NULL, NULL);
}
printf("Server: Client Connected!\n" );
socketOne = acceptSocket;
//**********************THIS IS THE PART I NEED HELP WITH*****************************
//what code to put in here so i can send the html file to the browser that is connected.
// I want the browser to display the html page
//************************************************************************************
break ;
}
cin.get();
return 0;
}
Last edited on Feb 5, 2010 at 11:16am Feb 5, 2010 at 11:16am UTC
Feb 5, 2010 at 2:39pm Feb 5, 2010 at 2:39pm UTC
A browser already has the page it needs to display. The client only sends requests, not the actual page to be displayed.
Topic archived. No new replies allowed.