BSD Sockets - How to use non-blocking sockets?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

Whenever I run it, the server still waits for me to send something before it will read and output what the client has sent Well, that is how you wrote it. You block on IO from stdin, and then and only then do you send/receive cin>>out; cin.get() Also, you are using a local socket (AF_UNIX) which creates a special file in your filesystem for interprocess communication - this is a different mechanism than IP, and is definitely not TCP as you indicate in your question. I suppose you could name the file 127.0.0.1 but that really doesn't make sense and implies confusion on your part, because that is an IP loopback address.

You'll want to use AF_INET for IP For an excellent starter guide on unix networking, I'd recommend beej.us/guide/bgnet If you want the display of messages received to be independant of your cin statements, either fork() off a seperate process to handle your network IO, or use a separate thread You might be interested in select(). In my opinion non-blocking sockets are usually a hack, and proper usage of select() or poll() is generally much better design and more flexible (and more portable). Try man select_tut for more information.

Whenever I run it, the server still waits for me to send something before it will read and output what the client has sent. Well, that is how you wrote it. You block on IO from stdin, and then and only then do you send/receive.

Cin>>out; cin.get(); Also, you are using a local socket (AF_UNIX) which creates a special file in your filesystem for interprocess communication - this is a different mechanism than IP, and is definitely not TCP as you indicate in your question. I suppose you could name the file 127.0.0.1, but that really doesn't make sense and implies confusion on your part, because that is an IP loopback address. You'll want to use AF_INET for IP.

For an excellent starter guide on unix networking, I'd recommend beej.us/guide/bgnet/ If you want the display of messages received to be independant of your cin statements, either fork() off a seperate process to handle your network IO, or use a separate thread. You might be interested in select(). In my opinion non-blocking sockets are usually a hack, and proper usage of select() or poll() is generally much better design and more flexible (and more portable).

Try man select_tut for more information.

Thanks, I will look into fork() and select(). Whenever I change all the AF_UNIX's to AF_INET, I get an Invalid Argument error when the client tries to connect. Should I only be changing it on the server side?

– Sterling Jul 14 at 21:12 Well, you will need to change both server and client to AF_INET. The difference between the two is large - one is a local (or unix) socket, the other is a network socket, specifically an IP socket. Definately take a look at the Beej's guide I posted, and then sanity check your code to compare.

I hope that helps! – Josh Jul 14 at 21:15 Well thats what I meant - I changed them both. I am reading the link now.

– Sterling Jul 14 at 21:18.

I think you have to set non-block sooner (ie get the socket then set it non block) also check that the fcntl to set it actually worked.

1 for "check that the fcntl to set it actually worked". Always check the return value of system calls. Always.

– Nemo Jul 14 at 20:42 I set it to non-block right after I get it through the accept call. If I set the server socket, sock, to non block it gives me an error of "resource temporarily unavailable" when I try to call accept. – Sterling Jul 14 at 20:51.

General approach for a TCP server where you want to handle many connections at the same time: make listening socket non-blocking add it to select(2) or poll(2) read event set enter select(2)/poll(2) loop on wakeup check if it's the listening socket, then accept(2) check for failure (the client might've dropped the connection attempt by now) make newly created client socket non-blocking, add it to the polling event set else, if it's one of the client sockets consume input, process it watch out for EAGAIN error code - it's not really an error, but indication that there's no input now if read zero bytes - client closed connection, close(2) client socket, remove it from event set re-init event set (omitting this is a common error with select(2)) repeat the loop Client side is a little simpler since you only have one socket. Advanced applications like web browsers that handle many connections often do non-blocking connect(2) though.

If you want non-blocking i/o, you want to use select. You can set it with stdin as one of the sockets it is listening on, along with the client sockets (just add file descriptor 1, which is stdin, to the fd_set). beej.us/guide/bgnet/output/html/multipag... I would recommend reading through what beej has to say about select.It looks a little intimidating but is really useful and simple to use if you take a little time to wrap your head around it.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions