Use UDP sendto() on win32 only

This commit is contained in:
Sergey Lyubka 2021-10-22 09:14:18 +01:00
parent 7c5e67b272
commit 54590aaf26
2 changed files with 12 additions and 2 deletions

View File

@ -2947,11 +2947,16 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, bool is_client,
static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
long n;
#if defined(_WIN32)
// See #1338, #1382. On Windows, UDP send() can fail despite connected.
// Use sendto() instead. But not UNIX: e.g. on Mac we'll get EISCONN
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
n = sendto(FD(c), (char *) buf, len, 0, &usa.sa, slen);
} else {
} else
#endif
{
n = send(FD(c), (char *) buf, len, MSG_NONBLOCKING);
}
return n == 0 ? -1 : n < 0 && mg_sock_would_block() ? 0 : n;

View File

@ -102,11 +102,16 @@ static struct mg_connection *alloc_conn(struct mg_mgr *mgr, bool is_client,
static long mg_sock_send(struct mg_connection *c, const void *buf, size_t len) {
long n;
#if defined(_WIN32)
// See #1338, #1382. On Windows, UDP send() can fail despite connected.
// Use sendto() instead. But not UNIX: e.g. on Mac we'll get EISCONN
if (c->is_udp) {
union usa usa;
socklen_t slen = tousa(&c->peer, &usa);
n = sendto(FD(c), (char *) buf, len, 0, &usa.sa, slen);
} else {
} else
#endif
{
n = send(FD(c), (char *) buf, len, MSG_NONBLOCKING);
}
return n == 0 ? -1 : n < 0 && mg_sock_would_block() ? 0 : n;