连接到服务器并接收数据

时间:2014-10-02 22:37:25

标签: c++ xbox360

好的,我正在开发一款连接到我家用电脑上的服务器的软件。我有我的堂兄在硅谷工作看看它,他无法弄清楚它为什么不起作用。我正在使用winsockx.h标头,因为该软件将在Xbox 360控制台上运行,但标头的设计功能与标准的winsock标头相同。

我已经为我的家用电脑转发并设置了端口9001,因此设置不应该是一个问题。我也被告知我的服务器应该正常运行,但我应该在一个单独的线程上有主侦听器,这样可以同时处理多个客户端。

仅供参考:当我调用DbgPrint时,它会将数据发送到名为xbWatson的调试监视器,它还会检测错误并在屏幕上显示错误。

以下是关注该问题的两个主要屏幕截图.. 调试监视器:http://i.gyazo.com/b4c5e90b427113992fa02cdb3c73e7e2.png& 代码中断的位置:http://i.gyazo.com/5942967dd8456f4f194107bfce1d2b4c.png

VOID Connect()
{
    DbgPrint("-- Creating XNetStartupParams variable 'pXNSP'... ");
    XNetStartupParams pXNSP;
    DbgPrint("Done!\n");
    DbgPrint("-- Allocating variable 'pXNSP'... ");
    memset(&pXNSP, 0, sizeof(XNetStartupParams));
    DbgPrint("Done!\n");
    DbgPrint("-- Configuring Size of our 'pXNSP' structure... ");
    pXNSP.cfgSizeOfStruct = sizeof(XNetStartupParams);
    DbgPrint("Done!\n");
    DbgPrint("-- Configuring Flags of our 'pXNSP' variable... ");
    pXNSP.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
    DbgPrint("Done!\n");
    DbgPrint("-- Starting XNet using our 'pXNSP' variable... ");
    XNetStartup(&pXNSP);
    DbgPrint("Done!\n");

    DbgPrint("-- Creating WSADATA variable 'lpWSADATA'... ");
    WSADATA lpWSADATA;
    DbgPrint("Done!\n");
    DbgPrint("-- Starting WSA using our 'lpWSADATA' variable... ");
    WSAStartup(MAKEWORD(2, 2), &lpWSADATA);
    DbgPrint("Done!\n");

    DbgPrint("-- Creating SOCKADDR_IN variable 'SocketAddress'... ");
    SOCKADDR_IN SocketAddress;
    DbgPrint("Done!\n");

    DbgPrint("-- Creating a socket and returning it to our 'ConnectSocket' variable... ");
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    DbgPrint("Done!\n");
    DbgPrint("-- Enabling our broadcast... ");
    BOOL bBroadcast = TRUE;
    DbgPrint("Done!\n");
    DbgPrint("-- Optimizing our SOCKET variable 'ConnectSocket'... ");
    setsockopt(ConnectSocket, SOL_SOCKET, 0x5801, (PCHAR)&bBroadcast, 4);
    DbgPrint("Done!\n");

    DbgPrint("-- Configuring port of our 'SocketAddress' variable... ");
    SocketAddress.sin_port = htons(9001);
    DbgPrint("Done!\n");
    DbgPrint("-- Configuring ipv4 address of our 'SocketAddress' variable... ");
    SocketAddress.sin_addr.S_un.S_un_b.s_b1 = Server[0];
    SocketAddress.sin_addr.S_un.S_un_b.s_b2 = Server[1];
    SocketAddress.sin_addr.S_un.S_un_b.s_b3 = Server[2];
    SocketAddress.sin_addr.S_un.S_un_b.s_b4 = Server[3];
    DbgPrint("Done!\n");
    DbgPrint("-- Configuring family of our 'SocketAddress' variable... ");
    SocketAddress.sin_family = AF_INET;
    DbgPrint("Done!\n");

    DbgPrint("-- Creating integer variable 'SocketAddressSize'... ");
    INT SocketAddressSize = sizeof(SocketAddress);
    DbgPrint("Done!\n");

    for(;;)
    {
        DbgPrint("-- Checking for socket errors in our connection... ");
        if(connect(ConnectSocket, (PSOCKADDR)&SocketAddress, SocketAddressSize) == SOCKET_ERROR)
        {
            DbgPrint("Done (w/ errors)!\n");
            Error = FALSE;
            Authed = FALSE;
            return;
        }
        else
        {
            DbgPrint("Done (w/o errors)!\n");
            return;
        }
    }
}

VOID Disconnect()
{
    closesocket(ConnectSocket);
}

VOID Auth()
{
    DbgPrint("[Skylight] Authorization Started:\n");
    DbgPrint("- Connecting to server:\n");
    Connect();
    DbgPrint("- Configuring our 'ServerRequest' variable... ");
    ServerRequest.Version = 1;
    ServerRequest.Key = 9001;
    ServerRequest.Req = (REQUEST)1;
    DbgPrint("Done!\n");
    DbgPrint("- Creating byte[0x10] variable 'CPUKey'... ");
    BYTE CPUKey[0x10];
    DbgPrint("Done!\n");
    DbgPrint("- Getting/Storing CPU Key from system... ");
    UINT64 addr = 0x8000000000000000ULL | ((DWORD)MmGetPhysicalAddress(CPUKey) & 0xFFFFFFFF);
    HvxGetVersions( 0x72627472, 5, 0x20, addr, 0x10 );
    DbgPrint("Done!\n");
    DbgPrint("- Sending our 'CPUKey' variable to the server... ");
    send(ConnectSocket, (PCHAR)&CPUKey, 0x10, NULL);
    DbgPrint("Done!\n");
    DbgPrint("- Freeing our 'CPUKey' variable... ");
    free(CPUKey);
    DbgPrint("Done!\n");
    DbgPrint("- Waiting for authorization response from the server... ");
    recv(ConnectSocket, (PCHAR)&Authed, 4, NULL);
    DbgPrint("Done!\n");
    DbgPrint("- Disconnecting from server... ");
    Disconnect();
    DbgPrint("Done!\n"); DbgPrint("\n");
}

0 个答案:

没有答案