s3eSocket Marmalade无法创建套接字
s3eSocket Marmalade cannot create socket
嗨,我是Marmalade和C++的新手,但我懂编程。我在网上几乎到处找答案,但可能只是因为我还不太懂C++。
好的。我正在写一个可以在PHP服务器上运行的聊天程序,我正在尝试用C++在Marmalade上做客户端。我还处于这个应用程序的启动阶段,当我尝试创建s3eSocket时,我收到了一个不支持的错误。我在这里读过:Can';t打开套接字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文档中找到了这个代码,并对它进行了一些修改。如果你们中的任何一个人能给我一个进一步寻找的地方或任何帮助,那就太好了。非常感谢。
在您对的呼叫中
Connect("127.0.0.1/server.php", 9000);
您最终调用的是函数:
s3eInetLookup(host, &addr, NULL, NULL);
它需要一个"127.0.0.1"形式的ip地址,因此"127.0.0.1/server.php"无效。我不确定你的server.php文件中包含什么。。。因此,我假设在php文件中,您实际上正在创建一个套接字并侦听端口9000,然后进行此更改应该可以解决您的问题。。。
相关文章:
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- C++套接字客户端到 Python 服务器未创建连接
- 在C ++ ASIO中,如何为HTTP或HTTPS创建相同的套接字对象(接口/抽象问题?
- 如何在C++中创建特定大小的消息以通过UDP套接字发送?
- 在线程C++中创建一个套接字
- 从 PHP 调用已编译的C++可执行文件来创建套接字
- C++ 套接字编程:创建多个流
- 在新计算机上,ZMQ上下文套接字创建要慢得多
- C 创建多个套接字客户端
- 根据C++代码在 PHP 中创建字节数据并将其传递给套接字
- 正在创建多个TCP套接字连接
- 连接到Boost.Asio创建的域套接字时权限被拒绝
- 我缺少什么来创建套接字
- 当服务器重新启动并且客户端收到WSAECONNRESET错误代码时,我应该重新创建整个套接字吗
- 在具有 DNS 名称时创建套接字
- 如何在同一程序中创建多个套接字
- 在Qt中创建套接字
- s3eSocket Marmalade无法创建套接字
- Linux 系统不允许创建套接字,因为"Too many open files"*即使在重新启动后*
- 运行io_service后创建套接字