not declared in this scope error, c++ - c++

I am unsure how to solve my error. How do I make is so that my void run() function can see this variable 'intf'? the value of intf itself was declared separately in a .cnf file. Thank you
My errors are as follows
monreqserver.cc: In member function 'void Pds::MyXtcMonitorServer::run()':
monreqserver.cc:57: error: 'intf' was not declared in this scope
My code is as follows:
#include "pdsdata/app/XtcMonitorServer.hh"
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdlib.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#define mult_address "225.0.0.37"
#define mult_port "1100"
namespace Pds {
class MyXtcMonitorServer : public XtcMonitorServer {
public:
MyXtcMonitorServer(const char* tag,
unsigned sizeofBuffers,
unsigned numberofEvBuffers,
unsigned numberofEvQueues, const char * intf) :
XtcMonitorServer(tag,
sizeofBuffers,
numberofEvBuffers,
numberofEvQueues)
{
_init();
}
~MyXtcMonitorServer() {}
public:
void run() {
//////////////
//udp socket//
//////////////
int udp_socket_info;
struct sockaddr_in udp_server;
udp_socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_socket_info == -1) {
puts("Could not create socket");
}
udp_server.sin_addr.s_addr = inet_addr(mult_address);
udp_server.sin_port = htons(1100);
udp_server.sin_family = AF_INET;
ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, intf);
if (ioctl(udp_socket_info, SIOCGIFADDR, &ifr)<0) {
perror("SIOCGIFADDR failed");
}
char* port = "1100";
char* ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
char* zero = "/0";
char ip_port[100];
sprintf(ip_port, "%s %s %s", ip, port, zero);
sendto(udp_socket_info , ip_port , strlen(ip_port), 0, (struct sockaddr *)&udp_server, sizeof(udp_server));
puts("Message Sent");
///////////////////////////////
///SETTING UP TCP CONNECTION///
///////////////////////////////
int tcp_socket_info, tcp_new_socket;
struct sockaddr_in tcp_server, tcp_client;
tcp_socket_info = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_socket_info == -1) {
printf("Could not create socket");
}
tcp_server.sin_addr.s_addr = INADDR_ANY;
tcp_server.sin_family = AF_INET;
tcp_server.sin_port = htons(1100);
int y=1;
if(setsockopt(tcp_socket_info, SOL_SOCKET, SO_REUSEADDR, (char*)&y, sizeof(y)) == -1) {
perror("set reuseaddr");
}
//binds socket
if (bind(tcp_socket_info, (struct sockaddr *)&tcp_server, sizeof(tcp_server)) < 0) {
perror("Bind error");
}
//listen
listen(tcp_socket_info , 5);
//waiting for connection
puts("Waiting for incoming connections...");
int c = sizeof(struct sockaddr_in);
//accept connection loop
tcp_new_socket = accept(tcp_socket_info, (struct sockaddr *)&tcp_client, (socklen_t*)&c);
puts("Connection accepted");
while(1)
sleep(1);
}
private:
void _copyDatagram(Dgram* dg, char*) {}
void _deleteDatagram(Dgram* dg) {}
void _requestDatagram() {}
private:
};
};
using namespace Pds;
void usage(char* progname) {
printf("Usage: %s -p <platform> -P <partition> -i <node mask> -n <numb shm buffers> -s <shm buffer size> [-q <# event queues>] [-t <tag name>] [-d] [-c] [-g <max groups>] [-h]\n", progname);
}
int main(int argc, char** argv) {
const unsigned NO_PLATFORM = unsigned(-1UL);
unsigned platform=NO_PLATFORM;
const char* partition = 0;
const char* tag = 0;
const char* intf = 0;
int numberOfBuffers = 0;
unsigned sizeOfBuffers = 0;
unsigned nevqueues = 1;
unsigned node = 0xffff;
unsigned nodes = 6;
bool ldist = false;
int c;
while ((c = getopt(argc, argv, "I:p:i:g:n:P:s:q:t:dch")) != -1) {
errno = 0;
char* endPtr;
switch (c) {
case 'p':
platform = strtoul(optarg, &endPtr, 0);
if (errno != 0 || endPtr == optarg) platform = NO_PLATFORM;
break;
case 'I':
intf = optarg;
break;
case 'i':
node = strtoul(optarg, &endPtr, 0);
break;
case 'g':
nodes = strtoul(optarg, &endPtr, 0);
break;
case 'n':
sscanf(optarg, "%d", &numberOfBuffers);
break;
case 'P':
partition = optarg;
break;
case 't':
tag = optarg;
break;
case 'q':
nevqueues = strtoul(optarg, NULL, 0);
break;
case 's':
sizeOfBuffers = (unsigned) strtoul(optarg, NULL, 0);
break;
case 'd':
ldist = true;
break;
case 'h':
// help
usage(argv[0]);
return 0;
break;
default:
printf("Unrecogized parameter\n");
usage(argv[0]);
break;
}
}
if (!numberOfBuffers || !sizeOfBuffers || platform == NO_PLATFORM || !partition || node == 0xffff) {
fprintf(stderr, "Missing parameters!\n");
usage(argv[0]);
return 1;
}
if (numberOfBuffers<8) numberOfBuffers=8;
if (!tag) tag=partition;
printf("\nPartition Tag:%s\n", tag);
MyXtcMonitorServer* apps = new MyXtcMonitorServer(tag,
sizeOfBuffers,
numberOfBuffers,
nevqueues, intf);
apps->distribute(ldist);
apps->run();
return 0;
}
and the header file include is as follows:
#ifndef Pds_XtcMonitorServer_hh
#define Pds_XtcMonitorServer_hh
#include "pdsdata/app/XtcMonitorMsg.hh"
#include "pdsdata/xtc/TransitionId.hh"
#include <pthread.h>
#include <mqueue.h>
#include <queue>
#include <stack>
#include <vector>
#include <poll.h>
#include <time.h>
namespace Pds {
class Dgram;
class TransitionCache;
class XtcMonitorServer {
public:
XtcMonitorServer(const char* tag,
unsigned sizeofBuffers,
unsigned numberofEvBuffers,
unsigned numberofEvQueues, const char * intf);
virtual ~XtcMonitorServer();
public:
enum Result { Handled, Deferred };
Result events (Dgram* dg);
void discover ();
void routine ();
void unlink ();
public:
void distribute (bool);
protected:
int _init ();
private:
void _initialize_client();
mqd_t _openQueue (const char* name, mq_attr&);
void _flushQueue (mqd_t q);
void _flushQueue (mqd_t q, char* m, unsigned sz);
void _moveQueue (mqd_t iq, mqd_t oq);
bool _send (Dgram*);
void _update (int,TransitionId::Value);
void _clearDest (mqd_t);
private:
virtual void _copyDatagram (Dgram* dg, char*);
virtual void _deleteDatagram(Dgram* dg);
virtual void _requestDatagram();
private:
const char* _tag; // name of the complete shared memory segment
unsigned _sizeOfBuffers; // size of each shared memory datagram buffer
unsigned _numberOfEvBuffers; // number of shared memory buffers for events
unsigned _numberOfEvQueues; // number of message queues for events
char* _myShm; // the pointer to start of shared memory
XtcMonitorMsg _myMsg; // template for messages
mqd_t _discoveryQueue; // message queue for clients to get
// the TCP port for initiating connections
mqd_t _myInputEvQueue; // message queue for returned events
mqd_t* _myOutputEvQueue; // message queues[nclients] for distributing events
std::vector<int> _myTrFd; // TCP sockets to clients for distributing
// transitions and detecting disconnects.
std::vector<int> _msgDest; // last client to which the buffer was sent
TransitionCache* _transitionCache;
int _initFd;
pollfd* _pfd; /* poll descriptors for:
** 0 new client connections
** 1 buffer returned from client
** 2 events to be distributed
** 3+ transition send/receive */
int _nfd;
mqd_t _shuffleQueue; // message queue for pre-distribution event processing
mqd_t _requestQueue; // message queue for buffers awaiting request completion
timespec _tmo;
pthread_t _discThread; // thread for receiving new client connections
pthread_t _taskThread; // thread for datagram distribution
unsigned _ievt; // event vector
};
};
#endif

