digitalmars.D.learn - Weird result of getsockopt
- CodeSun (42/42) May 24 2015 Hello guys,
- tcak (4/46) May 24 2015 Could you check the return value of "setsockopt"? Make sure it
- Daniel Kozak (12/54) May 24 2015 Cause your code is wrong:
- Daniel Kozak (2/65) May 24 2015 *call it again with right size and than it will probably work
- Daniel Kozak (3/71) May 24 2015 I mean call it with something big enought, size than should be
- CodeSun (2/65) May 24 2015 You are right, thx.
Hello guys,
Today, I found a weird problem when I was learning to enable
SO_KEEPALIVE for a specific socket. I use setsockopt to enable
keepalive firstly, and then use getsockopt to show if it is
enabled correctly.
My code snippet is listed below:
Dlang version:
import core.sys.posix.sys.socket;
import core.sys.posix.netinet.in_;
import std.c.stdio;
void main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t)flag.sizeof);
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t*)&size);
printf("%d\n", flag);
}
C version:
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
int main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t)sizeof(flag));
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t*)&size);
printf("%d\n", flag);
return 0;
}
Dlang version always prints 0, which means keepalive is not
enabled, while C version can almost display 1 all the time as
expected.
So is there anything wrong inside the code? If not, whose
behavior is correct?
May 24 2015
On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote:
Hello guys,
Today, I found a weird problem when I was learning to enable
SO_KEEPALIVE for a specific socket. I use setsockopt to enable
keepalive firstly, and then use getsockopt to show if it is
enabled correctly.
My code snippet is listed below:
Dlang version:
import core.sys.posix.sys.socket;
import core.sys.posix.netinet.in_;
import std.c.stdio;
void main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t)flag.sizeof);
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t*)&size);
printf("%d\n", flag);
}
C version:
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
int main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t)sizeof(flag));
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t*)&size);
printf("%d\n", flag);
return 0;
}
Dlang version always prints 0, which means keepalive is not
enabled, while C version can almost display 1 all the time as
expected.
So is there anything wrong inside the code? If not, whose
behavior is correct?
Could you check the return value of "setsockopt"? Make sure it
returns 0 as an indicator of successful operation. If so, then we
can think about it further.
May 24 2015
On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote:
Hello guys,
Today, I found a weird problem when I was learning to enable
SO_KEEPALIVE for a specific socket. I use setsockopt to enable
keepalive firstly, and then use getsockopt to show if it is
enabled correctly.
My code snippet is listed below:
Dlang version:
import core.sys.posix.sys.socket;
import core.sys.posix.netinet.in_;
import std.c.stdio;
void main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t)flag.sizeof);
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
cast(socklen_t*)&size);
printf("%d\n", flag);
}
C version:
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
int main() {
int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int flag = 1;
size_t size;
setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t)sizeof(flag));
flag = 0;
getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag,
(socklen_t*)&size);
printf("%d\n", flag);
return 0;
}
Dlang version always prints 0, which means keepalive is not
enabled, while C version can almost display 1 all the time as
expected.
So is there anything wrong inside the code? If not, whose
behavior is correct?
Cause your code is wrong:
"If the size of the option value is greater than option_len, the
value stored in the object pointed to by the option_value
argument shall be silently truncated. Otherwise, the
object pointed to by the option_len argument shall be modified to
indicate the actual
length of the value."
So because you have size set to 0 it will not work, you mast call
it again and than it will probably work.
In C this work because size is not initialize which mean it could
be anything
May 24 2015
On Sunday, 24 May 2015 at 21:11:34 UTC, Daniel Kozak wrote:On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote:*call it again with right size and than it will probably workHello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t*)&size); printf("%d\n", flag); } C version: #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t*)&size); printf("%d\n", flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct?Cause your code is wrong: "If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value." So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything
May 24 2015
On Sunday, 24 May 2015 at 21:13:02 UTC, Daniel Kozak wrote:On Sunday, 24 May 2015 at 21:11:34 UTC, Daniel Kozak wrote:I mean call it with something big enought, size than should be modified to real sizeOn Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote:*call it again with right size and than it will probably workHello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t*)&size); printf("%d\n", flag); } C version: #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t*)&size); printf("%d\n", flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct?Cause your code is wrong: "If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value." So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything
May 24 2015
On Sunday, 24 May 2015 at 21:11:34 UTC, Daniel Kozak wrote:On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote:You are right, thx.Hello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, cast(socklen_t*)&size); printf("%d\n", flag); } C version: #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &flag, (socklen_t*)&size); printf("%d\n", flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct?Cause your code is wrong: "If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value." So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything
May 24 2015









"tcak" <tcak gmail.com> 