Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Simple POSIX sockets Wrapper (C++)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
ZeuZ_NG
Guru
Guru


Joined: 10 Sep 2008
Posts: 396
Location: Near /dev/urandom | /dev/null

PostPosted: Thu Sep 05, 2013 3:16 pm    Post subject: Simple POSIX sockets Wrapper (C++) Reply with quote

Hey all, so I've gotten to this point trying to use the latest recommended by manpages and some help from ##posix:
EDIT: added code for reliability on Enviar() (send method)
Code:

#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <string>
#ifndef DEBUG
   #include <iostream>
#endif
class Conexion {
private:
   int estadoDeOperacion,Socket;
   struct addrinfo *resultados;
   struct addrinfo banderas;
   struct addrinfo *resultado;
public:
   ~Conexion();
   Conexion();
   bool Conectado();
   void Desconectar();
   bool Conectar(std::string host, std::string puerto);
   bool Enviar(std::string datos);
   std::string Recibir();
};
Conexion::~Conexion(){
   if (Socket>0) close(Socket);
   freeaddrinfo(resultados);
   freeaddrinfo(resultado);
}
bool Conexion::Conectado(){
   return (Socket>0);
}
bool Conexion::Conectar(std::string host, std::string puerto) {
   memset(&banderas,0,sizeof banderas);
   banderas.ai_family = AF_UNSPEC;
   banderas.ai_socktype = SOCK_STREAM;
   banderas.ai_flags = AI_PASSIVE;
   estadoDeOperacion = getaddrinfo(host.c_str(),puerto.c_str(),&banderas,&resultados);
   if (estadoDeOperacion != 0) {
      return false;
   }
   for (resultado = resultados; resultado != NULL; resultado = resultado->ai_next) {
      estadoDeOperacion = socket(resultado->ai_family,resultado->ai_socktype,resultado->ai_protocol);
      if (estadoDeOperacion == -1) {
         continue;
      }
      estadoDeOperacion = connect(Socket,resultado->ai_addr,resultado->ai_addrlen);
      if (estadoDeOperacion==-1) {
         close(Socket);
         continue;
      }
      break;
   }
   if (resultado == NULL) {
      return false;
   }
   freeaddrinfo(resultados);
   return true;
}
void Conexion::Desconectar() {
   close(Socket);
}
bool Conexion::Enviar(std::string datos) {
   size_t bytesEnviados = estadoDeOperacion=0;
   char buffer[1024] = "";
   if (Conectado()) {
      do {
         try {
            strcpy(buffer,datos.substr(bytesEnviados,1023).c_str());
         }
         catch(const std::exception) {
            strcpy(buffer,datos.substr(bytesEnviados,datos.size()).c_str());
         }
         estadoDeOperacion = send(Socket,buffer,1024,0);
         if (estadoDeOperacion==-1) return false;
         else bytesEnviados+=estadoDeOperacion;
      } while (bytesEnviados != datos.size());
   }
   return true;
}



However I need some help for returning the entire message that comes back as a string, what happens if the message comes in fragmented in different packets? (this would be on the method Conexion::Recibir(std::string))
_________________
| Intel Core i7 920. | Intel DX58SO Extreme. |
| 8gb DDR3 1666mHz | 1TB 7200 RPM WD HDD.|
Unix´s guru view of sex:
unzip ; strip ; touch ; grep ; finger ; \
mount ; fsck ; more ; yes ; umount ; sleep.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum