mercredi 1 mars 2017

Winsock Socket TCP/IP. If statement showing a socket is equal to ~0 when it is not ( or is ?? )

This program uses an if statement (lines 90-94) to check to see if the SOCKET SOCK_desc is equal to INVALID_SOCKET (~0) . The problem is, even though it seems as SOCK_desc didn't get a return value of ~0 , according to the WSAGetLastError() function and it's value , the statement beleives it is. Resulting in termination of the program. Help on why this is happening will be appreciated, thanks :) .

[main.cpp]

    #include "dexaBOT.h"

    #define _WIN32_WINNT  0x501

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #include <ws2tcpip.h>
    #include <time.h>

    #define MAXDATASIZE 100


    dexaBOT::dexaBOT(char * BOTnickname, char * BOTusername){

            nick = BOTnickname;
            usr = BOTusername;

            std::cout << "|-------START-------|" << std::endl;

            std::cout << nick << std::endl;
            std::cout << usr << std::endl;



    }


    dexaBOT::~dexaBOT(){

        std::cout << "|-------END-------|" << std::endl;


    }

    void dexaBOT::BOTstart(){


            startup = true;

            int resultint;
            port = "6667";

            WSADATA BOTwsa;
            struct addrinfo BOThints;
            struct addrinfo *BOTservinfo = NULL;






            ZeroMemory(&BOThints, sizeof(BOThints));

            BOThints.ai_family = AF_UNSPEC;
            BOThints.ai_socktype = SOCK_STREAM;
            BOThints.ai_protocol = IPPROTO_TCP;

            if (resultint = WSAStartup(MAKEWORD(2,2), &BOTwsa) != 0){
                std::cout << "FAILURE TO START WSA DLL" << std::endl;
            }



            if (resultint = getaddrinfo("192.168.2.69", port, &BOThints, &BOTservinfo) != 0){
                startup = false;
                std::cout << "DNS Translation / ANSI Translation Failure ::: " << WSAGetLastError() << std::endl;
            }


            SOCK_desc = socket(BOTservinfo->ai_family, BOTservinfo->ai_socktype, BOTservinfo->ai_protocol);


            std::cout << "\n-Socket Info---------------" << std::endl;

            std::cout << BOTservinfo->ai_family << std::endl;
            std::cout << BOTservinfo->ai_socktype << std::endl;
            std::cout << BOTservinfo->ai_protocol << std::endl;

            std::cout << "--------------------------" << std::endl;


            std::cout << "Socket & Error Code  \n" << SOCK_desc << std::endl;
            std::cout << WSAGetLastError() << std::endl;

            std::cout << "--------------------------" << std::endl;

            if(SOCK_desc = INVALID_SOCKET){
                std::cout << "Socket Object failed to be created ::: " << WSAGetLastError() << std::endl;
                closesocket(SOCK_desc);

            }


            if (connect(SOCK_desc, BOTservinfo->ai_addr, (int)BOTservinfo->ai_addrlen) == SOCKET_ERROR){
                std::cout << "Socket Failed to connect to host ::: " << WSAGetLastError() << std::endl;
                closesocket(SOCK_desc);
            }

            freeaddrinfo(BOTservinfo);






    }


    int main()
    {

        dexaBOT bot_one = dexaBOT("USERNAME", "TEST");


        bot_one.BOTstart();
        WSACleanup();





        return 0;
    }

[dexaBOT.h]

    #ifndef dexaBOT_H_INCLUDED
    #define dexaBOT_H_INCLUDED

    #include <windows.h>

     class dexaBOT {

        public:


                        dexaBOT(char *, char *);
                        virtual ~dexaBOT();


                        bool startup;

                        void BOTstart();
                        bool charSearch(char *, char*);


        private:


                        char *port;
                        SOCKET SOCK_desc = INVALID_SOCKET;

                        char *nick;
                        char *usr;

                        bool BOTisConnected;
                        void localTime();
                        bool BOTsendData(char *BOTmsg);
                        void BOTsendPong(char *buf);
                        void msgHandle(char *buf);



     };



    #endif // ** dexaBOT_H_ ** //

Aucun commentaire:

Enregistrer un commentaire