non-blocking sockets

This is a discussion on non-blocking sockets within the Networking/Device Communication forums, part of the General Programming Boards category; So I found a select server off of a website and modified it. I am using the server with non-blocking ...

non-blocking sockets

So I found a select server off of a website and modified it. I am using the server with non-blocking clients.

If I connect 2 clients to the select server the clients work in blocking mode just fine. When I set the clients to non-blocking they echo to them selves. Whether connected to the server or not. As long as 2 or more clients are connected. For example if I connect cli1 & cli2 (no server), type hey in cli1 and it will send hey to itself.

Sorry anduril, I wrote that in a rush. Its just getting nice in Jersey. Super nice actually! Ive been planning a camping trip to the Appalachian mountains. And my gas gauge is broke so I've spent the day replacing my fuel sending unit on my 94 Ranger. I tried writing that post on the way to mechanic shop to pick up the part.

I'm getting gas now and figured I'd respond real quick. Ill re-write everything in a more elaborate manner along with the code when I get home tonight. This is from my android phone. Thanks and sorry for the cracked out post!

I have a select server in working order. I got the select server from a website and got it working with minimal tweaking. I wrote the clients myself. Now if I connect my 2 blocking clients to the select server, they communicate synchronously just fine. The problem occurs when i set non-blocking mode.

NOW FOR THE WEIRD ISSUE.
When i enable non-blocking mode, the clients connect to the server just fine. "EXCEPT" anything i type is just echoed back to the client that sent the string. For example, if I type hello on client 1, client 1 will immediately recieve a hello string from itself.

I am using strace and tcpdump to monitor all processes.

Another issue is that even if do not have the server running, the clients will work and echo to them selves. As long as 2 or more clients are connected. So no need for the server at all. The problem is non-existant as long as the clients are not set to non-blocking.

Yes, the full code would be helpful, though there are some issues with what you posted. You need to re-read the man page for the read function. It will not return EAGAIN, and you need to check EWOULDBLOCK too. Also, readfd is not being used the way I would expect. Either it's a file descriptor for something else, and you're overwriting it with a bogus value (the return value of read), which could cause your problem, or it's just really badly named, and should be called something sensible like bytes_read.

Your use of read() is fine. It's the variable readfd that is confusing. The fd implies it's a file descriptor. If it's actually a file descriptor, then readfd = read() will overwrite the file descriptor with some other value, so when you try to use it as a descriptor, it will likely fail. If it's not a file descriptor, it's simply very badly named. I have to leave work now, so not sure when I'll get back to this. When you read the man page for read(), pay careful attention to the return value, and the section about what to check for EAGAIN/EWOULDBLOCK.

Oh ok, good. Yeah I named the variable like that of an FD, but it is not. It does confuse a lot of people though. I am self taught and that is how I learned. Just a bad habit and sometimes bad habits are hard to break ya know.

Also, readfd is not being used the way I would expect. Either it's a file descriptor for something else, and you're overwriting it with a bogus value (the return value of read), which could cause your problem, or it's just really badly named, and should be called something sensible like bytes_read.

I think you (Annonymous) got the point here but I'm gonna reiterate because I'm sure I've made this (exact same) observation before (as in, about your use of the identifier "readfd" is networking code). It also turns out to be central to the problem. The thing is, you can write your code how the hell you like, if you don't care whether or not anyone else ever looks at it, or what they'll think if they do. Otherwise, try to take criticism about style and readability to heart, even if you don't think these things affect function. One basic point about style is: make your identifiers meaningful. And don't reuse identifiers in ways inappropriate to their name. Eg, something I used to do was, if I'd declared an int somewhere for whatever purposes, and it remained in scope at some later point where I needed another int temporarily, and the original purpose of that first int was complete, I'd just re-use it. So I might end up with an int readfd that really was a file descriptor at some point and then use it temporarily in a loop to get (eg) the return value of read. Or something.

Eventually I'd come back and look and my own code and recognize this and think, that's kind of stupid, so I'd change the name of the variable to something like "tmp" or "x". That would be like, my general purpose int for the block. Better than declaring 3 ints if you don't have to, right? No, it isn't. That "savings" is totally irrelevant. Eventually I realized it's much better to have three ints with three specific names than one with a name that could be anything. Much easier to read.

