0

Hi I am new to Marmalade and C++, but I know programming. I have searched almost everywhere on the web for an answer to this, but it might just be because I don't quite know C++ yet.

OK. I am writing a chat program which works with a PHP server and I am trying to do the client on Marmalade with C++. I am still at the start of this app, and when I try to create a s3eSocket, I get a UNSUPPORTED error. I've read here: Can't open socket C++, that I maybe need to do a startup before I can use sockets, but I am not sure how to do this.

Here is my C++ code:

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

I found this code from the Marmalade docs and modified it a bit. If any of you guys can give me a place to look further or any help, that would be great. Thank you.

Community
  • 1
  • 1
instanceof
  • 1,404
  • 23
  • 30

1 Answers1

0

In your call to:

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

You ultimately are calling the function:

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

which is expecting an ip address in the form of "127.0.0.1" so "127.0.0.1/server.php" is invalid. I'm not sure what is contained in your server.php file... so I'm going to assume that in your php file you are actually creating a socket and listening on port 9000, then making this change should solve your problem...

rkhtech
  • 1
  • 2