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

How to use setsockopt under unix /solaris 2.6

I am trying to add a timeout to send and recv functions for TCPSocket  in C++ on unix/solaris 2.6 environment .
to solve I  am trying to use setsockopt with SO_RCVTIMEO for recv and I am getting the compilation errors as
code:
        struct timeval tv;
        tv.tv_sec =  timeout_n ;
        tv.tv_usec = 0 ;
        setsockopt( sockDesc , SOL_SOCKET , SO_RCVTIMEO , &tv , sizeof(tv) ) ;
        setsockopt( sockDesc , SOL_SOCKET , SO_SNDTIMEO , &tv , sizeof(tv) ) ;
Error:
Formal argument 4 of type const char* in call to setsockopt(int, int, int, const char*, int) is being passed timeval*.

Please advise.

0
bmanik
Asked:
bmanik
  • 4
  • 2
1 Solution
 
bkthompson2112Commented:
Hi bmanik,

Using Tru64, this works:
setsockopt( sockDesc , SOL_SOCKET , SO_RCVTIMEO , (void *) &tv , sizeof(tv) ) ;   // ---------added (void *)

What's the man page say?

bkt
0
 
bmanikAuthor Commented:
man page doesnt specify SO_RCVTIMEO ,but I was following richard stevens book and coded accordingly , there must be some other way to set the time outs , pls advise .
0
 
bkthompson2112Commented:
> Formal argument 4

would be &tv.

Make sure you've cast it to the correct type.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
bmanikAuthor Commented:
Thanks , I did type cast it and its now clean compiled , going ahead for testing it now .
0
 
bkthompson2112Commented:
You're welcome.  Hope it's working now.
0
 
sunnycoderCommented:
check the man page .... this particular option, though existing is not necessarily working on all platforms.

It wont report an error but will be silently ignored
0
 
bkthompson2112Commented:
>I did type cast it and its now clean compiled , going ahead for testing it now

bmanik, how did the testing go?  is it working now?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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