N
nonzero
I was hoping someone could explain the Syntax of the second argument of the
bind() function.
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
.....................(struct sockaddr
*)&my_addr....................................
I understand that it is converting the structure at &my_addr to a structure
type sockaddr. What I don't understand is the syntax. I don't understand
why you need the & before my_addr, and why it is outside of the quotes, and
the * is on the inside of the quotes. I haven't found any other code as an
example that has the same syntax. If anyone could provide any examples or
insight, it would be much appreciated.
Here's a link and a cut and paste of the code I am talking about...
http://www.ecst.csuchico.edu/~beej/guide/net/html/syscalls.html#bind
Here is the synopsis for the bind() system call:
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
sockfd is the socket file descriptor returned by socket(). my_addr is a
pointer to a struct sockaddr that contains information about your address,
namely, port and IP address. addrlen can be set to sizeof(struct sockaddr).
Whew. That's a bit to absorb in one chunk. Let's have an example:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 3490
main()
{
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("10.12.110.57");
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
// don't forget your error checking for bind():
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
.
.
.
bind() function.
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
.....................(struct sockaddr
*)&my_addr....................................
I understand that it is converting the structure at &my_addr to a structure
type sockaddr. What I don't understand is the syntax. I don't understand
why you need the & before my_addr, and why it is outside of the quotes, and
the * is on the inside of the quotes. I haven't found any other code as an
example that has the same syntax. If anyone could provide any examples or
insight, it would be much appreciated.
Here's a link and a cut and paste of the code I am talking about...
http://www.ecst.csuchico.edu/~beej/guide/net/html/syscalls.html#bind
Here is the synopsis for the bind() system call:
#include <sys/types.h>
#include <sys/socket.h>
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
sockfd is the socket file descriptor returned by socket(). my_addr is a
pointer to a struct sockaddr that contains information about your address,
namely, port and IP address. addrlen can be set to sizeof(struct sockaddr).
Whew. That's a bit to absorb in one chunk. Let's have an example:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 3490
main()
{
int sockfd;
struct sockaddr_in my_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0); // do some error checking!
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = inet_addr("10.12.110.57");
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
// don't forget your error checking for bind():
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
.
.
.