babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

[Issue] ulfius_init_instance API with sockaddr_in parameter does not work #185

Closed batsayan closed 3 years ago

batsayan commented 3 years ago

Describe the issue The following code block works if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) { printf("Error ulfius_init_instance, abort\n"); return(1); }

The following does not work struct sockaddr_in listen; listen.sin_family = AF_INET; listen.sin_addr.s_addr = htonl (INADDR_ANY); //listen.sin_addr.s_addr = inet_addr("10.10.140.148");

if (ulfius_init_instance(&instance, PORT, &listen, NULL) != U_OK) { printf("Error ulfius_init_instance, abort\n"); return(1); }

To Reproduce Use any sample application with or without the sockaddr_in parameters

Expected behavior sockaddr_in specified ulfius_init_instance() API should work

Screenshots Got the message "Failed to connect to localhost port 8080: Connection refused" when I use

(ulfius_init_instance(&instance, PORT, &listen, NULL)

System (please complete the following information):

babelouest commented 3 years ago

Hello @batsayan ,

When you use a struct sockaddr_in with ulfius_init_instance, you must also set the struct sockaddr_in.port value, because the port number specified in ulfius_init_instance is no longer used.

listen.sin_port = htons(PORT);

Can you retry with that?

batsayan commented 3 years ago

@babelouest This works, thanks. One observation, PORT parameter goes twice inside ulfius_init_instance API.

babelouest commented 3 years ago

PORT parameter goes twice inside ulfius_init_instance API

Yes it does, it's a known limitation. I'll update the documentation to make it more obvious but to avoid breaking existing code, I'm not at ease to make aworkaround just for that.