The variable intf is a local variable of main(). It is therefore unknown outside the scope of main(). This is why you can't access it in the member functions of your class.
Three possible solutions:
you make the variable global (as it seems to be a global parameter that applies to all the classes
you make the variable a public static variable in the class. You then can initialize it from main, by using the prefix of your class.
or you define it as parameter of the run() member function (and invoke run from main accordingly).
The last one is the on which I'd choose, but i don't know enough about the context to give more objective advices for the choice:
// in the class:
void run(const char*intf) { // for convenience you can use the same name
...
}
// in main:
...
apps->run(intf); // pass the local variable as parameter

Name intf is referred in function run. But the compiler does not see any its declaration before its usage in the function
void run() {
//...
strcpy(ifr.ifr_name, intf);
^^^^^

Related

socket recv read no message

The following code is an application supposed to be communicating between two applications. In one exe (A) user type a message and the message is printed in the other exe(B).
The flow of the program:
Both exe calls connectTo so they are ready to send and receive messages between.
User type a message to be send in A console window, which calls sendMsg in A exe. When message is received in B, the message is printed in B console window.
The problem is that the message received is sometimes empty. When I enter 1234 ten times in A, ten messages are printed in B with only 5-6 of them are 1234 and the rest are empty. The situation is the same from B to A.
++++++++++++++++++++++++++
whole program
++++++++++++++++++++++++++
Header.h
#pragma once
#include <WinSock2.h>
#include <Windows.h>
#include <mutex>
#include <thread>
class CommuWin
{
private:
std::mutex m_accessMutexSend;
std::mutex m_accessMutexReceive;
std::thread m_sendThread;
std::thread m_receiveThread;
bool m_IsSendReady = false;
bool m_IsRecvReady = false;
SOCKET m_outSocket;
SOCKADDR_IN m_outAddr;
SOCKET m_inSocket;
SOCKADDR_IN m_inAddr;
public:
CommuWin(int InPort, int OutPort);
~CommuWin();
int connectTo();
int sendMsg(const char* message);
int StartReceiveMsg();
bool GetRecvStatus();
bool GetSendStatus();
private:
void SetRecvStatus(bool ready);
void SetSendStatus(bool ready);
int SetupRecvEnd();
int SetupSendEnd();
int sendMsgTo(const char* message);
int ReceiveMsgFrom();
};
Source.cpp
#pragma comment(lib, "Ws2_32.lib")
#include "Header.h"
#define OKAY (1)
#define ERROR (-1)
#define MAX_MSG_SIZE (200)
class NetworkServices
{
public:
static int sendMessage(SOCKET curSocket, const char* message, int messageSize);
static int receiveMessage(SOCKET curSocket, char * buffer, int bufSize);
};
int NetworkServices::sendMessage(SOCKET curSocket, const char* message, int messageSize)
{
return send(curSocket, message, messageSize, 0);
}
int NetworkServices::receiveMessage(SOCKET curSocket, char * buffer, int bufSize)
{
return recv(curSocket, buffer, bufSize, 0);
}
CommuWin::CommuWin(int InPort, int OutPort)
{
WSAData wsaData;
WORD DLLVersion;
DLLVersion = MAKEWORD(2, 1);
int r = WSAStartup(DLLVersion, &wsaData);
///////////////////////////////////////////////////////////////////////
m_outSocket = socket(AF_INET, SOCK_STREAM, NULL);
m_outAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
m_outAddr.sin_family = AF_INET;
m_outAddr.sin_port = htons(OutPort);
m_inSocket = socket(AF_INET, SOCK_STREAM, NULL);
m_inAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
m_inAddr.sin_family = AF_INET;
m_inAddr.sin_port = htons(InPort);
}
CommuWin::~CommuWin()
{
}
int CommuWin::connectTo()
{
printf("connect to");
printf("\n");
m_sendThread = std::thread(
&CommuWin::SetupSendEnd,
this);
m_receiveThread = std::thread(
&CommuWin::SetupRecvEnd,
this);
return OKAY;
}
int CommuWin::SetupSendEnd()
{
SOCKET sListen;
sListen = socket(AF_INET, SOCK_STREAM, NULL);
bind(sListen, (SOCKADDR*)&m_outAddr, sizeof(m_outAddr));
listen(sListen, SOMAXCONN);
m_outSocket = accept(sListen, NULL, NULL);
if (m_outSocket != INVALID_SOCKET)
{
SetSendStatus(true);
printf("accepted\n");
}
return OKAY;
}
int CommuWin::SetupRecvEnd()
{
int connectSucceed = 0;
do
{
Sleep(1000);
connectSucceed = connect(m_inSocket, (SOCKADDR*)&m_inAddr, sizeof(m_inAddr));
} while (connectSucceed == SOCKET_ERROR);
SetRecvStatus(true);
printf("connected\n");
return OKAY;
}
int CommuWin::sendMsg(const char* message)
{
if (GetSendStatus())
{
m_sendThread.detach();
m_sendThread = std::thread(
&CommuWin::sendMsgTo,
this,
message);
}
return OKAY;
}
int CommuWin::sendMsgTo(const char* message)
{
NetworkServices::sendMessage(m_outSocket, message, (int)strlen(message));
return OKAY;
}
int CommuWin::StartReceiveMsg()
{
if (GetRecvStatus())
{
m_receiveThread.detach();
m_receiveThread = std::thread(
&CommuWin::ReceiveMsgFrom,
this);
}
return OKAY;
}
int CommuWin::ReceiveMsgFrom()
{
while (true)
{
char message[MAX_MSG_SIZE];
ZeroMemory(message, MAX_MSG_SIZE);
NetworkServices::receiveMessage(m_inSocket, message, sizeof(message));
printf(message);
printf("\n");
}
return OKAY;
}
void CommuWin::SetRecvStatus(bool ready)
{
std::lock_guard<std::mutex> lock(m_accessMutexReceive);
m_IsRecvReady = ready;
}
void CommuWin::SetSendStatus(bool ready)
{
std::lock_guard<std::mutex> lock(m_accessMutexSend);
m_IsSendReady = ready;
}
bool CommuWin::GetRecvStatus()
{
std::lock_guard<std::mutex> lock(m_accessMutexReceive);
return m_IsRecvReady;
}
bool CommuWin::GetSendStatus()
{
std::lock_guard<std::mutex> lock(m_accessMutexSend);
return m_IsSendReady;
}
main.cpp
#include "stdafx.h"
#include "Header.h"
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::cout << argc <<std::endl;
int Inport = std::stoi(argv[1]);
int Outport = std::stoi(argv[2]);
//std::cout << "inport = " << argv[1] << " outport = " << argv[2] << std::endl;
std::cout << "inport = " << Inport << " outport = " << Outport << std::endl;
CommuWin com(Inport, Outport);
com.connectTo();
while (true)
{
if (com.GetSendStatus() && com.GetRecvStatus())
{
com.StartReceiveMsg();
break;
}
}
while (true)
{
std::cout << "Enter Send Message" << std::endl;
std::string msg;
std::cin >> msg;
com.sendMsg(msg.c_str());
}
return 0;
}
There are multiple issues with your code. First of all, you need to check results of all the functions, including, but not limited to, to sListen, bind, listen, recv.
NetworkServices::receiveMessage(m_inSocket, message, sizeof(message));
// Without checking recv result there is no way to guess how much
// bytes are actually stored in `message`, if any. Also boldly assuming
// that `message` is null terminated and represents a proper format string
// is dangerous.
printf(message);
You also need to carefully initialize all the stuff, especially sockaddr structures, which may get potentially partially initialized in this case. You are using multiple threads but perform insufficient synchronization. Method sendMsgTo(const char* message) executed by (potentially detached) background thread receives a pointer to a string buffer that may get invalidated at any time.
If you want to send and receive messages, you have to write some code to do that. Nowhere is there any code to send or receive messages. If you think there is, point specifically to the code that figures out whether or not the data you received is one or more messages. You cannot do it.
TCP is not a message protocol. If you need a message protocol on top of TCP, you have to implement one. Have a look at protocols that do this such as HTTP, IRC, or FTP so see how it's done.
If you log the number of bytes received, you will see that all the data you sent was received. It's your job to split that data into messages if you need to -- it won't happen by itself.

libpcap c/c++ callback function [duplicate]

This question already has answers here:
How can I pass a class member function as a callback?
(13 answers)
Closed 5 years ago.
I write application for studies, i am not experienced with c/c++. I have problem with call callback function, when I try call callback function i have an error
invalid use of non-static member function
if(pcap_loop(handle, 1, got_packet, NULL)<0){
Here is my callback function:
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
static int count = 1;
fprintf(stdout,"%d, ",count);
if(count == 4)
fprintf(stdout,"Come on baby sayyy you love me!!! ");
if(count == 7)
fprintf(stdout,"Tiiimmmeesss!! ");
fflush(stdout);
count++;
}
this is how I call this function
int CapThread::capture()
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
QString filter_expression = "udp and ip dst host "+ui->ip_addr->text();
QByteArray byte_array =filter_expression.toUtf8();
const char *filter_exp = byte_array.data();
struct bpf_program fp;
bpf_u_int32 mask;
bpf_u_int32 net; /* The IP of our sniffing device */
struct pcap_pkthdr header;
struct ether_header *eptr;
struct udphdr *udp_header;
const u_char *packet;
struct ip *ip;
struct rtp_header *rtp_header;
u_char *ptr;
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
return(1);
}
pcap_t *handle;
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(3);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filtecho deb http://repository.spotify.com stable non-free | sudo tee /etc/apt/sources.list.d/spotify.lister %s: %s\n", filter_exp, pcap_geterr(handle));
return(4);
}
// packet = pcap_next(handle, &header);
// pcap_loop(handle,5,got_packet,NULL);
if(pcap_loop(handle, 6, got_packet, NULL)<0){
qDebug()<<"got packet";
}
pcap_close(handle);
return 0;
}
this is how my header file look like:
#ifndef CAPTHREAD_H
#define CAPTHREAD_H
#include <pcap.h>
#include <netinet/ether.h> //plik nagłówkowy umożliwiający przekonwertowanie danych z nagłówka na kod ASCII
#include <netinet/ip.h> //plik zawierający struktury nagłówka IP i umożliwiający przekonwertowanie danych z nagłówka na kod ASCII
#include <net/ethernet.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <QDebug>
#include <iostream>
#include <stdio.h>
#include "ui_mainwindow.h"
class CapThread
{
public:
CapThread();
CapThread(Ui::MainWindow *ui);
~CapThread();
void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);
int capture();
private:
struct rtp;
Ui::MainWindow *ui;
char *dev, errbuf;
pcap_t *handle;
char filter_exp[];
struct bpf_program fp;
struct ether_header *eptr;
bpf_u_int32 mask;
bpf_u_int32 net;
};
#endif // CAPTHREAD_H
actually i am out of ideas, can someone help me?
The problem you are having is because you are trying to call a member function without an instance. Since your callback does not use any class member attributes, you could declare it as static, this means you can call it without an instance of the class.
On the header:
static void got_packet(u_char *args, const pcap_pkthdr *header, const u_char *packet);
On the implementation:
static void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
...
}

