General Socket Wrapper
Go to file
2018-06-11 14:25:24 +08:00
.gitignore Initial Commit 2017-07-27 19:52:58 +08:00
gsock_helper.cpp Update helper 2018-06-11 11:45:50 +08:00
gsock_helper.h Update helper 2018-06-11 11:45:50 +08:00
gsock.cpp Fix inet_ntop wrong usage 2018-06-07 20:04:28 +08:00
gsock.h Better document. 2018-06-06 01:04:44 +08:00
LICENSE Add MIT License 2018-02-11 15:44:02 +08:00
Readme.md Update Readme.md 2018-06-11 14:25:24 +08:00
VERSION Bump version 2018-05-09 21:35:39 +08:00

General Socket Wrapper

GSOCK is a project created by Kiritow.

Licensed under MIT

Example

TCP Client Example

#include "GSock/gsock.h"
#include "GSock/gsock_helper.h"

int main()
{
    int ret;
    std::string ip;
    if ((ret = DNSResolve("kiritow.com", ip)) < 0)
    {
        printf("Failed to resolve ip address. (%d)\n", ret);
        return 1;
    }
    
    sock s;
    if ((ret = s.connect(ip, 80)) < 0)
    {
        printf("Failed to connect. (%d)\n", ret);
        return 1;
    }

    std::string context("GET / HTTP/1.1\r\nHost: kiritow.com\r\nAccept: */*\r\n\r\n");
    sock_helper sh(s);
    if ((ret=sh.sendall(context.c_str(), context.size())) < 0)
    {
        printf("Failed to send request header. (%d)\n", ret);
        return 1;
    }

    char buff[1024];
    while (true)
    {
        memset(buff, 0, 1024);
        int ret = s.recv(buff, 1024);
        if (ret <= 0) break;
        printf("%s", buff);
    }

    return 0;
}