

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Socket Introduction 1
struct sockaddr { uint8_t sa_len; sa_family_t sa_family; /address family: AF_xxx value/ char sa_data[14]; /protocol specific address/ }**
struct sockaddr_in serv; /IPv4 socket address structure/ /* fill in serv{} */ bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));
Comparison of socket address structure
IPv4 Header
IPv
Address
Socket address structure pass.
Accept, recvfrom, getsockname, getpeername
length Socket Address structure
Kernel
int *
User
process
value result
Protocol address
struct sockaddr_in serv
/* fill in serv{} */
connect(sockfd, (SA *)&serv,
sizeof(serv));
Figure3.9 determine host byte order
**int main(int argc, char argv) { union {short s; char c[sizeof(short)]; } un; un.s = 0x0102; printf("%s: ", CPU_VENDOR_OS); if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf("big-endian\n"); else if (un.c[0] == 2 && un.c[1] == 1) printf("little-endian\n"); else printf("unknown\n"); } else printf("sizeof(short) = %d\n", sizeof(short)); exit(0); }
void bzero(void dest, size_t nbytes); / sets the specified no of bytes to zero */
void bcopy(const void *src, void dest, size_t nbytes); / byte by byte copy from source to destination */
int bcmp(const void *ptr1, const void ptr2, size_t nbytes); / return 0 if equal, nonzero if unequal */
For ASCII to network binary:
int inet_aton(const char *strptr, struct in_addr *addrptr);
/* return : 1 if string was valid,0 on error */
For ASCII to network binary:
in_addr_t inet_addr(const char *strptr);
/* deprecated. return : 32bit binary network byte ordered IPv4 address; INADDR_NONE if error */
For network binary to ASCII:
char *inet_ntoa(struct in_addr inaddr);
/return pointer to dotted-decimal string/