ACE c++ send_n and recv_n not working as expected - c++

I am trying to implement a basic client-server program using ACE, but the problem is that if I send from client and receive it from server, it works properly. But, when I am increasing the complexity i.e., multiple sends and recvs the programs isn't working. Any help is appreciated.
Here's the client code.
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "iostream"
using namespace std;
int main(){
ACE_INET_Addr server(7128);
ACE_SOCK_Connector cli;
ACE_SOCK_Stream cstream;
if(cli.connect(cstream, server)==-1){perror("Error");}
char buff[100];
if(-1==cstream.recv_n(buff, 7)){printf("Error");}
cout<<buff<<endl;
cstream.send_n("FU", 3);
}
Here's the server code
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include "iostream"
using namespace std;
int main(){
ACE_INET_Addr server(7128);
ACE_INET_Addr client;
ACE_SOCK_Acceptor cli(server);
ACE_SOCK_Stream cstream;
if(cli.accept(cstream, &client)==-1){perror("Error");}
char buff[100];
if(-1==cstream.send_n("RAND", 5)){printf("Error");}
cstream.recv_n(buff, 3);
}

Your server is sending 5 bytes but your client is expecting to read 7. The client is blocked waiting for 2 more bytes that will never come.

Related

How do I write a minimal GraphQL query of the Axie server?

I'm writing a program to download data about Axies and process them. My plan is to download all the marketplace, getting just the index numbers, then download details about Axies. Before getting all the details about an Axie, I'd like to get just one detail. I've succeeded in making an HTTPS connection to the server and sending a query, but all it replies is "Bad Request".
I've been using Shane Maglangit's site https://axie-graphql.web.app/ for examples, but the examples are too big for me to understand, since I don't know GraphQL or JSON, and part of the queries has literal \n and the other part has linefeeds, which is confusing me. His code is in JavaScript, which I don't know, so I don't know if JS is doing something different with \n than C++ does.
Here's my code:
main.cpp
#include <iostream>
#include <iomanip>
#include <boost/program_options.hpp>
#include "http.h"
using namespace std;
int main(int argc,char **argv)
{
string query="{\n \"operationName\": \"GetAxieDetail\",\n"
" \"variables\":\n {\n \"axieId\": \"5144540\"\n },\n"
" \"query\": \"query GetAxieDetail($axieId: ID!) {\\n ...AxieDetail\\n __typename}\n}"
"fragment AxieDetail on Axie{axie(axieId: $axieId)}\"";
string response;
string urlv2="https://axieinfinity.com/graphql-server-v2/graphql";
string urlv1="https://graphql-gateway.axieinfinity.com/graphql";
response=httpPost(urlv1,query);
cout<<response<<endl;
return 0;
}
http.h
#include <string>
std::string httpPost(std::string url,std::string data);
http.cpp
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/beast.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/asio/ssl/error.hpp>
#include <boost/asio/ssl/stream.hpp>
#include <array>
#include <iostream>
namespace beast=boost::beast;
namespace http=beast::http;
namespace net=boost::asio;
namespace ssl=net::ssl;
using tcp=net::ip::tcp;
using namespace std;
array<string,4> parseUrl(string url)
// protocol, hostname, port, path. All are strings, including the port.
{
size_t pos0=url.find("://");
size_t pos1;
array<string,4> ret;
ret[0]=url.substr(0,pos0);
if (pos0<url.length())
pos0+=3;
pos1=url.find("/",pos0);
ret[1]=url.substr(pos0,pos1-pos0);
ret[3]=url.substr(pos1);
pos0=ret[1].find(":");
if (pos0<ret[1].length())
{
ret[2]=ret[1].substr(pos0+1);
ret[1]=ret[1].substr(0,pos0);
}
else
if (ret[0]=="https")
ret[2]="443";
else if (ret[0]=="https")
ret[2]="80";
else
ret[2]="0";
return ret;
}
string httpPost(string url,string data)
{
net::io_context context;
ssl::context ctx(ssl::context::tlsv12_client);
tcp::resolver res(context);
tcp::resolver::results_type endpoints;
beast::ssl_stream<beast::tcp_stream> stream(context,ctx);
array<string,4> parsed=parseUrl(url);
http::request<http::string_body> req;
http::response<http::string_body> resp;
beast::flat_buffer buffer;
//load_root_certificates(ctx);
ctx.set_verify_mode(ssl::verify_peer);
endpoints=res.resolve(parsed[1],parsed[2]);
beast::get_lowest_layer(stream).connect(endpoints);
SSL_set_tlsext_host_name(stream.native_handle(),parsed[1].c_str());
if (parsed[0]=="https")
stream.handshake(net::ssl::stream_base::client);
req.method(http::verb::post);
req.target(parsed[3]);
req.set(http::field::host,parsed[1]);
req.set(http::field::user_agent,BOOST_BEAST_VERSION_STRING);
req.set(http::field::content_type,"application/json");
req.set(http::field::accept,"application/json");
req.body()=data;
req.prepare_payload();
http::write(stream,req);
http::read(stream,buffer,resp);
cout<<parsed[0]<<"|\n"<<parsed[1]<<"|\n"<<parsed[2]<<"|\n"<<parsed[3]<<"|\n";
cout<<data<<"|\n";
return resp.body();
}
How can I write a query that returns one detail of the Axie with the specified number? Which of the two Axie servers should I use, and what's the difference?
Here is a working query string:
string query="{\n"
" \"operationName\": \"GetAxieDetail\",\n"
" \"variables\":\n"
" {\n"
" \"axieId\": \"5144540\"\n"
" },\n"
" \"query\":\n"
" \"query GetAxieDetail($axieId: ID!)"
" {\\n"
" axie(axieId: $axieId)\\n"
" {\\n"
" class\\n"
" }\\n"
" }\"\n"
"}\n";
The response is:
{"data":{"axie":{"class":"Plant"}}}
The server insists on no line feeds in the quoted query string, but allows \n; the \n, though, makes no difference, as the response is just one line.

