I have a question about socket programming. I am using Linux.

I want to establish a connection between a client and a server using the methods: Server: socket(), bind(), listen(), accept(), connect(), send(), recv(), close(). Client: socket(), connect(), send(), recv().

My question is: If I am a client and I want to connect to a server, how do I know if the server exists or not, if it is ready to be connected to, in other words? Can we avoid busy waiting in this case? I read somewhere that the function Select() handles this problem (cuz it's blocking) but I checked it and it isn't?

I read in another place that the function poll() helps select() somehow, but i didn't find anyway a good written code.


9 Years
Discussion Span
Last Post by Salem

Yes, I tried looking there but it tells us to use select(), and when we tried that out, it returned a live connection when actually there wasn't. I tried creating a client on my computer and told it to connect to myself (while I didn't have a server running) and the select() told me that there is a connection.

And also, a part of the code there says:

for(;;) {
    read_fds = master; // copy it
    if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {

which seems to be busy-waiting,
So unfortunatelly I can't seem to find a good solution.

Thanks again.


Edited by mike_2000_17: Fixed formatting


You could have a parallel thread using another port whose sole duty is to say "yes I'm here," or "I'm busy - try again later."

I wouldn't try to have that in the same thread as the main network code -- too much can go wrong.


Well you didn't use code tags, and you neglected to show us how you actually set up the read_fds.

I'm guessing bugs in your code rather than a failure in the technology.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.