Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../global.php(29) : eval()'d code(6) : eval()'d code on line 1

PHP Warning: Use of undefined constant misc - assumed 'misc' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant index - assumed 'index' (this will throw an Error in a future version of PHP) in ..../includes/class_bootstrap.php(1422) : eval()'d code(4) : eval()'d code on line 1

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 85

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 85

PHP Warning: Use of undefined constant onlinestatusphrase - assumed 'onlinestatusphrase' (this will throw an Error in a future version of PHP) in ..../includes/class_core.php(4684) : eval()'d code on line 6
Simple winsock tcp client
Results 1 to 5 of 5

Thread: Simple winsock tcp client

  1. #1

    Simple winsock tcp client

    Hello guys.

    Its been long time since i last time posted here but here we go. This thing is a simple library that i wrote to make winsock usage easier. It uses blocking sockets and is meant to be simple and easy to use

    I would like if someone would take a quick look on my code and give me some feedback

    Feel free to use and comment, i will include a zip file in attachments with same code + a quick testing program source and binary.


    network.c
    Code:
    #include "network.h"
    
    /*
    functions connect, send, recv will call closesocket on failure automaticly
    
    
    
    */
    
    
    int network_init() {
        int ret;
    
        ret = WSAStartup(MAKEWORD(2,2), &wsaData);      /* initalize winsock */
    
        if(ret != 0) {
            printf("[network]: WSAStartup failed with error: %d\n", ret);
            return -1;
        }
    
        return 0;
    }
    
    int network_createSocket(network_t* net) {
    
        net->sock = INVALID_SOCKET;
    
        net->sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);     /*create TCP socket */
    
        if(net->sock == INVALID_SOCKET) {
            printf("[network]: socket failed with error: %d\n", WSAGetLastError());
            return -1;
        }
        return 0;
    
    }
    
    
    int network_connect(network_t* net, char* address, int port) {
        int ret;
    
        memset(&net->addressInfo, 0, sizeof(net->addressInfo));       /* make sure addressinfo structure is empty */
    
        net->addressInfo.sin_family = AF_INET;
        net->addressInfo.sin_addr.s_addr = inet_addr(address);
        net->addressInfo.sin_port = htons(port);
    
        ret = connect(net->sock, (SOCKADDR*)&net->addressInfo, sizeof(net->addressInfo));        /* attempt to connect to server */
    
        if(ret != 0) {
            printf("[network]: Unable to connect to server!\n");
            closesocket(net->sock);
            return -1;
        }
    
        return 0;
    }
    
    int network_send(network_t* net, char* buffer, int lenght) {
        int ret;
    
        ret = send(net->sock, buffer, lenght, 0);
        if(ret == SOCKET_ERROR) {
            printf("[network]: send failed with error: %d\n", WSAGetLastError());
            closesocket(net->sock);
            return -1;
        }
    
        return ret;
    
    }
    
    int network_recv(network_t* net, char* buffer, int size) {
        int ret;
    
        ret = recv(net->sock, buffer, size, 0);
        if(ret == -1) {
            printf("[network]: recv failed with error: %d\n", WSAGetLastError());
            closesocket(net->sock);
            return -1;
        }
    
        return ret;
    }
    
    /* make sure you have enough room in output buffer */
    
    int network_resolve(char* address, char* output) {
        int ret;
        struct addrinfo hints;
        struct addrinfo *result;
        struct sockaddr_in  *sockaddr_ipv4;     /* for converting addresses to ascii */
    
        memset(&hints, 0, sizeof(hints));
    
        hints.ai_family = AF_INET;      /* not sure if it will force this to ipv4 but hope so ;) */
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
    
    
    
        ret = getaddrinfo(address, NULL, &hints, &result);      /* allocates memory to "result" structure, needs to be free'ed */
        if(ret != 0) {
            printf("[network]: getaddrinfo failed with error: %d\n", WSAGetLastError());
            return -1;
        }
    
        sockaddr_ipv4 = (struct sockaddr_in *) result->ai_addr;
    
    
        /*printf("IPv4 address %s\n", inet_ntoa(sockaddr_ipv4->sin_addr)); */
    
        strcpy(output, inet_ntoa(sockaddr_ipv4->sin_addr));     /* we only take the first address from list, but it seems to work just fine */
    
    
        freeaddrinfo(result);       /* here we free that memory */
    
        return 0;
    }
    
    int network_close(network_t* net) {
        closesocket(net->sock);
    
        return 0;
    }
    
    int network_cleanup() {
        WSACleanup();
    
        return 0;
    
    }
    network.h
    Code:
    #ifndef NETWORK_H
    #define NETWORK_H
    
    #define _WIN32_WINNT  0x501     /* i love this windows stuff */
    
    
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>       /* even more love here */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    typedef struct network_t {
        SOCKET sock;
        SOCKADDR_IN addressInfo;
    
    }network_t;
    
    
    WSADATA wsaData;
    
    
    int network_init();
    
    int network_createSocket(network_t* net);
    int network_connect(network_t* net, char* address, int port);
    int network_send(network_t* net, char* buffer, int lenght);
    int network_recv(network_t* net, char* buffer, int size);
    
    /* dns stuff */
    int network_resolve(char* address, char* output);
    
    int network_close(network_t* net);
    
    int network_cleanup();
    
    
    
    #endif
    Attached Files Attached Files

  2. #2
    This is great if you're trying to interface with Tibia's protocol or some other proprietary TCP protocol. If you are, however, implementing your own protocol in C/C++, check out ZMQ.

  3. #3
    Sorry i forgot to mention, this isint directly related to tibia Iam working on a wget clone for windows.

    Mainly i just want some feedback about how my code is structured etc.
    I <3 Boting

  4. #4
    Quote Originally Posted by Uggis View Post
    Sorry i forgot to mention, this isint directly related to tibia Iam working on a wget clone for windows.

    Mainly i just want some feedback about how my code is structured etc.

    If that's your goal, look into winhttp. Much nicer for a wget style application.

  5. #5
    Quote Originally Posted by DarkstaR View Post
    If that's your goal, look into winhttp. Much nicer for a wget style application.
    Ill pass, i want to learn to write better C so im doing it "the hard way"
    I <3 Boting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •