Solved

setsockopt

Posted on 1997-05-14
5
674 Views
Last Modified: 2008-03-17
I am currently trying to implement my first client server
application. I am using Perl 5.002 to do so. I noticed
that in the Camel Book pg.350 under the section about
sockets, they give an example of a multi-threaded server.
In that example the following statment is found:

        setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("1", 1))
                or die "setsockopt: $!";

But when I looked up the definition of setsockopt in the
aphabetical listing of all Perl functions (on pg.214) it says:

        setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 1)
                or warn "Can't do setsockopt: $!\n";

My question is why would you use pack to set the value of OPTVAL
in the first example?

Any help would be greatly appreciated!
0
Comment
Question by:areeves
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 200 total points
ID: 1204006
pack("1", 1) should explicitly return the binary number 00000001.

Anyway, there's no difference between the two forms, as long as setsockopt() is concerned.
In fact, having a look at setsockopt(2), you may see that the 'option_value' parameter may be _any_ non-zero value for enabling a boolean option (zero for disabling it).

HTH, julio
0
 

Author Comment

by:areeves
ID: 1204007
OK, I guess I am still fuzzy on what all this means.
When you say OPTVAL can be any non-zero number to
enable boolean options, are you talking about OPTNAME?
The Pearl5 Camel Book talks about passing arguments,
and then states in the next sentence a common option
to set would be SO_REUSEADDR. Are they talking about
OPTNAME being the argument and/or the boolean option
you are talking about?

Then in the man pages it says that OPTVAL is the
address of a location in memory that contains the
option information to be passed to the calling
process. What are they talking about? And how does
it relate to this discussion?
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1204008
Hello, setsockopt(2) gives this synopsis (on DU), but note that this is a _C_function_:

int setsockopt (
  int socket,
  int level,
  int option_name,
  const void *option_value,
  size_t option_len );

The Perl setsockopt() is something similar (many perl functions resamble to C functions), but it does not need an 'option_len' parameter.

So:
'option_name' specifies the option to set;
'option_value' specifies if we want to enable or disable that option.

Now should be clear that, in the Perl5 Camel Book, SO_REUSEADDR is meant as the 'option_name' argument.

The problem with man pages is that you usually find the corrensponding C functions, but you should possibly care of the differences between C and Perl.

E.g., in the setsockopt() case, the 'option_value' parameter needed by the C function is a pointer to a location which holds the actual value, while the Perl function just needs the value itself.

In general, you may go this way (but a bit of experience will be of help): read a man page to understand the meaning and purpose of parameters, then go back to the book and (usually) you find that you just need to get rid of C pointers and directely pass values (variables) as parameters to the Perl function.

Hope this makes things a bit clearer, dispite of my English :)

Rgds, julio
0
 
LVL 5

Expert Comment

by:julio011597
ID: 1204009
BTW, was this worth a D grade?
Ok, never mind, i guess you're new here...

Cheers, julio
0
 

Author Comment

by:areeves
ID: 1204010
Sorry, for the D grade, but I just felt like you could have
given me a little more explanation with your first answer.
Remember I said I was developing my first client server
application. Now if you would have given me the explanation you
gave me in the second response maybe a better grade would have
been in order. Also I don't really think your first response
directly answered my question. Were the authors of the Perl5
Camel Book trying to throw the reader a curve ball by doing
the same thing two different ways or was there really a reason
for what they did?

I hope you understand my position, and sorry if I insulted you
by giving you a low grade. None the less, I still appreciated
the help!

Thanks again for your quick response!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 144
pipe to sed or perl, please help 5 120
Using Perl to parse rows 7 86
use google analytics code in perl script 2 56
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now