lewissbaker / cppcoro

A library of C++ coroutine abstractions for the coroutines TS
MIT License
3.41k stars 468 forks source link

Bug Report: the port of socket is incorrect when I use socket.bind(...) #203

Open L-Sun opened 3 years ago

L-Sun commented 3 years ago

https://github.com/lewissbaker/cppcoro/blob/a87e97fe5b6091ca9f6de4637736b8e0d8b109cf/lib/socket.cpp#L291

is the correct code following?

    ipv4Sockaddr.sin_port = htons(localEndPoint.to_ipv4().port()); 
                            ^^^^^
jdoubleu commented 1 year ago

Had the exact same issue.

My current workaround is to manually create an endpoint with a fixed port:

#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif

// ...

auto endpoint = cppcoro::net::ipv4_endpoint::from_string("127.0.0.1:7001");
endpoint = cppcoro::net::ipv4_endpoint{endpoint.address(), htons(endpoint.port())};

The actual bound port is accessible from socket.local_endpoint() after the call to socket.bind(..).