(GET HTTP Request)使用winsock.h的C ++套接字

时间:2015-10-01 08:56:37

标签: c++ c sockets winsock xbox360

好吧,在任何人得出结论之前(并且确实与我保持相关的事实),基本上,我试图做一个     使用Winsock.h 使用C / C ++中的套接字获取请求 [运行SocketRequest函数时存在的主要问题]
我从以下地方获得了错误响应代码: " WSAENOTCONN 10057"到" WSAENOTSOCK 10038" 其中,我在此处找到了更多相关信息:https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx

现在,这对其他人来说有点无关紧要,但更多的是我的情况。 我试图在XBOX360 Devkit上实现这个,使用" winsockx.h"当我发出一个" SocketRequest" ...这里有代码 WITH 输出以进行调试时崩溃了。 (我知道这通常不是你所看到的,所以按照你想要的方式)

此处包含的文件:

#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>

以下是定义和实例:

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)

这里是代码(包括无法运行的SocketRequest函数):

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");
                }
            }
        }
    }

这是输出时的输出:

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

停止代码:0x2b(PANIC_STACK_SWITCH)=当内核模式驱动程序使用过多堆栈空间时,通常会出现此错误。当内核中发生严重数据损坏时,它也会出现。 参考:https://msdn.microsoft.com/en-us/library/windows/hardware/ff557460(v=vs.85).aspx

所以,我不明白为什么它没有运行我的功能,我无法理解为什么它会崩溃。如果有人有任何想法,我将不胜感激;因为,我知道这是一个压倒性的信息主题,可能只与我的情况有关。 [我可以假设重载堆栈或char *类型的无效转换] 感谢。

1 个答案:

答案 0 :(得分:0)

您尚未拨打connect

当然connect需要一个远程地址,但您只有一个名字。因此,您需要通过调用gethostbyname将名称转换为地址。

获得地址后,顺序为:

  • socket创建套接字对象。
  • bind将连接的本地端绑定到本地地址和端口。 Bind连接你的套接字
  • connect将流套接字连接到远程主机。

此处您无需使用hton*系列函数。它们用于转换您通过网络收到的号码。

备注:看起来应该合并socketbind的调用,不是吗?他们不是历史的原因 - 这就是它现在的运作方式。