Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Non-Blocking (?) sockets

This topic is 6931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For my project, I need to be able to make it accept incoming socket connections, but not WAIT for them... It needs to do other things while it is waiting for a new connection (like service other connections). I can''t use any threads either . This is going to be running under linux, but right now it is under win98. --------------------

You are not a real programmer until you end all your sentences with semicolons;


Visit the ROAD Programming Website for more programming help.

Share this post

Link to post
Share on other sites
I believe what you are looking to do is to "mutliplex".

Multiplex - Relating to or being a system of simultaneous communication of two or more messages on the same wire or radio channel.

Ok this definition were if it were on the same "wire" but it applies to sockets. There is a function that can be found in unix/linux and even VC called "select" what this function does is check several sets of sockets for what they can do. More specifically can you read a socket, write to a socket, and does the socket have an exception (which means something bad happened, or OOB data is waiting).

If you add the socket that is connected to the port you are listening to, to the read set and select tells you it can be read from, accept will succeed without blocking (in other words a connection is waiting).

Here are some key words you will want to look at.
select - the function that multiplexes
struct fd_set - file descriptor set, you pass three of these to select
FD_SET - macro to add a socket to a fd_set structures list
FD_ZERO - clears a set to have it list nothing
FD_CLR - removes a single socket from the list
FD_ISSET - returns a boolean value that signifies if a socket is in a set.

A basic synopsis of what happens:
1. Create three "struct fd_set"''s one for reading, one for writing, one for exceptions.
2. Add those sockets you want to test for each catagory, also if you want to see if a connection is waiting add the socket that is bound to the listening port. (FD_SET macro is used to add the sockets to the fd_set''s)
3. Use the select function and pass the fd_sets to it.
4. Use the FD_ISSET macro to see if a socket is still in the set. (select removes those sockets that don''t apply to each catagory, in other words if there is no data waiting to be read from a socket, it will no longer be in the read set.)
5. If you want to test for a connection, FD_ISSET to see if the bound socket is still in the set.
6. Use whatever means is needed to get or send data, and accept the new connection if it is waiting.

Sorry if that isn''t clear, here are a few resources.
1. Beej''s socket tutorial - http://sol.te.net.ua/sockets/netsock.html - this has a small example of how select is used.
2. If you use VC look up the select function, it actually has a somewhat good explination of all its features!

Hope this helps!

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!