Jakash3’s Socket Class

Download Jakash3’s Socket Class
A socket class created by me. Supports both Windows and Linux. For Download and Documentation, click the link above. Here’s the sauce:
sock.h

#ifndef SOCK_H
#define SOCK_H

#if defined(_WIN32)
#include "WinSock2.h"
#include "Ws2tcpip.h"
#pragma comment(lib,"ws2_32.lib")
#elif defined(linux)
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#else
#error _WIN32 or linux not defined, your operating system may not be supported.
#endif

#include <stdlib.h>
#include <string.h>

class sock {
public:
    sock(int buffer_size = 4096);
#ifdef _WIN32
    ~sock() {WSACleanup();}
    SOCKET sockfd;
#elif defined(linux)
    int sockfd;
#endif
    char* data;
    void* eol;
    size_t eol_len;
    int set_eol(void* eol_sequence, size_t size);
    int link(const char* hostname, int port);
    int unlink();
    int watch(int port,int backlog);
    int open(sock& client);
    int writeln();
    int writeln(const char* buffer);
    int write(const char* buffer, int len);
    int write(int len);
    int readln(char* buffer);
    int readln();
    int read(char* buffer, int len);
    int read(int len);
    int set_buf(int len);
    sock& operator<< (const char* buffer);
    sock& operator>> (char* buffer);
    const char* peerip();
private:
    char tmp[4096];
};
#endif

sock.cpp

#include "sock.h"

sock::sock(int buffer_size) {
#ifdef _WIN32
    WSADATA wd;
    WSAStartup(0x0101,&wd);
#endif
    data = (char*)malloc(buffer_size);
    eol = (char*)malloc(2);
    eol_len = 2;
    memmove(eol,"\r\n",2);
    tmp[0] = '';
}

int sock::link(const char* hostname, int port) {
    if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) return 0;
    struct sockaddr_in sa;
    sa.sin_addr = *(struct in_addr*)gethostbyname(hostname)->h_addr;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    memset(&(sa.sin_zero),0,8);
    if (connect(sockfd, (struct sockaddr*)&sa, sizeof(struct sockaddr))) return 0;
    return 1;
}

int sock::watch(int port, int backlog) {
    if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) return 0;
    struct sockaddr_in sa;
    sa.sin_addr.s_addr = INADDR_ANY;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    memset(&(sa.sin_zero),0,8);
    if (bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr))) return 0;
    if (listen(sockfd,backlog)) return 0;
    return 1;
}

int sock::open(sock& s) {
    socklen_t slen;
    struct sockaddr_in sa;
    if ((s.sockfd = accept(sockfd,(struct sockaddr*)&sa,&slen)) == -1) return 0;
    strcpy(s.data,inet_ntoa(sa.sin_addr));
    return 1;
}

int sock::writeln(const char* buffer) {
    int i, j;
    for (i = 0, j = 0; memcmp(buffer + i, eol, eol_len); i++)
        j += send(sockfd, buffer + i, 1, 0);
    j += send(sockfd, (const char*)eol, eol_len, 0);
    return j;
}

int sock::writeln() {
    int i, j;
    for (i = 0, j = 0; memcmp(data + i, eol, eol_len); i++)
        j += send(sockfd, data + i, 1, 0);
    j += send(sockfd, (const char*)eol, eol_len, 0);
    return j;
}

int sock::write(const char* buffer, int len) {return send(sockfd, buffer, len, 0);}
int sock::write(int len) {return send(sockfd, data, len, 0);}

int sock::readln() {
    int i = 0, j = 0, k = 0;
    while (memcmp(data + j, eol, eol_len)) {
        if (i - j == eol_len) j++;
        k += recv(sockfd, data + i, 1, 0);
        i++;
    }
    data[i] = '';
    return k;
}

int sock::readln(char* buffer) {
    int i = 0, j = 0, k = 0;
    while (memcmp(buffer + j, eol, eol_len)) {
        if (i - j == eol_len) j++;
        k += recv(sockfd, buffer + i, 1, 0);
        i++;
    }
    buffer[i] = '';
    return k;
}

int sock::read(int len) {return recv(sockfd, data, len, 0);}
int sock::read(char* buffer, int len) {return recv(sockfd,buffer,len,0);}

sock& sock::operator<< (const char* str) {
    strcat(tmp,str);
    if (!memcmp(str+strlen(str)-eol_len,eol,eol_len)) {
        write(tmp,strlen(tmp));
        tmp[0] = '';
    }
    return *this;
}

sock& sock::operator>> (char* buffer) {
    readln(buffer);
    return *this;
}

int sock::unlink() {
#ifdef _WIN32
    shutdown(sockfd, SD_BOTH);
    return !closesocket(sockfd);
#elif defined(linux)
    shutdown(sockfd, SHUT_RDWR);
    return !close(sockfd);
#endif
}

int sock::set_buf(int size) {
    char* tmp = (char*)realloc(data, size);
    if (tmp)
        data = tmp;
    else
        return 0;
    return 1;
}

int sock::set_eol(void* eol_sequence, size_t size) {
    eol_len = size;
    if (!(eol=malloc(size))) return 0;
    if (!(memmove(eol,eol_sequence,size))) return 0;
    return 1;
}
sock.cpp
const char* sock::peerip() {
    socklen_t len = sizeof(struct sockaddr_in);
    struct sockaddr_in sa;
    if (getsockname(sockfd, (struct sockaddr*)&sa, &len)) return 0;
    return inet_ntoa(sa.sin_addr);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: