LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2004, 06:26 PM   #1
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Rep: Reputation: 15
non_blocking sockets


anyone can give me an example code of how implement non_blocking sockets ? tnx in advance
 
Old 04-06-2004, 05:24 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
What do you mean exactly? Non-blocking write/read or just reusabele socket?
 
Old 04-06-2004, 10:03 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Here's a little example for reading from a nonblocking stream socket. (untested)

Code:
unsigned int bytes_read = 0;
int ret;
char buf[MESSAGE_LEN];

while(bytes_read < MESSAGE_LEN) {
        ret = recv(filedes, buf + bytes_read , MESSAGE_LEN - bytes_read, MSG_DONTWAIT);
        if (ret == EAGAIN)
                do_something_else_for_a_while();
        else if (ret < 0)
                 err(EXIT_FAILURE, "recv");
        else if (ret == 0)
                 errx(EXIT_FAILURE, "client closed the connection.  how rude.");
        else {
                 bytes_read += ret;
         }
}

do_something_with_the_message(buf);
You see that I make the assumption that your messages have a fixed length of MESSAGE_LEN. If they don't, you need to decide for yourself when you're at the end of one.

Of course, the key to this example is do_something_else_for_a_while(). If you actually need to use nonblocking sockets, it's because you want to do something else instead of just sleeping until more data comes in. In my example, this is the function that does that other stuff. If you don't have other stuff to do, you should not be using nonblocking sockets.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c and sockets Matir Programming 4 03-09-2005 04:15 PM
sockets pantera Programming 6 11-24-2004 06:28 AM
Sockets sibtay Programming 4 10-08-2004 09:33 AM
sockets santiagosilva Linux - Networking 0 02-14-2004 07:09 PM
sockets andox Programming 4 07-05-2003 07:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration