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";
}
}
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);
Maybe try restructuring the string
arr="HTTP/1.1 200 OK\nContent-Type:text/html\nContent-Length: 16\n\n<h1>testing</h1>"
Related
This question already has an answer here:
Differ between header and content of http server response (sockets)
(1 answer)
Closed 1 year ago.
I'm making this socket HTTP client (very basic). When recv()'ing response data from example.com it works fine and writes it all to a buffer but when I try to revc any bigger amounts of data it stops at around 1500 bytes.
Right now all I'm trying to do is get the response written into the buffer (headers and all). Not trying to parse anything. But that isn't working. It works for a few iterations but then stops or hangs. I'm asking for help identifying the issue with this receive_response() function that causes these behaviors.
This is the function that revc's the HTTP response:
void tcp_client::receive_response(char *buffer) {
int bytes_recv = 0;
int total_bytes_recv = 0;
for (;;) {
bytes_recv = recv(sock, &buffer[total_bytes_recv], CHUNK_SIZE, 0);
if (bytes_recv <= 0) {
break;
} else {
total_bytes_recv += bytes_recv;
}
}
}
The main function:
int main(int argc, char **argv) {
http_client http;
char response[100000] = {0};
http.connect_to_host("go.com", 80);
http.send_request("GET / HTTP/1.1\r\n\r\n");
http.receive_response(response);
std::cout << response << std::endl;
return 0;
}
Thank you
You seem to expect the server to close the connection after the response is transmitted. A typical HTTP 1.1 server doesn't do that by default; they keep the connection open for further requests, unless the client explicitly asks otherwise via Connection: close header.
So, you receive all the data, and then the next recv call is sitting there, waiting for more data to arrive.
An HTTP 1.1 client is expected to detect the end of response via Content-Length header, or by decoding a chunked response as indicated by Transfer-Encoding: chunked header.
I am trying to send GET request to nodejs server from a C++ client.
nodejs server:
const server = http.createServer((request, response) => {
console.log(request.url);
response.end("received");
})
and here is my C++ clients:
while(getline(cin, random_input)) {
int s_len;
input = "GET / HTTP/1.1\r\n\r\n";
s_len = send(sock, input.c_str(), input.size(), 0);
if( s_len < 0)
{
perror("Send failed : ");
return false;
}
cout<<socket_c.receive(1024);
}
string tcp_client::receive(int size=512)
{
char buffer[size];
string reply;
int r_len; // received len
//Receive a reply from the server
r_len = recv(sock, buffer, sizeof(buffer), 0);
if( r_len < 0)
{
puts("recv failed");
}
if(buffer[r_len-1] == '\n') {
buffer[r_len-1] = '\0';
} else {
buffer[r_len] = '\0';
}
reply = buffer;
return reply;
}
so the C++ client can send GET requests each time when it's typing something in the terminal.
It works pretty fine if I type something right after the connection has been established. However, if I wait for 15-30 seconds after establish the connection, then type something on the client program, although the number of byte s_len that has been sent is correct, the server could't received anything.
May I know what goes wrong?
A few errors I spotted:
send return value is not checked correctly. Condition input.size() == s_len must be true.
recv return value is not checked of EOF. It treats r_len of 0 as valid data instead of disconnect. This may be the reason you do not see server replies: it may have disconnected but you did not notice that.
Setting the value of keepAliveTimeout of the node.js server to 0 could solve the problem
I'm just starting with RESTful programming and trying to make a program in c++ using the Casablanca sdk (https://github.com/Microsoft/cpprestsdk). I know that I need to use GET, POST, PUT and DEL methods to do data transfer etc. But I cant seem to find any examples on how to do this. I currently need to send an integer value to the server from the client and get a Boolean response from the server. I cant find any good examples in Casablanca's documentation or the web. Any help regarding how to do this simple transfer would be appreciated.
Spending more time to explore the documentation and various examples on the internet would probably have got you the answer.
Basically, you have to set up a http listener, as the server, that will listen to client request at a particular url.
Then a client can send data on that url, to communicate with it.
Nevertheless, if you want to exchange data in json format,
Server would look something like this
void handle_post(http_request request)
{
json::value temp;
request.extract_json() //extracts the request content into a json
.then([&temp](pplx::task<json::value> task)
{
temp = task.get();
})
.wait();
//do whatever you want with 'temp' here
request.reply(status_codes::OK, temp); //send the reply as a json.
}
int main()
{
http_listener listener(L"http://localhost/restdemo"); //define a listener on this url.
listener.support(methods::POST, handle_post); //'handle_post' is the function this listener will go to when it receives a POST request.
try
{
listener
.open() //start listening
.then([&listener](){TRACE(L"\nstarting to listen\n");})
.wait();
while (true);
}
catch (exception const & e)
{
wcout << e.what() << endl;
}
}
Client would be,
int main()
{
json::value client_temp;
http_client client(L"http://localhost");
//insert data into the json e.g : json::value(54)
client.request(methods::POST, L"/restdemo", object)
.then([](http_response response)
{
if (response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([&client_temp ](pplx::task<json::value> previousTask)
{
client_temp = previousTask.get();
})
.wait();
}
Your server reply will be stored into 'client_temp'
Im trying to make a TCP/IP client using boost::asio in C++. I have created function that creates a socket to connect to the server
void TCP_IP_Communication::Create_Socket()
{
...
_socket = new tcp::socket(_io); //create socket
_io.run();
boost::system::error_code error= boost::asio::error::host_not_found;
try
{
while (error && endpoint_iterator != end) //if error go to next endpoint
{
_socket->close();
_socket->connect(*endpoint_iterator++, error);
}
//if error throw error
if(error)
throw boost::system::system_error(error);
//else the router is connected
boost::asio::read_until(*_socket,buf,'\n');
}}
Then I use another function/thread to send a command and receive response.
try
{
if(p=='i')
_socket->send(boost::asio::buffer(" sspi l1\n\n")); //sending signal presence for input command
else
_socket->send(boost::asio::buffer(" sspo l1\n\n")); //sending signal presence for output command
for(; ;) //loop reading all values from router
{
//wait for reply??
boost::asio::read_until(*_socket,buf,'\n');
std::istream is(&buf);
std::getline(is,this->data);
std::cout<<std::endl<<this->data;
The problem is, each time I connect to the server it gives a response
? "login"
But I need to suppress it when I send a command. Actually it shouldn't show up as I'm not connecting to the server each time I send a command,but it does. What did I do wrong here? I just cant figure it out.
The main function is like this:
int main()
{
TCP_IP_Connection router;
router.Create_Socket();
boost::thread router_thread1,router_thread2;
router_thread1=boost::thread(&TCP_IP_Connection::get_status,&router,'i');
router_thread1.join();
std::string reply="\nend of main()";
std::cout<<reply;
return 0;
}
I have read the SSL TUNNELING INTERNET-DRAFT of December 1995 and set up an HTTP transparent proxy that works perfectly with unencrypted traffic.
Having read the above, as well as googled my brains out, the accepted method to create a tunnel for secure traffic through a proxy seems to be:
connect to the requested host, then have the proxy send an "HTTP 200..." confirmation message back to the client, then from that point on simply pass all further data traffic between client and server.
When I try this, however, the client (Chrome browser) responds to the "HTTP 200..." message with three wingdings characters which I forward to the remote host. At this point there is no response back and the connection fails.
Here is the code I am using for this, after having connected to the host:
if((*request=='C')&&(*(request+1)=='O')&&(*(request+2)=='N')&&(*(request+3)=='N'))
{
int recvLen;
send(output,htok,strlen(htok),0); //htok looks like "HTTP/1.0 200 Connection Established\nProxy-Agent: this_proxy\r\n\r\n"
std::memset(buff,0,bSize);
int total;
int bytes;
int n;
char cdata[MAXDATA];
while ((recvLen = recv(output, buff, bSize-1,0)) > 0) //recving from client - here we get wingdings
{
memset(cdata,0, MAXDATA);
strcat(cdata, buff);
while(recvLen>=bSize-1)//just in case buff is too small
{
std::memset(buff,0,bSize);
recvLen=recv(output,buff,bSize-1,0);
strcat(cdata, buff);
}
total = 0;
bytes = strlen(cdata);
cout << cdata << endl;//how I see the wingdings
while (total < strlen(cdata))
{
n = send(requestSock, cdata + total, bytes,0);//forwarding to remote host
if(n == SOCKET_ERROR)
{
cout << "secure sending error" << endl;
break;
}
total += n;
bytes -= n;
}
std::memset(buff,0,bSize);
recvLen=recv(requestSock, buff, bSize,0);//get reply from remote host
if (recvLen > 0)
{
do
{
cout<<"Thread "<<threadid<<" [Connection:Secure]: "<<recvLen<<endl;
send(output, buff, recvLen,0);//forward all to client
recvLen= recv(requestSock, buff, bSize,0);
if(0==recvLen || SOCKET_ERROR==recvLen)
{
cout<<"finished secure receiving or socket error"<<endl;
break;
}
}while(true);
}
}//end while, loop checks again for client data
Can anyone spot the error of my ways?
Your code is much more complicated than necessary. Just read into a char array, save the length returned, and write that many bytes from the same array, in a loop until recv() returns zero. 4 lines of code including two for the braces. Don't try to assemble the entire incoming message, just relay whatever comes in as it comes. Otherwise you are just adding latency, and programming errors. Get rid of all the strXXX() calls altogether.
I don't think you should make the assumption that the traffic does not contain ASCII NUL characters:
strcat(cdata, buff);
}
total = 0;
bytes = strlen(cdata);
If there are ASCII NULs in the stream, these will fail.