ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
//g++ -o client client.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include <iostream>
using namespace std;
void make_request( http_client & client, method mtd )
{
client.request(mtd, "/restdemo").then([](http_response response)
{
cout << __LINE__<<" here in get request \n";
if (response.status_code() == status_codes::OK)
{
cout << __LINE__<<" here in get request \n";
cout << response.body();
}
else
{
cout << __LINE__<<" here in get request \n";
}
return pplx::task_from_result(json::value());
}).then([](pplx::task<json::value> previousTask)
{
}).wait();
}
int main()
{
http_client client( "http://localhost" );
make_request( client, methods::GET );
return 0;
}
server code:
Code:
//g++ -o server server.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
#include <iostream>
using namespace std;
void handle_get(http_request request)
{
cout << __LINE__<<" here in get request \n";
request.reply(status_codes::OK, "GET Method result ");
}
int main()
{
http_listener listener("http://localhost/restdemo");
listener.support(methods::GET, handle_get);
try
{
listener
.open()
.then([&listener]() { cout<<"\nstarting to listen\n"; })
.wait();
while (true);
}
catch (exception const & e)
{
cout << e.what() << endl;
}
return 0;
}
I am seeing server is not responding for get method.
1. Can someone help where I am doing wrong ?
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?
I refered this link.
written small client and server code Client code:
Code:
//g++ -o client client.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_client.h>
using namespace web;
using namespace web::http;
using namespace web::http::client;
#include <iostream>
using namespace std;
void make_request( http_client & client, method mtd )
{
client.request(mtd, "/restdemo").then([](http_response response)
{
cout << __LINE__<<" here in get request \n";
if (response.status_code() == status_codes::OK)
{
cout << __LINE__<<" here in get request \n";
cout << response.body();
}
else
{
cout << __LINE__<<" here in get request \n";
}
return pplx::task_from_result(json::value());
}).then([](pplx::task<json::value> previousTask)
{
}).wait();
}
int main()
{
http_client client( "http://localhost" );
make_request( client, methods::GET );
return 0;
}
server code:
Code:
//g++ -o server server.cpp -lcpprest -lpthread -lcrypto -lboost_system
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
#include <iostream>
using namespace std;
void handle_get(http_request request)
{
cout << __LINE__<<" here in get request \n";
request.reply(status_codes::OK, "GET Method result ");
}
int main()
{
http_listener listener("http://localhost/restdemo");
listener.support(methods::GET, handle_get);
try
{
listener
.open()
.then([&listener]() { cout<<"\nstarting to listen\n"; })
.wait();
while (true);
}
catch (exception const & e)
{
cout << e.what() << endl;
}
return 0;
}
I am seeing server is not responding for get method.
1. Can someone help where I am doing wrong ?
Since you don't tell us anything about your environment, network topology, devices you're going in/out of, operating system, or even any message(s)/error(s) you're seeing, what you do you think we'll be able to tell you?? We can't guess. Have you tried checking any firewalls/iptables rules/etc?? Done ANY diagnostics at all??
..are you going to share your consulting fee with us for debugging your code?
Quote:
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?
Are you having load problems with the code? Is there a reason to change it? Asking "is there a better way?" will ALWAYS get you the answer of "yes", since no two programmers are going to agree on what the 'best' way is.
Since you don't tell us anything about your environment, network topology, devices you're going in/out of, operating system, or even any message(s)/error(s) you're seeing, what you do you think we'll be able to tell you?? We can't guess. Have you tried checking any firewalls/iptables rules/etc?? Done ANY diagnostics at all??
I felt environment don't have any thing to do. Because it is just localhost connectivity. However, I am using ubuntu.
Quote:
Originally Posted by TB0ne
You've been working with client/server things for years now:
I have been working only on raw sockets. I was working only in C Language. First time, started exploring and learning these kind of libraries and c++.
..are you going to share your consulting fee with us for debugging your code?
I was using that phrase "customer" in a general scenario. I don't have any customer as of now. Once I start making money, I will definitely consider paying. This is a very beautiful platform and I learned a lot and I am sure many persons would have and will be keep on learning here.
Quote:
Originally Posted by TB0ne
Are you having load problems with the code? Is there a reason to change it? Asking "is there a better way?" will ALWAYS get you the answer of "yes", since no two programmers are going to agree on what the 'best' way is.
yes. please have a look at the following (output of top):
While learning, I have habit of writing small pieces of code and make it work. when I have doubts, I keep posting like this. so that viewer may feel easy to understand what I am doing.
I felt environment don't have any thing to do. Because it is just localhost connectivity. However, I am using ubuntu.
What VERSION of Ubuntu??
Quote:
I have been working only on raw sockets. I was working only in C Language. First time, started exploring and learning these kind of libraries and c++.
Sorry, no...this is splitting hairs. Again, after working with such things for years, you should have a pretty good idea what's going on, especially with code YOU WROTE.
Quote:
I was using that phrase "customer" in a general scenario. I don't have any customer as of now. Once I start making money, I will definitely consider paying. This is a very beautiful platform and I learned a lot and I am sure many persons would have and will be keep on learning here.
Sorry...just don't believe it. Or do you expect us to believe you're continuing to work on things for free??? And again, no one here is going to take payment since we are VOLUNTEERS. The point is, it's plain rude of you to get a job working for someone, then ask others to do it for you, for free. YOU, personally, have to show some effort.
Quote:
yes. please have a look at the following (output of top):
Seriously? You do realize that if your program is the only thing running, it will be the ONLY THING using CPU, right????
Quote:
While learning, I have habit of writing small pieces of code and make it work. when I have doubts, I keep posting like this. so that viewer may feel easy to understand what I am doing.
Great; you've been doing this for years now, and you STILL haven't answered any of the questions asked of you in the first reply. Again, we are not going to guess, download/compile your code to figure out something, debug your code for you, or run your code to try to answer you. You, personally, have to provide information.
AGAIN:
Tell us about your environment
Tell us about the network topology
Tell us about the devices you're going in/out of
Tell us any message(s)/error(s) you're seeing
Tell us anything about the port, protocol, etc., that your code is using
Show us any diagnostics you've done, such as wireshark traces, tcpdumps, system logs, etc.
And AGAIN, have you tried checking any firewalls/iptables rules/etc?? And have you bothered to check your system for anything else running on the same port?
...
I am seeing server is not responding for get method.
1. Can someone help where I am doing wrong ?
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?
Is the server receiving the request?
Have you tested that? How?
Is the listener port actually accessible?
This is why others may need to know something about your environment.
The while(true); statement simply holds the server in the listen state until an exception is thrown by the listener causing it to exit. Whether there is a "better" way to do that depends on the specification of the server, what it is intended to accomplish, trivial though it may be, and of course its runtime environment.
Quote:
Originally Posted by chakka.lokesh
I was using that phrase "customer" in a general scenario. I don't have any customer as of now. Once I start making money, I will definitely consider paying.
I think the question was rhetorical, not an actual suggestion to make any kind of payment. To be very clear, it is NEVER acceptable to request or offer payment for help in the LQ forums.
It may be helpful to review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help. Remember, the better you understand your problem and frame your questions, the better others may understand it and offer help.
Let's keep the discussion constrained to the question asked, please.
Ubuntu version don't have any thing to do here. As long as compilation and execution happens(means required libraries are there), it is enough. This I am sure of.
Quote:
Originally Posted by TB0ne
Sorry, no...this is splitting hairs. Again, after working with such things for years, you should have a pretty good idea what's going on, especially with code YOU WROTE.
I didn't write this code. Just I copied from the link(you can check the question)
Quote:
Originally Posted by TB0ne
Seriously? You do realize that if your program is the only thing running, it will be the ONLY THING using CPU, right????
Obviously there will be a lot of processes running. But this is single threaded and it is consuming the entire one core of CPU. Other CPU cores are free.
Great; you've been doing this for years now, and you STILL haven't answered any of the questions asked of you in the first reply. Again, we are not going to guess, download/compile your code to figure out something, debug your code for you, or run your code to try to answer you. You, personally, have to provide information.
Quote:
Originally Posted by TB0ne
Tell us about your environment
Tell us about the network topology
Tell us about the devices you're going in/out of
Tell us any message(s)/error(s) you're seeing
Tell us anything about the port, protocol, etc., that your code is using
Show us any diagnostics you've done, such as wireshark traces, tcpdumps, system logs, etc.
And AGAIN, have you tried checking any firewalls/iptables rules/etc?? And have you bothered to check your system for anything else running on the same port?
[/QUOTE]
Environment is
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
network topology is not having anything to do here. I am sure of this.
there are no devices going in/out
there are no error messages
because it is localhost communication, firewalls/iptables don't have anything to do here. I am sure of this.
wireshark -- oh yeah, SORRY I entirely forgot about it. Have a look at the attachment. I am wondering it is with full of errors.
so connection is happening but call backs are not triggering on server side. on client side, return status is not "status_codes::OK"
Not able to understand why handle_get is not being executed on server side and on client side, why response code is not status_codes::OK
also, surprisingly all captured packets are having wrong checksums, wrong length fields.
Last edited by chakka.lokesh; 05-29-2021 at 05:59 AM.
Ubuntu version don't have any thing to do here. As long as compilation and execution happens(means required libraries are there), it is enough. This I am sure of.
Really?? So there are NO DIFFERENCES in the libraries that you're using/linking to between versions???
Quote:
I didn't write this code. Just I copied from the link(you can check the question)
No thanks..not going to chase down other websites. Again, you seem to be asking us to download/compile code so we can debug it for you. And for someone who has been programming for several years, it's odd you can't debug it.
Quote:
Obviously there will be a lot of processes running. But this is single threaded and it is consuming the entire one core of CPU. Other CPU cores are free.
Right; see previous comment.
Quote:
Environment is
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 21.04
Release: 21.04
Codename: hirsute
There a reason you couldn't/didn't provide this at the start??
Quote:
network topology is not having anything to do here. I am sure of this.
there are no devices going in/out
there are no error messages
because it is localhost communication, firewalls/iptables don't have anything to do here. I am sure of this.
Really?? Have you actually CHECKED the rules on your system? Again, what port is this running on and is there anything ELSE running on it?? Again, answer the questions asked previously.
Quote:
wireshark -- oh yeah, SORRY I entirely forgot about it. Have a look at the attachment. I am wondering it is with full of errors.
Don't wonder; it is.
Quote:
so connection is happening but call backs are not triggering on server side. on client side, return status is not "status_codes::OK"
Not able to understand why handle_get is not being executed on server side and on client side, why response code is not status_codes::OK
also, surprisingly all captured packets are having wrong checksums, wrong length fields.
You still don't tell us what you're typing in, or what you're seeing in response. You still aren't telling us much, but for a programmer with several years experience, and a fully documented/explained program, you should have what you need.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.