emscripten-core / emscripten-core/emscripten

Emscripten C struck at function socket()

Open
#17,972 30 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

In the below code, program execution is getting struck after calling the socket() function.

Could anyone find the cause and solution for this problem?

**(To reproduce the issue) Here I have executed the below code without emscripten with GCC, and it is working without any issue but not working after compiling to emscripten web assembly**

```
#include
#include
#include
#include
#include

#define VNC_BASE 8080
#define DEFAULT_DISPLAY ":10"

int accept_connection (int port)
{
int bound;
int sock;
struct sockaddr_in sin;
int on = 1;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (/*VNC_BASE + port*/8080);

printf("In\n"); // <-- Getting printed

bound = socket (AF_INET, SOCK_STREAM, IPPROTO_IP); // <-- Getting struck

printf("Out : %d\n", bound); // <-- Not Getting printed

if (bound < 0)
{
perror ("socket");
exit (1);
}

setsockopt (bound, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));

if (bind (bound, (struct sockaddr *) &sin,
sizeof (struct sockaddr_in))) {
perror ("bind");
exit (1);
}

int lStatus = listen (bound, 1);

printf("Listen status : %d\n", lStatus);

sock = accept (bound, NULL, 0);

printf("Accept status : %d\n", sock);

close (bound);
return sock;
}

int main()
{
int clientw = accept_connection (10);
printf("Client w : %d\n", clientw);
}
```

Use the below command to compile the code to webassembly

`emcc ./test.c -s WASM=1 -o ./test.js -lwebsocket.js -s PROXY_POSIX_SOCKETS=1 -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1
`

I have made this repo to reproduce the issue :
[https://github.com/AithaTarun/Webassembly/tree/main](https://github.com/AithaTarun/Webassembly/tree/main)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.