cppcoro icon indicating copy to clipboard operation
cppcoro copied to clipboard

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

Open L-Sun opened this issue 3 years ago • 1 comments

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

is the correct code following?

    ipv4Sockaddr.sin_port = htons(localEndPoint.to_ipv4().port()); 
                            ^^^^^

L-Sun avatar Jun 12 '21 12:06 L-Sun

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(..).

jdoubleu avatar Jul 27 '23 08:07 jdoubleu