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)
{
...
}
Related
I'd like to store my serialized data on redis and publish to the channel that i've defined. But it seems that there is a problem on SETting the key value at redis. What do i miss for the solution ?
Thanks in advance.
#include <stdio.h>
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include "hiredis.h"
#include "async.h"
#include "macosx.h"
#define PACKETSIZE sizeof(cloudRANMessage)
#define COMMANDSIZE 256
typedef struct cloudRANMessage
{
unsigned int station_id;
unsigned int location_area;
char command[COMMANDSIZE];
}cloudRANMessage;
void printMyMessage(cloudRANMessage *message)
{
printf("%d\n", message->location_area);
printf("%d\n", message->station_id);
printf("%s\n", message->command);
}
void serialize(cloudRANMessage *message, char *data)
{
assert(data != NULL);
memcpy(data, message, sizeof *message);
}
void deserialize(char *data)
{
cloudRANMessage *tempMessage = malloc(sizeof(cloudRANMessage)); // To store deserialized message.
memset(tempMessage, 0, sizeof(cloudRANMessage));
memcpy(tempMessage, data, sizeof(cloudRANMessage));
printMyMessage(tempMessage);
}
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("%s\n", reply->str); // Call deserializaton function for the data retrieval.;
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect(c);
}
void callbackDeserialize(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("%s\n", reply->str); // Call deserializaton function for the data retrieval.
char *stringReply = reply->element[0]->str;
deserialize(stringReply);
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect(c);
}
void connectCallback(const redisAsyncContext *c, int status) {
if (status != REDIS_OK) {
printf("Error: %s\n", c->errstr);
return;
}
printf("Connected...\n");
}
void disconnectCallback(const redisAsyncContext *c, int status) {
if (status != REDIS_OK) {
printf("Error: %s\n", c->errstr);
return;
}
CFRunLoopStop(CFRunLoopGetCurrent());
printf("Disconnected...\n");
}
int main (int argc, char **argv) {
cloudRANMessage *newcloudRANMessage = malloc(sizeof(cloudRANMessage));
newcloudRANMessage->location_area = 7214;
newcloudRANMessage->station_id = 45632;
strcpy(newcloudRANMessage->command, "HANDOVER\0");
char data[PACKETSIZE];
serialize(newcloudRANMessage, data);
signal(SIGPIPE, SIG_IGN);
CFRunLoopRef loop = CFRunLoopGetCurrent();
if( !loop ) {
printf("Error: Cannot get current run loop\n");
return 1;
}
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
return 1;
}
redisMacOSAttach(c, loop);
redisAsyncSetConnectCallback(c,connectCallback);
redisAsyncSetDisconnectCallback(c,disconnectCallback);
redisAsyncCommand(c,getCallback,NULL,"SUBSCRIBE cloudRAN");
// Serialize Data then send to Redis
//redisAsyncCommand(c, getCallback, (char*) "SET", "SET LTEdata %s", data, strlen(data)); // key for our data in this case is LTEdata
redisAsyncCommand(c,NULL, NULL, "SET LTEdata %s", data);
//redisAsyncCommand(c, getCallback,(char*) "GET", "GET LTEdata");
redisAsyncCommand(c, callbackDeserialize,NULL, "GET LTEdata");
// Publish the information to the all subscribers.
redisAsyncCommand(c,NULL, NULL, "PUB cloudRAN %b",data,strlen(data));
CFRunLoopRun();
return 0;
}
In this call to redisAsyncCommand:
redisAsyncCommand(c,NULL, NULL, "SET LTEdata %s", data);
any null bytes appearing in data will terminate the hiredis string interpolation. Since you are binary encoding, this is likely to be truncating your string. Try specifying the length to make it binary-safe:
redisAsyncCommand(c,NULL, NULL, "SET LTEdata %b", data, sizeof(data));
I have been looking for several examples (on this page and others) of how to get the USB flash memory path folder (something like /media/john/B5D6-DC71) in order to make a c++ program to save files on this device. I mean, I want that the program detect a new device and get its folder path for (as I said) saving files on it.
I found a good one using libudev library I have changed it a bit and the code does everything what I want except giving the path. It gives me a lot of attributes and functions that I can find here: https://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/libudev-udev-device.html. But I didn't find what I want... maybe it is there but I made a mistake or maybe it is no there...
Anyone can help me?
Here is the code I use:
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <string.h>
#include <iostream>
using namespace std;
static
struct udev_device*
get_child(struct udev* udev, struct udev_device* parent, const char* subsystem) {
struct udev_device* child = NULL;
struct udev_enumerate *enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_parent(enumerate, parent);
udev_enumerate_add_match_subsystem(enumerate, subsystem);
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry;
udev_list_entry_foreach(entry, devices) {
const char *path = udev_list_entry_get_name(entry);
child = udev_device_new_from_syspath(udev, path);
break;
}
udev_enumerate_unref(enumerate);
return child;
}
static void enumerate_usb_mass_storage(struct udev* udev) {
struct udev_enumerate* enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "scsi");
udev_enumerate_add_match_property(enumerate, "DEVTYPE", "scsi_device");
udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
struct udev_list_entry *entry;
udev_list_entry_foreach(entry, devices) {
const char* path = udev_list_entry_get_name(entry);
struct udev_device* scsi = udev_device_new_from_syspath(udev, path);
struct udev_device* block = get_child(udev, scsi, "block");
struct udev_device* scsi_disk = get_child(udev, scsi, "scsi_disk");
struct udev_device* usb
= udev_device_get_parent_with_subsystem_devtype(scsi, "usb", "usb_device");
if (block && scsi_disk && usb) {
printf("block = %s, usb = %s:%s, scsi = %s, name = %s, size = %s, x = %i, x = %s, x = %s\n",
udev_device_get_devnode(block),
udev_device_get_sysattr_value(usb, "idVendor"),
udev_device_get_sysattr_value(usb, "idProduct"),
udev_device_get_sysattr_value(scsi, "vendor"),
udev_device_get_sysattr_value(usb, "udev"),
udev_device_get_sysattr_value(usb, "udev_root"),
udev_device_get_sysattr_value(usb, "size"),
udev_device_get_sysattr_value(usb, "speed"),
udev_device_get_sysattr_value(usb, "bMaxPower"));
}
if (block) {
udev_device_unref(block);
}
if (scsi_disk) {
udev_device_unref(scsi_disk);
}
udev_device_unref(scsi);
}
udev_enumerate_unref(enumerate);
}
int main(){
struct udev* udev = udev_new();
enumerate_usb_mass_storage(udev);
struct udev_device *dev;
struct udev_monitor *mon;
int fd;
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "block", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);
while (1) {
fd_set fds;
struct timeval tv;
int ret;
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;
ret = select(fd+1, &fds, NULL, NULL, &tv);
/* Check if our file descriptor has received data. */
if (ret > 0 && FD_ISSET(fd, &fds))
{
printf("\nselect() says there should be data\n");
dev = udev_monitor_receive_device(mon);
if (dev) {
printf("Got Device\n");
printf(" Node: %s\n", udev_device_get_devnode(dev));
printf(" Subsystem: %s\n", udev_device_get_subsystem(dev));
printf(" Devtype: %s\n", udev_device_get_devtype(dev));
printf(" Action: %s\n", udev_device_get_action(dev));
cout << udev_get_run_path (dev) << endl;
if( strcmp(udev_device_get_action(dev),"add")==0 ) cout << "conectat" << endl;
udev_device_unref(dev);
}
else {
printf("No Device from receive_device(). An error occured.\n");
}
}
usleep(250*1000);
fflush(stdout);
}
udev_unref(udev);
return 0;
}
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);
^^^^^
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.
I've been at this for some time now. I need a basic IRC Ping Pong function to return the the proper response when the IRC server pings. I changed the name of the function get() to something else and I'm still getting the error. I thought perhaps the function name get() was already defined in one of the includes or something else.
#include "stdafx.h"
#include "Ping_Pong.h"
#include <iostream>
#include <ws2tcpip.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
#define MT4_EXPFUNC __declspec(dllexport)
#pragma comment(lib, "Ws2_32.lib")
class MT4_EXPFUNC IRC {
private:
char buf[513];
char rbuf[513];
char sbuf[513];
char *tok;
int recv_bytes;
int irc_socket;
struct addrinfo hints;
struct addrinfo *results;
public:
char *nick, *user, *host, *chan, *type, *mesg;
int irc_connect(const char *host, const char *port, const char *nick);
void socket_err(const char* err_string);
//int join(const char *channel);
This is the name of the function in question
int __stdcall get();
char *check(const char* test_str);
char *pop_arg(char **save_ptr);
int init_comarg();
int say(const char *channel, const char *message);
int sayf(const char *channel, const char *message, ...);
int mode(const char *channel, const char *mode, char *target);
//void die();
};
And this is the function I'm having a problem with.
MT4_EXPFUNC int __stdcall IRC::get()
{
memset(rbuf, 0, 513);
recv_bytes = recv(irc_socket, rbuf, sizeof(rbuf), 0);
if (recv_bytes <= 0) {
return -1;
}
std::cout << rbuf;
// Auto-Respond to PING messages.
if (rbuf[0] == 'P' && rbuf[1] == 'I') {
tok = strtok(rbuf, "PING :");
sprintf(buf, "PONG %s", tok-1 );
send(irc_socket, buf, strlen(buf), 0);
std::cout << buf;
memset(buf, 0, 513);
}
if ( strstr(rbuf, "PRIVMSG")) {
memcpy(sbuf, rbuf, 513);
nick = strtok(sbuf, "!") + 1;
user = strtok(NULL, "#");
host = strtok(NULL, " ");
type = strtok(NULL, " ") - 1;
chan = strtok(NULL, " ");
mesg = strtok(NULL, ":");
}
return 1;
}
In the tutorial linked below, a .def file was suggested with the following code:
Library "Ping_Pong"
Export get
I removed the .def and it worked.
http://www.xpworx.com/metatrader-mql-articles/mql4-articles/create-your-own-metatrader-extension-dll.php