LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2021, 07:04 AM   #1
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
cpprestsdk client/server not working


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 ?
2. I am not able to understand the purpose of while(true); Is there a better way of coding as it may waste CPU ?
 
Old 05-25-2021, 05:13 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by chakka.lokesh View Post
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??

You've been working with client/server things for years now:
https://www.linuxquestions.org/quest...rk-4175671314/
https://www.linuxquestions.org/quest...ts-4175643154/
https://www.linuxquestions.org/quest...ds-4175601968/

When you mention your 'customer':
https://www.linuxquestions.org/quest...3/#post6129848

..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.

Last edited by TB0ne; 05-25-2021 at 05:18 PM.
 
Old 05-28-2021, 07:09 AM   #3
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by TB0ne View Post
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 View Post
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++.

Quote:
Originally Posted by TB0ne View Post
When you mention your 'customer':
https://www.linuxquestions.org/quest...3/#post6129848

..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 View Post

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):

Code:
  16122 lokesh    20   0  408700   8560   7740 R 100.0   0.2   0:06.46 server_cpp
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.
 
Old 05-28-2021, 08:12 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by chakka.lokesh View Post
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):
Code:
  16122 lokesh    20   0  408700   8560   7740 R 100.0   0.2   0:06.46 server_cpp
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?
 
Old 05-28-2021, 01:10 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by chakka.lokesh View Post
...
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 View Post
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.

Last edited by astrogeek; 05-28-2021 at 02:36 PM.
 
1 members found this post helpful.
Old 05-29-2021, 05:35 AM   #6
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by TB0ne View Post
What VERSION of Ubuntu??
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 View Post
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 View Post
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 View Post
  • 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.
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2021-05-29 15-56-22.png
Views:	12
Size:	174.6 KB
ID:	36487  

Last edited by chakka.lokesh; 05-29-2021 at 05:59 AM.
 
Old 05-29-2021, 05:39 AM   #7
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by astrogeek View Post
Is the listener port actually accessible?
surprisingly, netstat is not showing anything about server. but client details are being displayed.

output of netstat -t -p

Code:
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        1      0 localhost:55380         localhost:http          CLOSE_WAIT  7568/./client
 
Old 05-29-2021, 06:30 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I'd suggest `netstat -tanp`
 
Old 05-29-2021, 01:49 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by chakka.lokesh View Post
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.

Last edited by TB0ne; 05-29-2021 at 01:56 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
yum working fine on server and ftp is working f9 but not able to config yum on client joj123 Red Hat 12 01-13-2015 06:03 PM
zmq client server program ..my client is working but server is not..pls help batman4 Programming 2 08-13-2012 07:44 AM
[SOLVED] zencafe - server to client pc, all working except client pc getting internt access captain_sensible Linux - Newbie 3 06-08-2010 10:37 AM
Nis Client On Centos not working with Suse Server . But works with Suse Nis Client jibinforu Linux - Server 2 07-23-2009 08:44 PM
Nis Client On Centos not working with Suse Server . But works with Suse Nis Client jibinforu Linux - Networking 1 07-13-2009 05:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration