LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-27-2008, 01:36 AM   #1
Linuxfan0001
Member
 
Registered: Apr 2008
Posts: 47

Rep: Reputation: 15
SO_ERROR functionality


Hi,

To test the functionality of SO_ERROR i wrote a sample code where in connect() throws Connection refused error as server is not running and client tries to establish connection. but getsockopt on this fd returns 0 in error parameter in below function call.

getsockopt(Sockfd, SOL_SOCKET, SO_ERROR, &error, &len);
cout << error << endl; displays 0
I tried this on both Linux and Windows.
Under what scenario will this option returns error value. Please let me know so that i can write the sample code to understand the functionality of SO_ERROR.

Thanks....
 
Old 09-04-2008, 12:56 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 09-04-2008, 01:25 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sorry, but the traditional way to do this is to read "errno" (Linux) or WSAGetLastError () (Windows) at the time of error (i.e. when your "connect()" fails; presumably returning "-1").

This is true for all the socket calls ("socket()", "accept()", "setsockopt ()", etc etc).

I suspect the problem here is that, because the "connect()" failed, you don't actually have a socket and there's nothing for "getsockopt ( SO_ERROR )" to return.

But I'm not sure under what circumstances you can legitimately call "getsockopt ( SO_ERROR )", or how portable this approach is. There's really no easy way to adapt a "try/catch" style of programming without writing your own wrapper classes (or using somebody else's OO socket wrappers).

I would definitely encourage you to simply check for (and respond to) errors at the point of call. I would also encourage you to become thoroughly comfortable with "procedural sockets", even if you do ultimately wind up using a higher-level, OO library. Here are two excellent references:

http://beej.us/guide/bgnet/
Stevens, Unix Network Programming (3rd Ed):
http://www.amazon.com/Unix-Network-P...dp/0131411551/

IMHO .. PSM

Last edited by paulsm4; 09-04-2008 at 01:31 PM.
 
Old 09-12-2009, 01:33 PM   #4
AlainLux
LQ Newbie
 
Registered: Sep 2009
Posts: 1

Rep: Reputation: 0
But I'm not sure under what circumstances you can legitimately call "getsockopt ( SO_ERROR )"

This call is for when using "non-blocking" connect calls (after fcntl(fd, O_NONBLOCK)). In that case, the connect system call returns right away, and actual completion can be probed for using select(). Once completed, you use getsockopt(SO_ERROR) on the handle to check whether completion was successful or not, and, if unsuccessful, which failure condition applies.
 
  


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
LiveDistro functionality pooya Linux - Distributions 8 06-22-2007 03:47 AM
Less functionality in 10.1? powadha SUSE / openSUSE 8 05-27-2006 03:59 PM
nessus and more functionality juanb Linux - Security 1 03-07-2006 06:23 AM
I need MS Access functionality! trey85stang Linux - Software 15 05-25-2004 03:40 PM
NAMED functionality Nezar Linux - Networking 1 07-17-2001 03:41 AM

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

All times are GMT -5. The time now is 02:53 AM.

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