Bonjour à tous,
Je travaille actuellement sur la conception d'un bot pour D2 de type socket en C++.
J'ai actuellement coder la connexion au serveur d'authentification, jusqu'ici tout va bien, le bot se connecte correctement.
Cliquez pour révéler
Cliquez pour masquer
Code utilisé pour se connecter.
// MAIN.CPP
#include <iostream>
#include "Network.hpp"
#include "Exception.hpp"
#define PORT (5555)
#define IP ("213.248.126.39")
int main()
{
try
{
Network *n;
n = new Network(PORT, IP);
std::cout << "Tentative de connection sur : " << n->getIp() << ":" << n->getPort() << std::endl;
n->initialize();
n->connection();
n->process();
n->closeSocket("");
}
catch (Except *e)
{
std::cerr << "[ERROR LOG] " << e->what() << std::endl;
}
}
// NETWORK.CPP
#include "Network.hpp"
#include "Exception.hpp"
#include <stdio.h>
Network::Network(const int p, const std::string &_ip) : ip(_ip)
{
this->port = p;
}
Network::~Network()
{
}
void Network::initialize()
{
struct protoent *pe;
int errsv;
pe = getprotobyname("TCP");
if (!pe)
{
errsv = errno;
std::string error = "Error on getprotobyname [";
error += strerror(errsv);
error += "]";
throw Except(error);
}
this->socketFd = socket(AF_INET, SOCK_STREAM, pe->p_proto);
if (this->socketFd == -1)
{
errsv = errno;
std::string error = "Error on socket [";
error += strerror(errsv);
error += "]";
throw Except(error);
}
}
void Network::connection()
{
struct sockaddr_in sin;
int errsv;
sin.sin_family = AF_INET;
sin.sin_port = htons(this->port);
sin.sin_addr.s_addr = inet_addr(this->ip.c_str());
int ret = connect(this->socketFd, (struct sockaddr *)&sin, sizeof(sin));
if (ret == -1)
{
errsv = errno;
std::string error = "Error on connect [";
error += strerror(errsv);
error += "]";
this->closeSocket(error);
throw Except(error);
}
std::cout << "Connection to the server successfully established" << std::endl;
}
void Network::process()
{
}
int Network::getPort() const
{
return (this->port);
}
const std::string &Network::getIp() const
{
return (this->ip);
}
void Network::closeSocket(const std::string &e)
{
if (close(this->socketFd) == -1)
{
std::string err = e;
err += "\nError on close [";
int errsv = errno;
err += strerror(errsv);
err += "]";
throw Except(err);
}
}
// NETWORK.HPP
#ifndef NETWORK_HPP__
#define NETWORK_HPP__
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string>
#include <iostream>
#include <string.h>
class Network
{
private :
int socketFd;
int port;
const std::string ip;
public :
Network(const int, const std::string &);
~Network();
void initialize();
void connection();
void process();
void closeSocket(const std::string &);
int getPort() const;
const std::string &getIp() const;
};
#endif
Si vous avez des sugestions / remarques, n'hésiter pas.
Mais maintenant se pose le probleme du thread et j'aurais quelque question à vous poser :
- est-il obligatoire de threader la connexion ?
- comment si prendre ?
Pour le moment j'ai testé avec un simple recv et j'ai réussis à récuperer le premier paquet (id = 1) mais je me demandais si utiliser recv était la meilleure solution ou si il y avait mieux à faire (utiliser select() et/ou la lib pthread ?)
Voici mon code utilisé pour lire les paquets et récupérer l'id.
unsigned char buffer[8192 + 1];
int len = recv(this->socketFd, buffer, 5000, 0);
if (len > 0)
{
buffer[len] = 0;
std::cout << "taille lu : " << len << std::endl;
short int header = (unsigned char)buffer[0] << 8 | (unsigned char)buffer[1];
int id = header >> 2;
std::cout << "id : [" << id << "]" << std::endl;
std::cout << "===========MESSAGE=============" << std::endl;
for (int j=0;j<len;j++)
{
printf("%02x ",(unsigned char)buffer[j]);
}
std::cout << std::endl;
std::cout << "===============================" << std::endl;
}
Et le résultat obtenue en console :
Loading Image
Merci d'avance,
Cordialement,
Kangogoo.
Ps : comme vous pouvez le constater je code sous Linux (avec emacs et g++)