gpt4 book ai didi

c++ - Emscripten -> websockify -> raw tcp 意外挂断

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:58 25 4
gpt4 key购买 nike

我在使用 emscripten 从已编译的 Web 应用程序连接到原始 TCP 服务器时遇到问题。当我从应用程序的桌面版本连接时,一切都很好。

在我的 VPS 上,我通过以下方式下载、编译和运行 websockify:

./websockify 0.0.0.0:1235 127.0.0.1:1234

接下来我编译并运行我的服务器,代码:http://pastebin.com/KiehDrvk (来自BeeJ网络)

我的客户端代码非常简单(仅用于测试目的),一些代码:

    TCPsocket sock;
struct sockaddr_in sock_addr;

/* Allocate a TCP socket structure */
sock = (TCPsocket)malloc(sizeof(*sock));
if ( sock == NULL ) {
SDLNet_SetError("Out of memory");
goto error_return;
}

/* Open the socket */
sock->channel = socket(AF_INET, SOCK_STREAM, 0);
if ( sock->channel == INVALID_SOCKET ) {
SDLNet_SetError("Couldn't create socket");
goto error_return;
}
/* Connect to remote, or bind locally, as appropriate */
if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) {

// ######### Connecting to remote

memset(&sock_addr, 0, sizeof(sock_addr));
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = ip->host;
sock_addr.sin_port = ip->port;

/* Connect to the remote host */
if ( connect(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR && errno != EINPROGRESS ) {
SDLNet_SetError("Couldn't connect to remote host");
goto error_return;
}
while (1);
}

所以当我在桌面上运行它时,客户端连接到服务器并按预期等待。

服务器终端返回:

selectserver: new connection from 91.211.105.49 on socket 5

Websockify 终端返回:

None because is from desktop

但是当我尝试从网络版本连接时,客户端连接并突然断开连接:

服务器终端返回:

selectserver: new connection from 127.0.0.1 on socket 6
selectserver: socket 6 hung up
hung up: Success

Websockify 终端返回:

  1: got client connection from 91.211.105.49
1: forking handler process
1: using plain (not SSL) socket
1: using protocol HyBi/IETF 6455 13
1: connecting to: 127.0.0.1:1234
1: client closed connection
1: handler exit

有人知道吗?

最佳答案

您必须允许您的代码返回到网络浏览器运行时。您有一个 while(1) 永恒循环,我怀疑您的浏览器由于无响应而终止了您的应用程序?

如果您正在使用 Emscriptem 建议的主循环设计 (https://kripken.github.io/emscripten-site/docs/porting/emscripten-runtime-environment.html#implementing-an-asynchronous-main-loop-in-c-c),您只需将变量移动到全局范围并从代码中删除 while(1) 循环。

// (1) put all your variables here (global scope)
int main() {

// (2) put your connect code here (without the while(1) loop)

#ifdef __EMSCRIPTEN__
// void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop);
emscripten_set_main_loop(one_iter, 60, 1);
#else
while (1) {
one_iter();
// Delay to keep frame rate constant (using SDL)
SDL_Delay(time_to_next_frame());
}
#endif
}

// The "main loop" function.
void one_iter() {
// process input
// render to screen
}

这至少可以让您在浏览器中测试您的代码。祝你好运!

关于c++ - Emscripten -> websockify -> raw tcp 意外挂断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38383200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com