[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: help with a simple C program



On Sat, Jun 03, 2000 at 11:58:50PM +1000, Shao Zhang wrote:
> Hi,
>     Could anyone explain to me why the following C program always fails?
>     I mean the setsockopt always fails. Thanks for the help.
> 
>     Thanks for any help in advance.
> 
> Regards,
> 
> 
> Shao.
> 
> 
> =====cut here======
> #include <stdio.h>
> #include <stdlib.h>
> #include <sys/socket.h>
> #include <sys/types.h>
> int main(void)
> {
>     int sd, error;
>     const char f = 1;
      ^^^^^^^^^^ (make it an int)
> 
>     sd = socket(AF_INET, SOCK_DGRAM, 0); /* 0 for IP protocol */
>     error = setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (char *) &f, 1);
                                                       ^^^^^^^^^^^^^^
						       no cast,
						       sizeof(f)
>     if (error == -1)
>         printf("setsockopt on %d failed!!\n", sd);
> 
>     close(sd);
>     return 0;
> }

Realize, I could be wrong (but those changes made it work here).

-- 
¶ One·should·only·use·the·ASCII·character­set·when·compos­

» ing·email·messages.




Reply to: