ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Once I send a single UDP msg from my client to this server, it receives the msg, but then acts as if it's stuck in a loop and FD_ISSET always returns true thereafter. And when the recvmsg() below is called, it always has the same msg. It acts like there's always something on the incoming socket to read.
What am I doing wrong?
Code:
struct timeval timeout;
fd_set read_flags;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
fd_flags = fcntl(sfd, F_GETFL);
err = fcntl(sfd, F_SETFL, fd_flags |= O_NONBLOCK);
if (err < 0) { ... }
while (running) {
FD_ZERO(&read_flags);
FD_SET(sfd, &read_flags);
if (FD_ISSET(sfd, &read_flags)) {
// this returns true, always falls here.
}
ready = select(sfd + 1, &read_flags, NULL, NULL, &timeout);
if (ready < 0) {
fprintf(...);
continue;
}
else if (ready == 0)
continue;
if (FD_ISSET(sfd, &read_flags)) {
// always returns true, always falls here.
my_read();
}
}
int MyTest::my_read()
{
char msgbuf[512];
struct sockaddr_storage src_addr;
memset(msgbuf, 0x0, MSG_SZ);
memset(iov, 0x0, sizeof(iovec));
iov[0].iov_base = msgbuf;
iov[0].iov_len = sizeof(msgbuf);
memset(&msg, 0x0, sizeof(msghdr));
msg.msg_name = &src_addr;
msg.msg_namelen = sizeof(src_addr);
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = 0;
msg.msg_controllen = 0;
received = recvmsg(sfd, &msg, fd_flags);
if (received < 0) {
goto out;
}
else if (msg.msg_flags & MSG_TRUNC) {
goto out;
}
// print msgs
// same msg always appears, even though cli sent it only
// once and shuts down after it sends.
return received;
}
Also, and this may be the main issue... you need to reset the 'timeout' variable each iteration through the while-loop. It may be decremented after the call to select().
if (FD_ISSET(sfd, &read_flags)) {
// this returns true, always falls here.
}
What does this comment mean? You realize you're testing to see if sfd is set right after you explicitly set it, right?
Quote:
Originally Posted by rrlangly
Code:
received = recvmsg(sfd, &msg, fd_flags);
Why are you using fcntl flags where the recvmsg flags go? There's a chance that one of the bits in fd_flags coincides with MSG_PEEK. On my system O_RDWR==MSG_PEEK, and since it's a socket I'm sure this would cause your problem if the same equality holds on your system.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.