I am attempting to send an HTML page over a WebSocket using C++
EDIT: I've updated the response header to **not** specify
how many characters are being sent via char response[] = "...";
I've added to the header the Content-Type and Content-Length headers
with length value being 1024. The result is still the same.
The server sends the response to the web page client and the
web page prints the header information instead of using the HTML tags.
I'm aware C++ isn't exactly a preferred language for this, but it's the language I have to use.
I have already managed to "establish?" a connection with a client webpage in google chrome. The server is getting the initial request from the client and is able to send back a response that the client is accepting. It's just displaying the wrong part.
Request Header:
- Host: hostinfo.com:8xxx
- Connection: keep-alive
- Upgrade-Insecure-Request: 1
- DNT: 1
- User-Agent: Mozilla/5.0 (chrome etc...)
- Accept: (encrypted message)
Server Response:
- HTTP/1.1 200 OK
- Upgrade-Insecure-Request: 1
- Connection: keep-alive
- Content-Length: 1024
- Content-Type: text/html
- (space for separation of header and HTML info using \r\n\r\n (space after n))
- test
- (and the end page response \r\n\r\n (space after n)
However, when the webpage connects, instead of displaying "test" it's displaying the entire set of characters in the header including "HTTP/1.1 200 OK... etc"
I'm new to HTTP protocols and handshakes, I've gotten to this point in just a few days using articles like this, and this, and reading the RFC indexes but there doesn't seem to be any answers for this specific problem. I could understand handshake errors that resulted in a failure to send data between the server and client but sending the header instead of the HTML code is beyond me. Thanks for any help/insight.
TLDR: Why would the client be displaying the header information rather than using the HTML code to display an HTML page?
Client-Side HTML:
<form action="http://xxxxxxx.com:8xxx" method="POST">
<div>
<input>
</div>
<div>
<button>Test Connection</button>
</div>
</form>
Server-Side C++ Code: Header File
#pragma once
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
class html_connection
{
public:
static int client_handler_thread(int index);
static int server();
};
Server-Side C++ Code: Class File
#include "pch.h"
#include "html_connection.h"
#pragma warning(disable : 4996)
SOCKET Connections[100]; // client threads (up to 100)
int ConnectionCounter = 0;
int html_connection::server()
{
WSADATA ws;
WORD dllV = MAKEWORD(2, 1);
if (WSAStartup(dllV, &ws) != 0)
{
MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
}
SOCKADDR_IN addr; // create an address
int addrSize = sizeof(addr); //length of address
addr.sin_addr.s_addr = inet_addr("xxx.x.x.xx"); //Broadcast IP
addr.sin_port = htons(8000); //Port
addr.sin_family = AF_INET; // IPv4 socket
SOCKET s = socket(AF_INET, SOCK_STREAM, NULL); // listener socket
bind(s, (SOCKADDR*)&addr, sizeof(addr)); // binds address to socket
listen(s, SOMAXCONN); // max possible listeners
SOCKET newC; // client socket
for (int i = 0; i < 100; i++) // ensures limited number of clients can connect
{
newC = accept(s, (SOCKADDR*)&addr, &addrSize); // connect
if (newC == 0) // if client con failed
{
std::cout << "connect failed" << std::endl;
}
else // connected
{
Connections[i] = newC; // store new connection in array of connections
ConnectionCounter += 1;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)client_handler_thread, (LPVOID)(i), NULL, NULL);
}
}
return 0;
}
int html_connection::client_handler_thread(int index)
{
// buffer for the client request
char clientInput[256];
// Response to the client
char response[] = "HTTP/1.1 200 OK \r\n Connection: keep-alive \r\n Content-Length:1024 \r\n Upgrade-Insecure-Requests: 1 \r\n Content-Type: text/html \r\n \r\n\r\n <html> </html> \r\n\r\n ";
while (true)
{
// gets the request from the client and prints it to the console
recv(Connections[index], clientInput, sizeof(clientInput), 0);
std::cout << clientInput << std::endl;
// send server response
send(Connections[index], response, sizeof(response), 0);
}
closesocket(Connections[index]);
}
Output: Client - Webpage (initial page - pre-request)