bind() with only sockaddr fails, sockaddr_storage works..

I think that I am missing something.
First of all let me use the standard excuse that I am new into this. :wink:

At the moment I am trying to set up a bind() within Digi ME. But my bind function always gives me always a minus 1 as result.

I made successful a socket, that was not hard and listening from it is also not a problem. But bind is strange to me.

The help file writes that I should use “Pointer to a sockaddr structure that stores the attributes to be bound to the socket endpoint address of the connected socket.”
Okay, and I should use INADDR_ANY to avoid static IP addresses. Assuming that I should set this IP-address in the sock settings, called a sock structure.

When I look at the sockaddr struct, the confusion starts.
The building stones of the struct.
sa_len Total Length
sa_family Address Family AF_xxx
sa_data Protocol specific address

Total Length? Would be nice when known which length I should use.
AF_xxx is crystal clear, I should pick IPv4 or IPv6. That is well documented.
Then sa_data… It is char[], how can I place an value of INADDR_ANY in here?

While using the union from sock_addr with only the family en port set, I have no problems at all.
There must be something I am doing wrong, but what…

I think the confusion is in the typecasting to sockaddr. Generally I do not attempt to fill in a struct sockaddr but I mught typecast to it. What I fill in is (from the NET+OS API reference guide, a struct in_sockaddr.
struct in_sockaddr
{
i16 sin_family;
u16 sin_port;
struct in_addr sin_addr;
char sin_zero[8];
}

struct in_addr
{
u_32 saddr;
}
typedef short i16;
typedef unsigned short u16;
typdef unsigned int u32;

with this you can fill in an addr or INADDR_ANY into saddr.

This is more or less explained in a book such as UNIX Network Programming (volume 1) by W. Richard Stevens.

Ah, the functions are from the regular UNIX network C functions. After reading more information the whole situation became clear to me. Tank you.
TCP is now up and running and UDP is now in progress. :slight_smile:

A mistake from my side. The text writes a pointer to A sockaddr. That does not mean the sockaddr, but a type cast is also welcome.

On the other hand, I still do not know what the meaning of total_length is.