• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

setsockopt

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
areeves
Asked:
areeves
  • 3
  • 2
1 Solution
 
julio011597Commented:
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
 
areevesAuthor Commented:
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
 
julio011597Commented:
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
 
julio011597Commented:
BTW, was this worth a D grade?
Ok, never mind, i guess you're new here...

Cheers, julio
0
 
areevesAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now