Thread Design

Started by
27 comments, last by hplus0603 18 years, 2 months ago
I have a question which might be quick answered so i didn't started a new topic.

I'm looking for a tutorial or a code expample how to use select() and connect(). I heard that i have to use nonblocking sockets but i don't know anything about that.

mfg.
Advertisement
search google for "Beej's networking tutorial". There is a table of contents on his site that you can look through and use as a reference whenever you want to see how a certain function works. I used his select() guide to help me. Took me about 2 hours to get comfortable with them, playing around and then created my own async server.
Hello?
Quote:Original post by cherryhouse
search google for "Beej's networking tutorial". There is a table of contents on his site that you can look through and use as a reference whenever you want to see how a certain function works. I used his select() guide to help me. Took me about 2 hours to get comfortable with them, playing around and then created my own async server.


Thanks! I've found the tutorial and read a little bit.
So i have to use nonblocking sockets if i wanna use connect() and select()?? Or is there another possibility?
Not if you want high performance.
Quote:Original post by SteveTaylor
Not if you want high performance.


And how i can make connect() and select() work with blocking sockets?
It doesn't make sense to use select() with blocking sockets. Of course I'm coming from a Java NIO perspective, but it is roughly the same thing. select() will find sockets that are ready to send/receive data, so any operation on them will not block. In java, the select operation itself will bock only if no sockets are ready. Probably the same in the C world too :)
Quote:It doesn't make sense to use select() with blocking sockets.


It makes perfect sense to use select() on blocking sockets. You will get whatever data is in the incoming buffer from a recv(), without blocking, and you can send at least one byte with a send(), without blocking. In reality, you'll send significantly more than one byte, because of the silly windows avoidance code in the TCP stack. If you use select(), and have your own user-side buffering for outgoing messages, there is no reason to use non-blocking sockets at all; blocking sockets do all you need.

Note that select() has problems scaling to really large number of sockets, though (i e thousands). Instead, you have to look at kqueue (BSD), sys_epoll (Linux), or I/O Completion Ports (Windows). Sadly, the latter require threading.
enum Bool { True, False, FileNotFound };
So how can i make select() work with connect()???
If you don't want to block on connect(), you have to make that socket non-blocking. IIRC, select() will tell you that the socket has connected by signalling that it's writable.

However, most of the time, connect() only happens on the client, before any real communication is established, so the fact that it's blocking is often not a big deal.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement