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

Joined: 10 Sep 2008
Posts: 453
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)

#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <string>
#ifndef DEBUG
   #include <iostream>
class Conexion {
   int estadoDeOperacion,Socket;
   struct addrinfo *resultados;
   struct addrinfo banderas;
   struct addrinfo *resultado;
   bool Conectado();
   void Desconectar();
   bool Conectar(std::string host, std::string puerto);
   bool Enviar(std::string datos);
   std::string Recibir();
   if (Socket>0) close(Socket);
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) {
      estadoDeOperacion = connect(Socket,resultado->ai_addr,resultado->ai_addrlen);
      if (estadoDeOperacion==-1) {
   if (resultado == NULL) {
      return false;
   return true;
void Conexion::Desconectar() {
bool Conexion::Enviar(std::string datos) {
   size_t bytesEnviados = estadoDeOperacion=0;
   char buffer[1024] = "";
   if (Conectado()) {
      do {
         try {
         catch(const std::exception) {
         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