NOW FOR THE WEIRD ISSUE.
When i enable non-blocking mode, the clients connect to the server just fine. "EXCEPT" anything i type is just echoed back to the client that sent the string. For example, if I type hello on client 1, client 1 will immediately recieve a hello string from itself.

Let's look at somt code that could be responsible for this from the server:

No offence, Annon, but that is pathetic. It looks like we are moving into a nest THAT DOESN'T EXIST. This is at least as misleading as misnaming variables. Are you trying to confuse people, lol? And I don't care if it was because of the forum widget or whatever. You should check the post and if the indenting is wrong do something to fix it. I cut n' paste here all the time, I don't have any problems.

About naming loops in comments: that's fine, but again, give them a meaningful name, not a number. That number implies nesting THAT DOESN'T EXIST. AGAIN.

All your code is like this -- very haphazardly indented.Stop it. You know how to indent, do it. The fact that you don't implies you are rushing and figuring you can tidy up later. That is a "shoot myself in the foot" repeatedly approach. We're going to see the consequence in a moment.

So, actually that section of code is not responsible. I'll give you a clue about what is. It's in the client code:

If your code weren't such a mess, this would have been obvious in < ten minutes.

So, several days later, do you think you saved much time being lazy on style? Anduril462 actually pointed this out in post #6, but that got lost in the confusion about what exactly readfd referred to.

In theory, I guess that condition could be true, except:

A) Guess what? Readfd hasn't necessarily even been initialized at that point. Which is a good reason to always initialize variables when they are declared.

B) I guess EAGAIN could be less than 0 depending on the implementation. It almost certainly isn't, but it could be. However, if EAGAIN is implementation dependent, then you shouldn't assume it is less than 0. This goes for ALL such defines and error codes.

That condition might be sort of fulfilled with:

Code:

if((readfd < 0) && (errno == EAGAIN))

Except, of course, readfd is still, potentially, at that point, uninitialized, and if not, what does it refer to? And the only reason I put errno there is because that's probably what you'd want to check -- if there is anything this check is supposed to apply to at that point in the code. I don't see anything.

So you get the "else". Notice before you get the echo back you get another message:

FCNTL(0) ---> Success.
Made a connection to 127.0.0.1
Message: whatWaiting for 127.0.0.1
-->what<--

If you wade through that haphazard mess of braces, you'll see what's going on.

Please, please, please don't just go and shim in some correction here. First, format everything correctly, and as you write, keep it so. What tool do you use to code, BTW?

If you post code with wacko freestyle indentation here again, don't expect to get any help from me Programming is not just about figuring out how to do something. It's also about keeping your tish together in the process. If you can't you're going to end up up a creek (again ).

MK27 is right. We aren't picky about what indentation/formatting style you choose, but it ought to be consistent. I usually recommend one of the first 3 styles mentioned here: Indent style - Wikipedia, the free encyclopedia, since they're common and easily read by pretty much anybody.

Egad! Look at all that mess. Neither one of those builds without errors, let alone without warnings. Fix all of those. Your code should compile without any errors or warnings, even on the highest settings. Use the -Wall option with gcc. You should get no output if your code is clean.

Egad! Look at all that mess. Neither one of those builds without errors, let alone without warnings. Fix all of those. Your code should compile without any errors or warnings, even on the highest settings. Use the -Wall option with gcc.

Umm, in Annon's defence, if you use -Wall but get rid of the unnecessary -std=c99, all you get is:

@anduril462, i usually compile with -g and -Wall and it displays no errors. I have never seen someone compile the way you did in the previous post you made.

I have read a lot of documentation on EWOULDBLOCK & EAGAIN and a lot of people have trouble with non-blocking sockets. There was no solution or very little help regarding my situation/issue. Although the documentation on EWOULDBLOCK was very small, there is a decent amount of information/help pertaining to EAGAIN. I just have not been reading much on it. Kind of busy.

@MK, I use gedit and the command line to write code. Sometimes nano, but never vim. I could never get used to it and have become comfortable with the ease of nano.

MK, i have the utmost respect for you and your opinion! The last thing i want to do make you mad and turn away your help. What i am going to do is re-read on the documentation that pertains to my data handling. Fix the indentation, and apply the correction you have supplied for me.

See the problem with the select function is there really isn't that great of documentation available. If there is, it is written very sloppy, very poorly explained with the assumption of the readers understanding, If there is well documented tutorials/files on the function they are much to advanced!