TCP "connecttion refused" error (c++ CLion)

I create a TCP server, I always get following error when I was run. How I get rid of this error, I do not understand where wrong?
Also it say data received while connection refused, I do not run client. Why?
Error:
Socket Created
Error connect to server: Connection refused
Error to accept: Invalid argument
Data received
Server
#include <iostream>
#include "serverh.hpp"
#include "glimpsepackages.h"
#include "soupbintcppackages.h"
int main() {
//soupBinTCP structures
DebugPacket D; //"+"
LoginAcceptedPacket A;
LoginRejectPacket J;
SequencedDataPacket S;
ServerHeartBeatPacket H;
EndOfSessionPacket Z;
//glimpse structures
SecondsMessage T;
CombinationOrderBookDelivery M;
TickSizeTableEntryPackage L;
OrderBookDirectoryPackage R;
OrderBookStatePackage O;
AddOrderMessageNoMPIDPackage A1;
AddOrderMessageMPIDPackage F;
std::string ip="127.0.0.1";
int port=7888;
ssize_t size=1024;
void *data={};
TcpServer server(ip, port);
server.connectToClient();
server.accept();
for (;;) {
server.receivingData(size);
switch (server.pop()) {
case '+':
// D.length=server.pop();
D.code = server.pop8();
D.text = server.pop();
break;
case 'A' :
A.to_little_endian();
A.code = server.pop8();
A.sequence_number = server.pop32();
for (int i = 0; i < 10; i++) {
A.session[i] = server.pop();
}
break;
case 'J':
J.code = server.pop8();
J.rejectreasoncode = server.pop();
break;
case 'S':
S.code = server.pop8();
S.message = server.pop();
break;
case 'H':
H.code = server.pop8();
case 'Z':
Z.code = server.pop8();
default:
return 0;
}
}
serverh.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string>
#include "serverh.hpp"
#include "util.h"
#include <iostream>
TcpServer::TcpServer(std::string ip, int port):_ip(ip),
_port(port), _addrSize(sizeof _serverAddress)
{
_sockFd=-1;
};
bool TcpServer::connectToClient(){
_sockFd=socket(AF_INET,SOCK_STREAM,0);
if(_sockFd==-1)
perror("Error creating socket");
else
std::cerr<<"Socket Created\n";
_serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());
_serverAddress.sin_family=AF_INET;
_serverAddress.sin_port=htons(_port);
if(::connect(_sockFd,(struct sockaddr *)&_serverAddress, sizeof(_serverAddress))<0) {
perror("Error connect to server");
return false;
}
else
std::cerr<<"Connected\n";
if(::bind(_sockFd,(struct sockaddr *)&_serverAddress,_addrSize)<0)
perror("Error bind to server");
else
std::cerr<<"Socket bind\n";
if(::listen(_sockFd,5)==0)
std::cerr<<"Listining\n";
else
perror("Error on Listening");
return true;
}
bool TcpServer::accept(){
if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)
perror("Error to accept");
else
std::cerr<<"Accepted\n";
return true;
}
void *TcpServer::receivingData(ssize_t size){
char *buffer=new char[size];
if(::recv(_sockFd,buffer, size,0)>0);
std::cerr<<"Data received\n";
return buffer;
}
void TcpServer::incOffset(int rest_length) {
void *restbuff = alloca(rest_length);
if (::recv(_sockFd, restbuff, rest_length, 0) < 0)
std::cerr << "Data received\n";
}
char TcpServer::pop(){
char ch;
if(::recv(_sockFd, &ch, 1, 0)>0)
return ch;
}
int8_t TcpServer::pop8() {
int8_t ch;
if (::recv(_sockFd, &ch, 8, 0) > 0)
return getLeValue(ch); // func include endian functions
}
int16_t TcpServer::pop16(){
int16_t ch;
if(::recv(_sockFd, &ch, 16, 0)>0)
return getLeValue(ch);
}
int32_t TcpServer::pop32(){
int32_t ch;
if(::recv(_sockFd, &ch, 32, 0)>0)
return getLeValue(ch);
}
int64_t TcpServer::pop64() {
int64_t ch;
if (::recv(_sockFd, &ch, 64, 0) > 0)
return getLeValue(ch);
}
Serverh.hpp
#ifndef SERVERH_H
#define SERVERH_H
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string>
class TcpServer{
public:
TcpServer(const std::string _ip,int _port);
bool connectToClient();
bool accept();
void *receivingData(ssize_t size);
bool sendToClient();
char pop();
int8_t pop8();
int16_t pop16();
int32_t pop32();
int64_t pop64();
void incOffset(int rest_length);
private:
std::string _ip;
int _port;
int _sockFd;
sockaddr_in _serverAddress;
socklen_t _addrSize;
};
#endif
The problem
Error: Socket Created Error connect to server: Connection refused Error to accept: Invalid argument Data received
Two errors here, a failure to connect because the other address is not accepting connections and a failure to accept because one or more of the input parameters are bad.
The failure to connect is understandable. Clients are rarely listening on a port for connections. Clients usually initiate the connection.
The failure to accept is caused by connectToClient exiting after failing to connect. It never binds and listens, so the socket has not been set up for accept.
Fixing this
A server should not attempt to connect. It should listen for attempts to connect to it. This makes connectToClient bizarre. It tries to connect to a client and then, on the same socket, bind and listen. You can't do both, so I'd discard
if(::connect(_sockFd,(struct sockaddr *)&_serverAddress, sizeof(_serverAddress))<0) {
perror("Error connect to server");
return false;
}
else
std::cerr<<"Connected\n";
and
reconsider
_serverAddress.sin_addr.s_addr=inet_addr(_ip.c_str());
as this will only listen to one incoming address which might be what you want, but isn't usually. I would also rename the function to reflect the change in purpose.
Note:
if(::accept(_sockFd,(struct sockaddr *) &_serverAddress, &_addrSize)<0)
passes in a pointer to the server address. This location will be overwritten with the address of the client. It's harmless, but you probably don't want to do this. You aren't using the returned address as far as I can see, so you might as well just pass in NULLs and pass on it.
if(::accept(_sockFd, NULL, NULL)<0)

Attempting to reference a deleted function inside xmemory0 on threaded sockets class

I'm trying to finish this threaded sockets class. This is what I've got so far:
#pragma once
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <atomic>
#include <thread>
#include <vector>
#pragma comment(lib, "Ws2_32.lib")
//---------------------------------------------------CLASS
class AsyncSockets
{
public:
int StartSockets(int, void(*)(SOCKET, sockaddr*, int, std::atomic<bool>&));
static const int FAIL_SUCCESS, FAIL_INIT_WS, FAIL_BIND_SOCKET, FAIL_ASYNCSELECT, FAIL_LISTEN, FAIL_ACCEPT;
bool StopSockets();
private:
/*static*/ void(*Functiane)(SOCKET, sockaddr*, int, std::atomic<bool>&);
struct RunThread {
std::thread Thread;
std::atomic<bool> Running = true;
};
void AcceptLoop();
SOCKET Socket; std::atomic<bool> running = false;
std::vector<AsyncSockets::RunThread>ThreadList;
void StartFunc(SOCKET, sockaddr*, int, int);
std::thread loopaccepter;
std::thread deleteInvalid;
};
//-------------------------------------STARTFUNC---PRIVATE
void AsyncSockets::StartFunc(SOCKET socketa, sockaddr* sockaddra, int sockaddrinfolena, int BoolIndex) {
if (socketa != INVALID_SOCKET) {
(*Functiane)(socketa, sockaddra, sockaddrinfolena, ThreadList[BoolIndex].Running);
}else{
closesocket(socketa); ThreadList[BoolIndex].Running = false; return;
}
closesocket(socketa);
ThreadList[BoolIndex].Running = false;
return;
}
//------------------------------------ACCEPTLOOP---PRIVATE
void AsyncSockets::AcceptLoop() {
while (true) {
struct sockaddr *AddressInfo = NULL; int addrinfoLen;
if (running) {
int temp = ThreadList.size();
for (int i = 0; i < temp; i++){
if (i != temp - 1) {
if (!ThreadList[i].Running){
if (ThreadList[i].Thread.joinable()) ThreadList[i].Thread.join();
ThreadList[i].Running = true; ThreadList[i].Thread = std::thread(&AsyncSockets::StartFunc, this, accept(Socket, AddressInfo, &addrinfoLen), AddressInfo, addrinfoLen, i); }
}else{
if (!ThreadList[i].Running) {
if (ThreadList[i].Thread.joinable()) ThreadList[i].Thread.join();
ThreadList[i].Running = true; ThreadList[i].Thread = std::thread(&AsyncSockets::StartFunc, this, accept(Socket, AddressInfo, &addrinfoLen), AddressInfo, addrinfoLen, i);}
else { ThreadList.push_back({ std::thread(&AsyncSockets::StartFunc, this, accept(Socket, AddressInfo, &addrinfoLen), AddressInfo, addrinfoLen, i + 1), true });}
}
}
}else return;
}
}
//-----------------------------------STARTSOCKETS---PUBLIC
int AsyncSockets::StartSockets(int port, void(*functian)(SOCKET, sockaddr*, int, std::atomic<bool>&)) {
//DeclaringShiet
int recvbuflen = 512;
//InitWinsock
WSADATA WsaDat;
int nResult = WSAStartup(MAKEWORD(2, 2), &WsaDat);
if (nResult != 0)
{
return FAIL_INIT_WS;
}
//BuildAddr
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
//BindSocket
if (bind(Socket, (LPSOCKADDR)&SockAddr, sizeof(SockAddr)) == SOCKET_ERROR)
{
return FAIL_BIND_SOCKET;
}
//StartListening
if (listen(Socket, 10) == SOCKET_ERROR)
{
return FAIL_LISTEN;
}
//StartAccepting
running = true;
Functiane = (*functian);
loopaccepter = std::thread(&AsyncSockets::AcceptLoop, this);
}
//------------------------------------STOPSOCKETS---PUBLIC
bool AsyncSockets::StopSockets() {
running = false;
loopaccepter.join();
closesocket(Socket);
for (unsigned int i = 0; i < ThreadList.size(); i++)ThreadList[i].Running = false;
for (unsigned int i = 0; i < ThreadList.size(); i++)if(ThreadList[i].Thread.joinable())ThreadList[i].Thread.join();
return true;
}
//------------------------------------ERRORCONSTS---PUBLIC
#pragma region DeclaringErrorConsts
const int
AsyncSockets::FAIL_SUCCESS = 0x0,
AsyncSockets::FAIL_INIT_WS = 0x1,
AsyncSockets::FAIL_BIND_SOCKET = 0x2,
AsyncSockets::FAIL_LISTEN = 0x3,
AsyncSockets::FAIL_ACCEPT = 0x4;
#pragma endregion
So, as you may have noticed, I'm trying to do this with std::thread, vectors, and winsock. And the point is to have a function as an argument for StartSockets, that'll do all the sending, receiving, and processing.
Thread objects will be stored in a vector of the RunThread type, that contains an atomic bool that tells whether or not the thread is/should be running, as well as the thread object.
My problem is I'm getting an
'AsyncSockets::RunThread::RunThread(const AsyncSockets::RunThread &)': attempting to reference a deleted function
in
xmemory0 (line 840)
(c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.24728\include\xmemory0)
I have no idea why, and since RunThread is a struct and not a function, along with the fact that RunThread::RunThread doesn't make sense, I don't, at all, understand what the profanity is going on. In case it can help any further, I'm on VS 2017 RC on windows 10, win32 console application.
What am I doing wrong?
Thank you.

