JJK ZH
JJK ZH

Reputation: 31

Send html through socket in c++

I' trying to send html through sockets in c++; after accept the request, my code will call the following function to send arr to client. When I test it on terminal with code of client in c, I received arr. However ,when I tried to connect to port through a web browser, my code shown connect success and send success, but nothing is printed out on browser, it just keep loading. why is that?

void sendTCP(int client_socket, vector<string> path)
    {
        cout<<"in sendTCP\n";
        //string web=conWeb(path);
        //cout<<"constructed web is "<<web<<endl;
        char arr[1024]="HTTP/1.1 200 OK\r\n\r\n<html>\n\r<body>\n\r\rhello\n\r</body>\n</html>";
        int send_res=send(client_socket,arr,sizeof(arr),0);
        if(send_res == -1)
        {
            perror("send");
        }
        else{
            cout<<"send success\n";
        }
    }

Upvotes: 0

Views: 2193

Answers (2)

agusbava
agusbava

Reputation: 363

This worked:

char arr[200]="HTTP/1.1 200 OK\nContent-Type:text/html\nContent-Length: 16\n\n<h1>testing</h1>";
int send_res=send(sock,arr,sizeof(arr),0);

Upvotes: 2

user9110300
user9110300

Reputation:

Maybe try restructuring the string

arr="HTTP/1.1 200 OK\nContent-Type:text/html\nContent-Length: 16\n\n<h1>testing</h1>"

Upvotes: 0

Related Questions