I have these two poco programs, one sends the other receives. The receive doesn't crash, but I can't get the send to work. Sadly, the exception is not very helpful either, just saying "Net Exception". It might have to do with running both programs on the same machine, but still, the reuse address is set to true and so is the loopback.
//Receive
#include <string>
#include <iostream>
// MulticastSocket receive example
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>
using namespace std;
int main(int argc, char* argv[])
{
Poco::Net::initializeNetwork();
std::cout << "1" << std::endl << std::flush;
//Poco::Net::SocketAddress localAddr(Poco::Net::IPAddress(), 1900);
Poco::Net::SocketAddress localAddr("239.255.255.250", 1900);
std::cout << "2" << std::endl << std::flush;
Poco::Net::MulticastSocket socket(localAddr, true);
std::cout << "3" << std::endl << std::flush;
socket.setLoopback(true);
std::cout << "4" << std::endl << std::flush;
socket.setTimeToLive(4);
std::cout << "5" << std::endl << std::flush;
Poco::Net::SocketAddress groupAddr("239.255.255.250", 1900);
std::cout << "6" << std::endl << std::flush;
socket.joinGroup(groupAddr.host());
std::cout << "7" << std::endl << std::flush;
Poco::Net::SocketAddress sender;
char buffer[512];
int n = socket.receiveFrom(buffer, sizeof(buffer), sender);
std::cout << buffer << std::endl << std::flush;
Poco::Net::uninitializeNetwork();
return 0;
}
//Send
#include <string>
#include <iostream>
#include <Poco/Net/SocketAddress.h>
#include <Poco/Net/MulticastSocket.h>
#include <Poco/Net/NetException.h>
using namespace std;
using namespace Poco;
int main(int argc, char* argv[])
{
Poco::Net::initializeNetwork();
try
{
Poco::Net::SocketAddress address("239.255.255.250", 1900);
//Poco::Net::SocketAddress address(Poco::Net::IPAddress(), 1900);
std::cout << "1" << std::endl << std::flush;
Poco::Net::MulticastSocket socket(address, true);
std::cout << "2" << std::endl << std::flush;
socket.setLoopback(true);
std::cout << "3" << std::endl << std::flush;
//Poco::Net::SocketAddress sender;
char buffer[512];
buffer[0] = 'H';
buffer[1] = '\0';
std::cout << "Sending " << buffer << std::endl;
//socket.sendTo(buffer, 512, sender);
socket.sendBytes(buffer, 512, 0);
std::cout << "4" << std::endl << std::flush;
}
catch(const Poco::Net::NetException& ex)
{
std::cout << ex.what() << std::endl << std::flush;
}
Poco::Net::uninitializeNetwork();
return 0;
}
You have to bind to the IP of your computer and not the IP of the multicast group. You have commented the correct line out:
Poco::Net::SocketAddress localAddr(Poco::Net::IPAddress(), 1900);
Try looking at the MulticastEchoServer example that comes with Poco. It is under Net/testsuite/src/MulticastEchoServer.cpp in the sources.
Related
I want to search argv[2] value in JSON but i couldn't do this. I looked the other threads but they couldn't hep either. Because i am a newbie. So ho can I do this?
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <json/value.h>
using namespace std;
// struct instead of class
struct Element {
int atomNumber;
string atomName;
string atomSymbol;
string atomType;
string atomSerie;
// void funciton instead of Book(){}
void printinfo() {
cout << "Atom Number: " << atomNumber << "\n";
cout << "Atom name: " << atomName << "\n";
cout << "Atom Symbol: " << atomSymbol << "\n";
cout << "Atom Type: " << atomType << "\n";
cout << "Atom Serie: " << atomSerie << "\n";
}
};
// Arg Count Args
// V V
int main(int argc, char* argv[]) {
Json::Value ptable;
std::ifstream ptable_file("ptable.json", std::ifstream::binary);
ptable_file >> ptable;
if (strcmp(argv[1], "-p") == 0){
};
return 0;
}
As previous questions have been answered, the way to pass a std::ofstream object as a function argument seems to be to instead pass a reference: std::ofstream&.
Whilst this solution compiles, the resulting output is not equivalent to creating an std::ofstream object within the method then calling write().
The code below does not give the correct output:
In main.cpp:
std::ofstream file(path + "output.stubs");
stub->writeRaw(file); //stub is a pointer to an object of class Stub
file.close();
In Stub.cpp:
void Stub::writeRaw(std::ofstream& file) {
file.write((char*)this, sizeof(*this));
}
The correct output is given by both changing Stub.cpp to:
void Stub::writeRaw(void) {
std::ofstream file(path + "output.stubs");
file.write((char*)this, sizeof(*this));
file.close();
}
or writing the object to the file in main instead of calling a class method.
Any help on this behaviour would be greatly appreciated!
EDIT
Some context for the class Stub:
Stub.hpp
#pragma once
#include <iostream>
#include <ios>
#include <fstream>
#include "constants.hpp"
#include "DataTypes.hpp"
class Stub {
private:
StubHeader header;
StubIntrinsicCoordinates intrinsic;
StubPayload payload;
public:
Stub(void);
virtual ~Stub(void);
StubHeader getHeader(void);
StubIntrinsicCoordinates getIntrinsicCoordinates(void);
StubPayload getPayload(void);
void setHeader(StubHeader stub_header);
void setIntrinsicCoordinates(StubIntrinsicCoordinates stub_intrinsic);
void setPayload(StubPayload stub_payload);
void print(void);
void writeRaw(std::ofstream& file);
};
And the relevant data types are defined as follows:
struct StubHeader {
uint8_t bx;
uint8_t nonant;
};
struct StubIntrinsicCoordinates {
uint8_t strip;
uint8_t column;
int crossterm;
};
struct StubPayload {
bool valid;
int r;
int z;
int phi;
int8_t alpha;
int8_t bend;
uint8_t layer;
bool barrel;
bool module;
};
EDIT 2
The (toy) code to read the stub is as follows:
std::ifstream r(path + "output.stubs");
Stub s;
r.read((char*)&s, sizeof(s));
s.print();
Only one stub is written to the file as this was a test of functionality. The print function for the Stub class is as follows:
void Stub::print(void) {
std::cout << "----- Header -----" << '\n';
std::cout << "bx: " << std::dec << (int)header.bx << '\n';
std::cout << "nonant: " << std::dec << (int)header.nonant << '\n';
std::cout << "----- Intrinsic Coordinates -----" << '\n';
std::cout << "strip: " << std::dec << (int)intrinsic.strip << '\n';
std::cout << "column: " << std::dec << (int)intrinsic.column << '\n';
std::cout << "crossterm: " << std::dec << (int)intrinsic.crossterm << '\n';
std::cout << "----- Payload -----" << '\n';
std::cout << "valid: " << std::boolalpha << payload.valid << '\n';
std::cout << "r: " << std::dec << (int)payload.r << '\n';
std::cout << "z: " << std::dec << (int)payload.z << '\n';
std::cout << "phi: " << std::dec << (int)payload.phi << '\n';
std::cout << "alpha: " << std::dec << (int)payload.alpha << '\n';
std::cout << "bend: " << std::dec << (int)payload.bend << '\n';
std::cout << "layer: " << std::dec << (int)payload.layer << '\n';
std::cout << "barrel: " << std::boolalpha << payload.barrel << '\n';
std::cout << "module: " << std::boolalpha << payload.module << "\n\n";
}
EDIT 3
For completeness and transparency, please find below the exact code for main.cpp:
int main(int argc, char const *argv[]) {
Geometry g;
g.generateModuleLUTs();
g.generateCorrectionLUTs();
std::vector<std::array<Stub*, PAYLOAD_WIDTH> > all_stubs;
std::vector<Module> modules = g.getData();
for (int i = 0; i < LINK_NUMBER; i++) {
LinkGenerator link_gen;
LinkFormatter link_formatter(link_gen.run());
StubFormatter stub_formatter(link_formatter.run(), i);
std::array<Stub*, PAYLOAD_WIDTH> stubs = stub_formatter.run(modules);
CoordinateCorrector coordinate_corrector(stubs);
all_stubs.push_back(coordinate_corrector.run());
}
std::ofstream f(path + "output.stubs");
all_stubs[0][0]->writeRaw(f);
all_stubs[0][0]->print();
std::ifstream r(path + "output.stubs");
Stub s;
r.read((char*)&s, sizeof(s));
s.print();
return 0;
}
The bug in the code was that I was not calling file.close() before constructing the std::ifstream object to read the file again. This was the cause of the unexpected behaviour.
Writing a class to file using this seems to be valid, although it is important that you are careful and know exactly what you want to write to a file.
Thank you to everyone who commented and helped to answer this question!
Currently I am trying to learn SFML networking but I am having a problem with the client (I think).
#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
int main()
{
sf::TcpSocket sock;
sf::Packet backpack;
sf::Thread Thread;
std::cout <<"Attempting to connect to server" << std::endl;
sf::Socket::Status status = sock.connect("127.0.0.1", 25568);
if (status != sf::Socket::Done){
std::cout << "Could Not Connect to server" << std::endl;
} else {
std::cout << "Connected to server" << std::endl;
}
backpack << 9;
std::cout << "Sending data" << std::endl;
sock.send(backpack);
std::cout << "Sent the data" << std::endl;
std::cout << "Client task completed" << std::endl;
return 0;
}
Here is The server code
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
std::cout << "Server Has started" << std::endl;
sf::TcpSocket peer;
sf::TcpListener ear;
sf::Packet backpack;
if (ear.listen(25568) != sf::Socket::Done){
std::cout << "Listerner failed to be setup, another program may be using that port." << std::endl;
} else {
std::cout << "Server is ready" << std::endl;
}
if (ear.accept(peer) != sf::Socket::Done){
std::cout << "Client refused connection" << std::endl;
} else {
std::cout << "Client connection Accepted" << std::endl;
}
peer.receive(backpack);
std::cout << "Data recieved" << std::endl;
int num;
backpack >> num;
std::cout << num << std::endl;
std::cout << "hello world" << std::endl;
return 0;
}
When running the server and client (In either order) The client will output nothing after the message "Attempting to connect to server" appears, And the server outputs nothing after the message "Server is ready" Please help =) I have been stuck on this problem for a while.
Apparently, on Linux (Centos 7, g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)) you can seek to absurd offsets in files opened for reading. I can get the file length and check the offset myself, but shouldn't the seekg fail?
This program illustrates. No error conditions are detected, but the file is much less than 999999 bytes long.
#include <iostream>
#include <fstream>
int main(int argc, char **argv) {
std::ifstream f("./tstseek.cpp",std::ios::in);
if(!f.seekg(9999999)) {
std::cerr << "SEEK FAILED" << std::endl;
}
long int pos = f.tellg();
if(f.bad() || f.fail()) {
std::cerr << "SEEK FAILED" << std::endl;
}
if(f.eof()) {
std::cerr << "EOF AFTER SEEK" << std::endl;
}
std::string s;
std::getline(f,s);
if(f.bad() || f.fail()) {
std::cerr << "getline failed" << std::endl;
}
if(f.eof()) {
std::cerr << "EOF after getline" << std::endl;
}
std::streamsize bytesread = f.gcount();
std::cerr << "Position after seekg(9999999) = " << pos << std::endl
<< "bytes read = " << bytesread << std::endl
<< "string=[" << s << "]" << std::endl;
}
Trying to get some data from CallNtPowerInformation(SystemPowerInfomation…) but the returned data doesn't seem to be correct (lidpresent should be true but it's false, VideoDimPresent should be false but it's true..) I'm new to C++ and I'm pretty sure I'm doing something wrong.
My code:
#include <NTstatus.h>
#define WIN32_NO_STATUS
#include <windows.h>
#include <Powrprof.h>
#include <iostream>
#pragma comment(lib, "Powrprof.lib")
int main()
{
SYSTEM_POWER_CAPABILITIES spwr;
NTSTATUS status = ::CallNtPowerInformation(SystemPowerInformation, NULL, 0, &spwr, sizeof(SYSTEM_POWER_CAPABILITIES));
if(STATUS_SUCCESS == status){
if(spwr.LidPresent){
std::cout << "LidPresent TRUE!" << std::endl;
}else{
std::cout << "LidPresent FALSE!" << std::endl;
}
if(spwr.VideoDimPresent){
std::cout << "VideoDimPresent TRUE!" << std::endl;
}else{
std::cout << "VideoDimPresent FALSE!" << std::endl;
}
if(spwr.SystemS1){
std::cout << "SystemS1 TRUE!" << std::endl;
}else{
std::cout << "SystemS1 FALSE!" << std::endl;
}
if(spwr.SystemS2){
std::cout << "SystemS2 TRUE!" << std::endl;
}else{
std::cout << "SystemS2 FALSE!" << std::endl;
}
if(spwr.SystemS3){
std::cout << "SystemS3 TRUE!" << std::endl;
}else{
std::cout << "SystemS3 FALSE!" << std::endl;
}
if(spwr.SystemS4){
std::cout << "SystemS4 TRUE!" << std::endl;
}else{
std::cout << "SystemS4 FALSE!" << std::endl;
}
}
else
{
std::cout << "CallNtPowerInformation failed. Status: " << status << std::endl;
}
return status;
}
You passed SystemPowerInformation, so lpOutputBuffer is expected to point to SYSTEM_POWER_INFORMATION structure.
You may want to pass SystemPowerCapabilities rather than SystemPowerInformation if you expect SYSTEM_POWER_CAPABILITIES.
See CallNtPowerInformation documentation.