How to implement a recv() callback

I'm trying to improve my knowledge of OOP and decided to create a simple class to simplify sockets programming.
This is a learning experiment so I do not want to use boost, or other libraries.
I want to implement an event-driven recv(). Meaning, everytime there is new data coming in, it should call my function.
I think I need to create a thread to run a recv() loop and then call my function everytime there is new data. Is there other way around using threads? I want my code to be portable.
Here is my simple Class and example code:
class.h:
#ifndef _SOCKETSCLASS_H
#define _SOCKETSCLASS_H
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define W32
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#define SOCKET int
#endif
#include <string>
#include<ctime>
#include <stdio.h>
#include <stdarg.h>
#include <varargs.h>
#include <tchar.h>
using namespace std;
#ifdef _DEBUG
#define DEBUG(msg) XTrace(msg)
#else
#define DEBUG(msg, params)
#endif
struct TCP_Client_opts
{
BOOL UseSCprotocol;
BOOL UseEncryption;
BOOL UseCompression;
int CompressionLevel;
void *Callback;
BOOL async;
};
struct TCP_Stats
{
unsigned long int upload; //bytes
unsigned long int download;//bytes
time_t uptime; //seconds
};
class TCP_Client
{
public:
TCP_Client();
TCP_Client(TCP_Client_opts opts_set);
~TCP_Client();
SOCKET GetSocket();
void SetOptions(TCP_Client_opts opts_set);
TCP_Client_opts GetOptions();
BOOL Connect(string server, int port);
int Send(string data);
int Recv(string *data);
BOOL IsConnected();
int Disconnect();
TCP_Stats GetStats();
private:
SOCKET s = SOCKET_ERROR;
TCP_Client_opts opts;
TCP_Stats stats;
BOOL connected = FALSE;
time_t starttime;
};
#endif
class.cpp:
#include "SocketsClass.h"
void XTrace(LPCTSTR lpszFormat, ...)
{
va_list args;
va_start(args, lpszFormat);
int nBuf;
TCHAR szBuffer[512]; // get rid of this hard-coded buffer
nBuf = _vsnwprintf_s(szBuffer, 511, lpszFormat, args);
::OutputDebugString(szBuffer);
va_end(args);
}
TCP_Client::TCP_Client(TCP_Client_opts opts_set)
{
SetOptions(opts_set);
}
TCP_Client::~TCP_Client()
{
Disconnect();
}
TCP_Client::TCP_Client()
{
}
void TCP_Client::SetOptions(TCP_Client_opts opts_set)
{
opts = opts_set;
}
TCP_Client_opts TCP_Client::GetOptions()
{
return opts;
}
SOCKET TCP_Client::GetSocket()
{
return s;
}
BOOL TCP_Client::IsConnected()
{
return connected;
}
int TCP_Client::Disconnect()
{
connected = FALSE;
stats.uptime = time(0) - starttime;
return shutdown(s, 2);
}
BOOL TCP_Client::Connect(string server, int port)
{
struct sockaddr_in RemoteHost;
#ifdef W32
WSADATA wsd;
if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
{
DEBUG(L"Failed to load Winsock!\n");
return FALSE;
}
#endif
//create socket if it is not already created
if (s == SOCKET_ERROR)
{
//Create socket
s = socket(AF_INET, SOCK_STREAM, 0);
if (s == SOCKET_ERROR)
{
DEBUG(L"Could not create socket");
return FALSE;
}
}
//setup address structure
if (inet_addr(server.c_str()) == INADDR_NONE)
{
struct hostent *he;
//resolve the hostname, its not an ip address
if ((he = gethostbyname(server.c_str())) == NULL)
{
//gethostbyname failed
DEBUG(L"gethostbyname() - Failed to resolve hostname\n");
return FALSE;
}
}
else//plain ip address
{
RemoteHost.sin_addr.s_addr = inet_addr(server.c_str());
}
RemoteHost.sin_family = AF_INET;
RemoteHost.sin_port = htons(port);
//Connect to remote server
if (connect(s, (struct sockaddr *)&RemoteHost, sizeof(RemoteHost)) < 0)
{
DEBUG(L"connect() failed");
return FALSE;
}
connected = TRUE;
starttime = time(0);
stats.download = 0;
stats.upload = 0;
return TRUE;
}
TCP_Stats TCP_Client::GetStats()
{
if (connected==TRUE)
stats.uptime = time(0)-starttime;
return stats;
}
int TCP_Client::Send(string data)
{
stats.upload += data.length();
return send(s, data.c_str(), data.length(), 0);
}
int TCP_Client::Recv(string *data)
{
int ret = 0;
char buffer[512];
ret = recv(s, buffer, sizeof(buffer), 0);
data->assign(buffer);
data->resize(ret);
stats.download += data->length();
return ret;
}
main.cpp:
#include <stdio.h>
#include <string.h>
#include "SocketsClass.h"
using namespace std;
int main(int argc, char *argv)
{
TCP_Client tc;
tc.Connect("127.0.0.1", 9999);
tc.Send("HEllo");
string data;
tc.Recv(&data);
puts(data.c_str());
tc.Disconnect();
printf("\n\nDL: %i\nUP: %i\nUptime: %u\n", tc.GetStats().download, tc.GetStats().upload, tc.GetStats().uptime);
return 0;
}
Some extra questions:
Imagine I'm sending a file. How would my function know that the current data is related to the previous message?
How is my class design and implementation? SHould I change anything?
Thank you
If by "portable" you mean runs on other platforms besides Windows then a recv() loop in a worker thread is your only portable option. On Windows specifically, you have some additional choices:
Allocate a hidden window and then use WSAAsyncSelect() to receive FD_READ notifications. This requires a message loop, which you can put in a worker thread.
Use WSAEventSelect() to register a waitable event for FD_READ notifications and then wait for those events via WSAWaitForMultipleEvents() in a thread.
use WSARecv() with an I/O Completion Port. Poll the IOCP via GetQueuedCompletionResult() in a thread.
As for your question regarding messaging, TCP is a byte stream, it has no concept of messages. You have to frame your messages yourself. You can either:
give each message a fixed header that contains the message length. Read the header first, then read however many bytes it says, then read the next header, and so on.
separate each message with a unique delimiter that does not appear in the message data. Read until you encounter that delimiter, then read until the next delimiter, and so on.
Have your event loop call either poll or select to determine if there is data that can be read on the socket(s). Then read it, and call the appropriate callback function.