s3eSocket 果酱无法创建套接字
Posted
技术标签:
【中文标题】s3eSocket 果酱无法创建套接字【英文标题】:s3eSocket Marmalade cannot create socket 【发布时间】:2014-10-08 23:42:27 【问题描述】:您好,我是 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】:在您的电话中:
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 上侦听,然后进行此更改应该可以解决您的问题。 ..
【讨论】:
以上是关于s3eSocket 果酱无法创建套接字的主要内容,如果未能解决你的问题,请参考以下文章
在 Erlang 中通过 RPC 在远程节点上创建套接字时,无法接受套接字上的连接
无法创建服务器 TCP 监听套接字 *:6383 绑定:无法在 docker 上的 redis 集群中分配请求的地址(在 Windows 中)
无法从 Data Fusion 连接 Cloud SQL mySql 实例。异常“无法创建套接字工厂 'com.google.cloud.sql.mysql.SocketFactory”
Qt Creator中的OpenCV:无法连接创建者通信套接字/tmp/qt_temp.T32147/stub-socket:没有这样的文件或目录