s3eSocket Marmalade无法创建套接字

时间:2014-10-08 23:42:28

标签: php c++ sockets marmalade

您好我是Marmalade和C ++的新手,但我知道编程。我几乎在网上的任何地方搜索过这个答案,但这可能只是因为我还不太了解C ++。

行。我正在编写一个与PHP服务器一起工作的聊天程序,我正在尝试使用C ++在Marmalade上运行客户端。我还在这个应用程序的开头,当我尝试创建一个s3eSocket时,我收到一个UNSUPPORTED错误。我在这里读到:Can't open socket C++,我可能需要在使用套接字之前做一个启动,但我不知道该怎么做。

这是我的C ++代码:

#include "s3e.h"
#include "s3eSocket.h"
#include <string.h>
#include "s3eOSReadString.h"
#include <string>

static s3eSocket* socket = NULL;
static char g_ErrorString[256];   
static bool g_SocketIsConnected = false;

#define SOCKET_TIMEOUT (30000)

void Connect(const char* host, uint16 port);
void DisplayText(const char* txt);
int32 ConnectCB(s3eSocket* g_Sock, void* sys, void* data);

// Main entry point for the application
int main()
{
    // Wait for a quit request from the host OS
    while (!s3eDeviceCheckQuitRequest())
    {
        //DisplayText("Hello world!");

        Connect("127.0.0.1/server.php", 9000);
    }
    return 0;
}

// Connect callback
int32 ConnectCB(s3eSocket* g_Sock, void* sys, void* data)
{
    s3eResult res = *(s3eResult*)sys;
    if (res == S3E_RESULT_SUCCESS)
        g_SocketIsConnected = true;
    else
        g_SocketIsConnected = false;
    return 0;
}

void Connect(const char* host, uint16 port)
{
    socket = s3eSocketCreate(S3E_SOCKET_TCP, S3E_SOCKET_LOCAL);
    if (socket == NULL)
    {
        DisplayText(s3eSocketGetErrorString());
        return;
    }

    g_SocketIsConnected = false;

    // look up address
    s3eInetAddress addr;
    memset(&addr, 0, sizeof(addr));
    if (s3eInetLookup(host, &addr, NULL, NULL) == S3E_RESULT_ERROR)
    {
        //sprintf(g_ErrorString, "`x666666s3eInetLookup failed: %s", host);
        std::string buf("`x666666s3eInetLookup failed:");
        buf.append(host);
        DisplayText(buf.c_str());
        return;
    }
    addr.m_Port = s3eInetHtons(port);
    int32 counter = 0;
    // Using the created socket, address structure and set callback function the        Connect is called.
    // A wait is performed for a minute while the connect is attempted. When             connection succeeds the
    // callback sets the g_SocketIsConnected bool to true causing the waiting to stop
    bool bNeedToWaitConnection = false;
    if (s3eSocketConnect(socket, &addr, &ConnectCB, NULL) != S3E_RESULT_SUCCESS)
    {
        switch (s3eSocketGetError())
        {
            // These errors are 'OK', because they mean,
            // that a connect is in progress
        case S3E_SOCKET_ERR_INPROGRESS:
            bNeedToWaitConnection = true;
            break;
        case S3E_SOCKET_ERR_ALREADY:
        case S3E_SOCKET_ERR_WOULDBLOCK:
            break;
        default:
            // A 'real' error happened
            std::string str = "`x666666Connecting failed:\n";
            std::string str2 = str.append(s3eSocketGetErrorString());
            DisplayText(str2.c_str());
            s3eSocketClose(socket);
            socket = NULL;
            return;
        }
    }
    else
    {
        bNeedToWaitConnection = true;
    }
    if (bNeedToWaitConnection)
    {
        // Try to connect, but wait a maximum time of SOCKET_TIMEOUT
        uint64 testStartTime = s3eTimerGetMs();
        while (!s3eTimerGetMs() - testStartTime < SOCKET_TIMEOUT)
        {
            // Stop waiting since socket is now connected
            if (g_SocketIsConnected)
                break;
            // Press key 4 in order to cancel connect operation
            s3eKeyboardUpdate();
            if (s3eKeyboardGetState(s3eKey4) & S3E_KEY_STATE_PRESSED)
            {
                s3eSocketClose(socket);
                socket = NULL;
                strcpy(g_ErrorString, "");
                return;
            }
            //Update screen during connect operation
            s3eSurfaceClear(0xff, 0xff, 0xff);
            s3eDebugPrint(10, 120, g_ErrorString, 1);
            sprintf(g_ErrorString, "`x666666 Trying to connect");
            s3eDebugPrint(10, 80, "`x666666Press 4 to cancel connect", 0);
            switch (++counter % 8)
            {
            case 0:
            case 4:
                strcat(g_ErrorString, " |"); break;
            case 1:
            case 5:
                strcat(g_ErrorString, " /"); break;
            case 2:
            case 6:
                strcat(g_ErrorString, " -"); break;
            case 3:
            case 7:
                strcat(g_ErrorString, " \\"); break;
            }
            s3eSurfaceShow();
            s3eDeviceYield(30);
        }
    }
    if (g_SocketIsConnected == false)
    {
        strcpy(g_ErrorString, "`x666666Socket connecting timed out");
        s3eSocketClose(socket);
        socket = NULL;
    }
    else
    {
        strcpy(g_ErrorString, "");
    }
}

void DisplayText(const char* txt)
{
    // Fill background blue
    s3eSurfaceClear(0, 0, 255);

    // Print a line of debug text to the screen at top left (0,0)
    // Starting the text with the ` (backtick) char followed by 'x' and a hex value
    // determines the colour of the text.
    s3eDebugPrint(120, 150, txt, 0);

    // Flip the surface buffer to screen
    s3eSurfaceShow();

    // Sleep for 0ms to allow the OS to process events etc.
    s3eDeviceYield(0);
}

我从Marmalade文档中找到了这段代码并对其进行了一些修改。如果你们中的任何一个人能给我一个进一步寻找或任何帮助的地方,那就太好了。谢谢。

1 个答案:

答案 0 :(得分:0)

致电:

 Connect("127.0.0.1/server.php", 9000);

你最终正在调用这个函数:

 s3eInetLookup(host, &addr, NULL, NULL);

期望以&#34; 127.0.0.1&#34;形式的IP地址;所以&#34; 127.0.0.1/server.php"是无效的。我不确定你的server.php文件中包含了什么...所以我假设在你的php文件中你实际上是在创建套接字并监听端口9000,然后进行这个更改应该解决你的问题...