How to open and close DVD-RW with C++?

I want to apologize for bad English.
In my system, the CD drive is occupied by another device and the program code does not work correctly, what should I use to open and close the DVD-RW drive?
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <shellapi.h>
#include <mmsystem.h>
using namespace std;
int main()
{
MCI_OPEN_PARMS OpenParm;
MCI_SET_PARMS SetParm;
MCIDEVICEID dID;
OpenParm.lpstrDeviceType=L"CDAudio";
mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE, (DWORD_PTR)&OpenParm);
dID = OpenParm.wDeviceID;
mciSendCommand(dID, MCI_SET, MCI_SET_DOOR_OPEN, (DWORD_PTR)&SetParm);
Sleep(3000);
mciSendCommand(dID, MCI_SET, MCI_SET_DOOR_CLOSED, (DWORD_PTR)&SetParm);
mciSendCommand(dID, MCI_CLOSE, MCI_NOTIFY, (DWORD_PTR)&SetParm);
return 0;
}
My System
What use instead CDAudio?

Reading arduino serial in C++ using codeblocks on ubuntu

I tried using and editing the code from Read and Write on serial port in Ubuntu with C/C++ and LibSerial and referencing from the Ubuntu manpage http://manpages.ubuntu.com/manpages/saucy/man3/LibSerial_SerialStream.3.html.
When I use the serial monitor from the Arduino IDE it works fine. But when I wanted to read it using codeblocks with C++, all I got was some garbage values.
Here's the code:
#include <SerialStream.h>
#include <iostream>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define PORT "/dev/ttyUSB0"
using namespace std;
using namespace LibSerial;
...
SerialStreamBuf::BaudRateEnum baud_rate = SerialStreamBuf::BAUD_115200;
const SerialStreamBuf::CharSizeEnum csize = SerialStreamBuf::DEFAULT_CHAR_SIZE;
const SerialStreamBuf::ParityEnum parity = SerialStreamBuf::PARITY_NONE;
short stop_bits = 1;
const SerialStreamBuf::FlowControlEnum flow_c = SerialStreamBuf::FLOW_CONTROL_NONE;
...
SerialStream(PORT, baud_rate, csize, parity, stop_bits, flow_c);
SerialStream serial_port ;
serial_port.Open(PORT) ;
...
while( 1 ){
char next_byte;
serial_port.get(next_byte);
std::cerr << next_byte;
usleep(100);
}
How do I fix this? I'm not good in object oriented programming so I'm not so sure about initializing the constructors.
You're defining two variables for the serial stream, but using the uninitialized one. Try replacing this:
SerialStream(PORT, baud_rate, csize, parity, stop_bits, flow_c);
SerialStream serial_port ;
serial_port.Open(PORT) ;
With
SerialPort serial_port ;
serial_port.Open(PORT, baud_rate, csize, parity, stop_bits, flow_c);
and then use the ReadByte function.

zeromq: PULL/PUSH client crashes after upon connection of 31. server on windows

I'm experimenting with the PUSH/PULL pattern for distributed computing in a local network.
Up to now everything seemed to work out, however, I had o discover that upon the startup of the 31 worker (server) the client (the ventilator and the collector) application crashes.
Is there a limit for the connections to a certain port on windows (on MacOs X this seems not to be the case). I'm using tcp trans port and ports 5555 and 5556 as in the zeromq example. The behavior is observer for remote and local workers.
Thx
Update: heres the code (modified sample from the zmq guide)
#include <zmq.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <sstream>
int main (int argc, char *argv[])
{
zmq::context_t context(1);
int number_of_sockets=32; // 32 will crash, 30 will pass
zmq::socket_t** receiver=new zmq::socket_t*[number_of_sockets];
zmq::socket_t** sender=new zmq::socket_t*[number_of_sockets];
std::cout<< "go"<<std::endl;
for (int i=0;i<number_of_sockets; i++)
{
receiver[i]=new zmq::socket_t(context, ZMQ_PULL);
receiver[i]->connect("tcp://localhost:5555");
}
std::cout<< "ok"<<std::endl;
for (int i=0;i<number_of_sockets; i++)
{
sender[i]=new zmq::socket_t(context, ZMQ_PUSH);
sender[i]->connect("tcp://localhost:5556");
}
std::cout << "done" <<std::endl;
return 0;
}
I build using Mingw-w64-tdm (4.5) by the command:
g++ -o worker.exe taskworker.cpp -L/./lib -lzmq -L/./lib/zeromq/libzmq.la -liphlpapi -lrpcrt4 -lws2_32 -lpthread
Ok. I pinned it down to this issue here. The problem is, that on windows by default there is a FD_SETSIZE limit of 64. (This makes the code crash - actually for the 32. worker). FD_SETSIZE can be modified during building 0mq (CPPFLAG="-DFD_SETSIZE=1024").
Now the crashes are gone.

FastCGI and Apache and C++

I comliled C++ project for FastCGI, copy executable file into www directory, open via browser - and got 500 error (timeout exeption). What do i wrong?
OS Ubuntu 10.05, server: Apache
source C++ code:
#include <fcgi_stdio.h> /* fcgi library; put it first*/
#include <fcgiapp.h>
#include <cstdlib>
#include <iostream>
using namespace std;
int count;
int main(int argc, char** argv) {
/* Response loop. */
while (FCGI_Accept() >= 0) {
cout<<"Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i>\n";
}
return 0;
}
When you're in the FCGI_Accept() loop you should be reading data, not writing it.
Check out http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/apaman.htm