1

Okay, before anyone jumps to conclusions (and do to the fact of me keeping this relevant to others), basically, I'm trying to make a Get Request with sockets in C/C++ using Winsock.h.
[Main problem existing in running of the SocketRequest Function]
I've been getting error response codes anywhere from: "WSAENOTCONN 10057" to "WSAENOTSOCK 10038" which, I found more information on that here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

Now, this is going to be a little irrelevant to others, but more to my situation. I'm trying to implement this on an XBOX360 Devkit, using "winsockx.h" and it crashes when I make a "SocketRequest"... here's the code WITH output for debugging purposes. (I understand this isn't normally what you'd see, so take it how you want)

Here's the included files:

#include "stdafx.h"
#include <time.h>
#include "Hooks.h"
#include <xhttp.h>
#include <winsockx.h>
#include <iostream>
#include <xtl.h>
#include <xbdm.h>
#include <malloc.h>
#include <stdio.h>

Here's the definitions and instances:

int Socket;
struct sockaddr_in SocketAddress;
char bufferReturn[10000];
char serverAddr[2000];
bool returnTEST = false;
char *Request1;
char *Request2;
char *ResetRequest;
//http://www.cplusplus.com/forum/general/9403/
//http://www.cplusplus.com/forum/beginner/139313/

#define SERVER_PORT htons(80)

Here's the code (Including the SocketRequest function that fails to run):

char* SocketRequest(char* URL, char* Path = "")
{
//THIS IS THE FUNCTION THAT CRASHES WHEN IT'S CALLED IDK WHY... NOTHING IN HERE RUNS, NOT EVEN THE "prinf" STATEMENT. 
    printf( "Step 0");
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSA Startup FAILED! ", WSAGetLastError() );
    }
    printf( "Step 1");
    SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    printf( "Step 2");
    SocketAddress.sin_family = AF_INET;
    printf( "Step 3");
    SocketAddress.sin_port = SERVER_PORT;
    printf( "Step 4");
    Socket = socket(AF_INET, SOCK_STREAM, 0);

    printf( "Step 5");
    // Make sure we have a valid socket
    if( Socket == INVALID_SOCKET )
    {
        printf( "Attempted to send to an invalid socket.\n" );
    }
    printf( "Step 6");
    // If we're not broadcasting, make sure we have a peer to send to
    if( SocketAddress.sin_addr.s_addr == INADDR_NONE )
    {
        printf( "Attempted to send a non-broadcast when we have no peer.\n" );
    }
    printf( "Step 7");
    if( bind( Socket, ( const sockaddr* )&SocketAddress, sizeof( SocketAddress ) ) != 0 )
    {
        printf( "Failed to bind socket, error %d.\n", WSAGetLastError() );
    }

    printf( "Step 8");
    strcpy(serverAddr, "GET /");
    if (strlen(Path) > 0){
        strcat(serverAddr, Path);
    }
    printf( "Step 9");
    strcat(serverAddr, "");
    strcat(serverAddr, " HTTP/1.0\r\nHOST: ");
    strcat(serverAddr, URL);
    strcat(serverAddr, "\r\n\r\n");

    printf( "Step 10");
    char buffer[10000];
    int nDataLength;
    while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
        int i = 0;
        while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
            cout << buffer[i];
            i += 1;
            printf("While_Loop1_Fail");
        }
        printf("While_Loop2_Fail");
    }
    closesocket(Socket);
    return 0;
    WSACleanup();
}

void ResetArray(char *array_)
{
    char *begin = bufferReturn;
    char *end = begin + sizeof(array_);
    std::fill(begin, end, 0);
}

bool IsRequest()
{
//HERES WHERE IT CRASHES, AS **SOON** as the "Request1 = SocketRequest" line is called.
    printf("is_request...");
    Request1 = SocketRequest("www.mywebsite.com", "test/test.php"); 
    if (strstr(Request1, "ON"))
    {
        returnTEST = true;
        ResetArray(bufferReturn);
        return true;
    }
    else if (strstr(Request1, "OFF"))
    {
        returnTEST = false;
        ResetArray(bufferReturn);
        return true;
    }
    else if (strstr(Request1, "Null"))
    {
        printf(strstr(bufferReturn, "Null\n\n"));
        return false;
    }
}

bool Reset()
{
    ResetRequest = SocketRequest("www.mywebsite.com", "test/test.php");
    printf(ResetRequest);
    return true;
}
//This is where the code is ran from on the Console ON BOOT.
void mainThread(){
    for(;;Sleep(45)){
            if(bInitialized[0] == true || bInitialized[1] == true)
            {
//THIS IS WHERE the function "IsRequest" is called.
                printf("binit is true!");
                if (IsRequest())
                {
                    printf("Worked!\n");
                }
            }
        }
    }

And here's the output when it's ran:

