TCP/IP sockets: runtime bind() error: invalid argument

TCP/IP sockets: runtime bind() error: invalid argument

Post by Monic » Fri, 14 Jun 1996 04:00:00



Problem: I am trying to use the C function int bind(int, struct sockaddr *,
int) for my socket setup server function.  I will be in domain internet sending
SOCK_STREAM .

Platform: I'm on a SunSPARC Sun 4.1.3, rloging to another UNIX machine running
Sun 5.4, solaris 2.4.  I am trying to port code that worked just fine on
another architecture (HP I think)

Compiler: I am using the CenterLine C Compiler using clcc -g

Details:
I have included all the "right" files:

#include <sys/types.h>
#include <sys/socket.h>

/* defined in <sys/socket.h> */
   struct {
      /* fields defined here */
      /*     int             */
      /*     char array[12]  */

          } sockaddr;
/* defined in <sys/socket.h> */
   extern int bind(int,struct sockaddr *, int);

/*
 *
looked through all the include files and was not able to find the function
definition of bind(int,struct sockaddr *, int) { ???? } but since I included
all of the files that the original author included then I should have no
problem, right? Wrong!  
 *
 */

#include <netinet/in.h>

/* defined in <netinet/in.h> */
   struct {
      /* fields defined here */
      /*    int              */
      /*    int              */
      /*    char             */
      /*    char array[8]    */

          } sockaddr_in;

/* defined in <netinet/in.h> */
  struct sockaddr_in *servaddr;

#include <arpa/inet.h>

/* Begin my program with all of the above include files (+other) included */

int sock_integer;

/*
 *
here I'm calling bind, casting from a pointer to structure sockaddr_in, to a
pointer to structure sockaddr
 *
 */

some_int = bind(sock_integer, (struct sockaddr *) &servaddr, SIZEOF(servaddr));

When my compiled and linked program comes to the above bind() call, I get my
error: bind() invalid argument.

I have also tried variations on bind() albeit silly, but I am desperate:
some_int = bind(sock_integer, (struct sockaddr *) &servaddr, SIZEOF(servaddr));
some_int = bind(sock_integer, (struct sockaddr *) servaddr, SIZEOF(servaddr));
some_int = bind(sock_integer, (struct sockaddr *) &servaddr,
SIZEOF(&servaddr));
some_int = bind(sock_integer, (struct sockaddr *) servaddr, SIZEOF(&servaddr));

Anyway none of the above worked any better, usually worse.

Has anyone had this problem before?

Thanks, Monica

 
 
 

TCP/IP sockets: runtime bind() error: invalid argument

Post by Andrew Gier » Fri, 14 Jun 1996 04:00:00



Quote:>Problem: I am trying to use the C function int bind(int, struct sockaddr *,
>int) for my socket setup server function.  I will be in domain internet sending
>SOCK_STREAM .

>Platform: I'm on a SunSPARC Sun 4.1.3, rloging to another UNIX machine running
>Sun 5.4, solaris 2.4.  I am trying to port code that worked just fine on
>another architecture (HP I think)

>Compiler: I am using the CenterLine C Compiler using clcc -g

[much snippage]

Quote:>some_int = bind(sock_integer, (struct sockaddr *) &servaddr, SIZEOF(servaddr));

>When my compiled and linked program comes to the above bind() call, I get my
>error: bind() invalid argument.

I hope you successfully called socket() and assigned the result to
sock_integer. You don't show us that bit, so it's a little difficult to
tell if it's correct.

The form of the call is basically correct, assuming that you have
initialised the servaddr structure correctly and that SIZEOF is defined
to do the obvious thing. (Do you have some particular reason not to use
just sizeof(servaddr)?)

You might want to take a look at the unix-socket-faq, regularly posted
here (and in news.answers & comp.answers), and also available at:

  http://www.auroraonline.com/sock-faq
  http://kipper.york.ac.uk/~vic/sock-faq
  ftp://rtfm.mit.edu/pub/usenet/news.answers/unix-faq/socket

There are examples available with it that may assist with your problem.


"Usenet is like a herd of performing elephants with diarrhea; massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it." [Gene Spafford]

 
 
 

TCP/IP sockets: runtime bind() error: invalid argument

Post by Dan McDona » Sat, 15 Jun 1996 04:00:00




>Problem: I am trying to use the C function int bind(int, struct sockaddr *,
>int) for my socket setup server function.  I will be in domain internet \
>sending SOCK_STREAM .

<SNIP!>

Quote:>/* defined in <netinet/in.h> */
>  struct sockaddr_in *servaddr;

First off, you have a POINTER to a sockaddr struct, not a sockaddr struct
itself.  Are you allocating storage for this?  If so, then cool.  Otherwise
call malloc(), or assign the pointer to some other storage.

Quote:>some_int = bind(sock_integer, (struct sockaddr *) &servaddr,
>    SIZEOF(servaddr));

You're passing a pointer to a pointer in this case.  You're lucky you didn't
dump core.  If you do have servaddr pointing to some storage (perhaps you
malloc()ed some), try this:

        some_int = bind(sock_integer, (struct sockaddr *) servaddr,
                sizeof(*servaddr));

The last parameter to bind() should be the length of the sockaddr structure.
You passed the sizeof a pointer (4 bytes on 32-bit machines).  Like I said,
you're lucky you didn't dump core.

>Has anyone had this problem before?


I had problems like these way back when I was cutting my teeth on C.  Funny
thing is that I was at NASA Goddard when I was doing it.  (That was a while
ago.)

Hope this helps.  Good luck.
--

Software Engineer  | *** My opinions aren't necessarily Sun's opinions! ***  |
SunSoft Internet   | "rising falling at force ten                            |
        Engineering|  we twist the world and ride the wind"  -  Rush         +

 
 
 

TCP/IP sockets: runtime bind() error: invalid argument

Post by Andrew Gier » Sat, 15 Jun 1996 04:00:00



Quote:>Problem: I am trying to use the C function int bind(int, struct sockaddr *,
>int) for my socket setup server function.  I will be in domain internet sending
>SOCK_STREAM .

>Platform: I'm on a SunSPARC Sun 4.1.3, rloging to another UNIX machine running
>Sun 5.4, solaris 2.4.  I am trying to port code that worked just fine on
>another architecture (HP I think)

>Compiler: I am using the CenterLine C Compiler using clcc -g

[much snippage]

Quote:>some_int = bind(sock_integer, (struct sockaddr *) &servaddr, SIZEOF(servaddr));

>When my compiled and linked program comes to the above bind() call, I get my
>error: bind() invalid argument.

I hope you successfully called socket() and assigned the result to
sock_integer. You don't show us that bit, so it's a little difficult to
tell if it's correct.

You might want to take a look at the unix-socket-faq, regularly posted
here (and in news.answers & comp.answers), and also available at:

  http://www.auroraonline.com/sock-faq
  http://kipper.york.ac.uk/~vic/sock-faq
  ftp://rtfm.mit.edu/pub/usenet/news.answers/unix-faq/socket

There are examples available with it that may assist with your problem.


"Usenet is like a herd of performing elephants with diarrhea; massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it." [Gene Spafford]