socket_connect.c 1.01 KB
#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/types.h>  /* SO_REUSEADDR */
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */
#include <errno.h>      /* for errno */
#include <stdarg.h>

#include "logger.h"
#include "cclass.h"
#include "socket.h"


void
sock_connect(SOCK this, const char * addr)
{
	inet_pton(AF_INET, addr, &((this->addr).sin_addr));
    (this->addr).sin_family = AF_INET;           /* Internet address family */
    (this->addr).sin_port   = htons(this->port); /* Local port */

	if (-1 == connect(this->handle, (struct sockaddr*) &(this->addr), sizeof(this->addr))) {
        logger_log(this->logger, LOGGER_CRIT,
                "error connection socket: %s - service terminated",
                strerror(errno));
        exit(EXIT_FAILURE);
    }
}

// vim: set ts=4 sw=4: