90 likes | 183 Views
Advanced UNIX programming. Fall 2002 Instructor: Ashok Srinivasan Lecture 17. Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan. Announcements. Reading assignment Chapters 3 and 4 of UNP Collect midterm from me
E N D
Advanced UNIX programming Fall 2002 Instructor: Ashok Srinivasan Lecture 17 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan
Announcements • Reading assignment • Chapters 3 and 4 of UNP • Collect midterm from me • ... during my office hours • T 2:15-3:15 pm or W 2:15-3:15 pm • If you have questions on HW2 results, meet the grader
Week 7 Topics • Basic TCP sockets • Socket address structure • Byte ordering and manipulation functions • socket, connect, bind, listen, accept • Client-Server design • Concurrent server
Basic TCP sockets • Socket address structure • Byte ordering and manipulation functions • socket, connect, bind, listen, accept
Socket address structure struct in_addr { in_addr_t s_addr; } struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; } • Always use sockaddr_in type for manipulation and convert it to sockaddr • See example1.c and example2.c struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; }
Network byte order • What happen when we run example2.c as server on program and example1.c as client on linprog? • sin_port and sin_addr must be in network byte order • Check example3.c • What is the difference between program and linprog?
Byte ordering functions • #include <arpa/inet.h> uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohl(uint32_t net32bitvalue); • See example3.c
Byte manipulation functions • #include <string.h> void *memset(void *dst, int c, size_t len); void *memcpy(void *dst, void *src, size_t nbytes); void *memcmp(const void *ptr1, const void *ptr2, size_t nbytes);
Address conversion functions • #include <arpa/inet.h> • in_addr_t inet_addr(const char *cp); • char *inet_ntoa(struct in_addr in); • The ones below should be preferred • inet_ntop(address family, source, destination, size); • inet_pton(address family, source, destination);