binit is true! and still running...is_request...
    ------------------------------------------------------------------------
      stop code: 0x2b (PANIC_STACK_SWITCH)
        (0x3A097900,0x80072908,0x3A14E110,0x80076D88)
    ------------------------------------------------------------------------
    Call Stack:
        0x80072908 (EADDR)
        0x80076D88 (LR)
        0x91F96F3C
        0x91F86B64
        0x91F75520
        0x91F5E92C
        0x91F5EC68
        0x91F5EECC
        0x91F682E8

stop code: 0x2b (PANIC_STACK_SWITCH) = This error normally appears when a kernel-mode driver uses too much stack space. It can also appear when serious data corruption occurs in the kernel. ref: https://msdn.microsoft.com/en-us/library/windows/hardware/ff557460(v=vs.85).aspx


So pretty much, I don't understand why it's not running my function, I can't understand why it's crashing. If anyone has any ideas, it would be greatly appreciated; as, I understand this is quite an overwhelming bit-topic of information that may only pertain to my situation. [I can assume overloading the stack or invalid conversion of type char*?]
Thanks.

  • You haven't called `connect` – Ben Oct 01 '15 at 09:05
  • @Ben That's what the "if( bind( Socket"... line is doing in the SocketRequest Function. Also, the SocketRequest Function won't even run, like it won't even printf. EDIT* I correct myself, You're right, but still doesn't solve the issue.... – user3097400 Oct 01 '15 at 09:13
  • That's because *everything else is wrong too*... You should start again from a sample code. – Ben Oct 01 '15 at 09:30
  • @Ben Okay, well I have started over and over and over. This is running on an external system and debugging is a tad harder (granted I can do it from my computer), coding will be different. As for everything else being wrong, how do you figure? – user3097400 Oct 01 '15 at 09:34
  • Everything. Is. Wrong. Everything. Bind is wrong, you are binding to port 80 which is wrong because bind is your end not the remote end. Connect is missing. You are using htonl where you shouldn't. You haven't resolved the name to an address. Since you are not on standard C++ I don't have access to your documentation so you will have to check yourself what form the address is supposed to take. – Ben Oct 01 '15 at 10:45
  • Well I appreciate you going into detail for me, and the documentation is in the: https://pastebin.com/CApdcw6y , granted I understand this isn't for you to do... I will figure it out on my own, but thanks. I guess back to square one, thanks anyway. – user3097400 Oct 01 '15 at 11:00
  • That's not documentation that's a header file. Somewhere there will be example code or a howto that explains what to do because gethostbyname is unavailable. – Ben Oct 01 '15 at 11:03
  • You are correct, that is the header (with comments); as the documentation on it is limited to the SDK and not very much is published on 'gethostbyname' or similar. – user3097400 Oct 01 '15 at 12:24
  • 1
    Is there any reason to use sockets to perform HTTP–requests instead of a high–level library (for example, WinHTTP or WinINet)? – Sergey Vyacheslavovich Brunov Oct 01 '15 at 13:32
  • Related question: http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c – Sergey Vyacheslavovich Brunov Oct 01 '15 at 17:56
  • Yes @SergeyBrunov as I can't use a more efficient method to accomplish what I'm trying to do on the console (being the XBOX 360). – user3097400 Oct 09 '15 at 05:06
  • @BaSs_HaXoR, sorry, what do you mean exactly? – Sergey Vyacheslavovich Brunov Oct 13 '15 at 11:54
  • @SergeyBrunov I'm still working on it with whatever time I have, I have gotten help from a couple fellow friends at this point. As I said, I'm using Winsockx; which, is utilized in XBOX Development, so I cannot use "gethostbyname". Also, I can't implement a library on Xbox without then making it become unstable or otherwise incompatible. Not saying it's not possible, but I'd rather just use sockets. – user3097400 Oct 16 '15 at 10:11

1 Answers1

0

You haven't called connect

Of course connect requires a remote address but you only have a name. So you need to convert the name into an address by calling gethostbyname.

Once you have an address, the sequence is:

  • socket to create a socket object.
  • bind to bind the local end of the connection to a local address and port. Bind connects your end of the socket.
  • connect to connect a stream socket to a remote host.

And you don't need to use the hton* family of functions here. They are for converting numbers you have received over the network.

Remarks: It certainly looks like the call to socket and bind should be combined, doesn't it? The reason they aren't is historical - that's just how it works now.

Ben
  • 34,935
  • 6
  • 74
  • 113
  • Thank you for your answer/help... but, I can't use "gethostbyname" as it was deprecated from the "winsockx.h" library I'm using (in the source says:): Error return codes from gethostbyname() and gethostbyaddr() * (when using the resolver). Note that these errors are * retrieved via WSAGetLastError() and must therefore follow * the rules for avoiding clashes with error numbers from * specific implementations or language run-time systems. * For this reason the codes are based at WSABASEERR+1001. * Note also that [WSA]NO_ADDRESS is defined only for * compatibility purposes. – user3097400 Oct 01 '15 at 09:20
  • Here's the winsockx.h source: https://pastebin.com/CApdcw6y Is there another way I can gethostbyname? – user3097400 Oct 01 '15 at 09:21