1 / 21

20 10 年秋学期 Rodney Van Meter

20 10 年秋学期 Rodney Van Meter. Network Programming in C ネットワークプログラミング Lecture 8, Network Programming (3) 第 8 回「ネットワークとプログラミング (3) 」. 今期の授業スケジュール(予定). 第1回 9/28 : Introduction / イントロダクション 第2回 10/5 : C Basics ~ Functions, Variables, Data Types ・ Makefiles

ziven
Download Presentation

20 10 年秋学期 Rodney Van Meter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2010年秋学期 Rodney Van Meter Network Programming in CネットワークプログラミングLecture 8, Network Programming (3)第8回「ネットワークとプログラミング(3)」

  2. 今期の授業スケジュール(予定) 第1回 9/28: Introduction / イントロダクション 第2回 10/5:C Basics~Functions, Variables, Data Types・Makefiles 第3回 10/12:C Basics~Command Line Arguments・ Structures ・ Pointers 第4回 10/19:C Basics~Pointers & Arrays ・ Lists 第5回 10/26: file I/O・ Network Protocols 第6回 11/2: Network Programming (1)‏ 第7回 11/9: Network Programming (2)‏ 第8回 11/16: Network Programming (3) 11/23: No class! ORF! Please come! 第9回 11/30: Applied Network Programming (1) 第10回 12/7: Applied Network Programming (2) 第11回 12/14: Work on Projects 第12回 12/21: Work on Projects 第13回1/11: Final Presentations!!!

  3. Today Makefiles & libraries Naming AF-independent coding: Itojun’s rules struct sockaddr_storage getaddrinfo() v. gethostbyname()‏ inet_aton() v. inet_pton()‏

  4. Attendance Server Did you get your program to connect to it properly? Same this week, two weeks from now will be different!

  5. Makefiles

  6. Executables Stop using “./a.out”! Stop doing “gcc myfile.c” by hand! 工学っぽくない。

  7. Libraries Big projects use more than one source file Can be put into libraries made with ar linked into final executable using ld (or gcc)

  8. gethostname()‏ Get your own name 自分のホスト名を取得する #include <unistd.h> int gethostname(char *name, size_t len); 返り値:成功した場合0,失敗した場合-1が返る 例: char shostname[HOST_NAME_MAX+1]; gethostname(shostname, sizeof(shostname));

  9. 実習:gethostname()‏ printmyhostname()という関数をつくりましょう。 mylib.cに入れよう。 printmyhostnameというプログラムを作りましょう. Makefileを使ってね。

  10. gethostbyname()‏ ホスト名からIPアドレスを得る 古い方法なんだけど、知っているべき! #include <unistd.h> struct hostent gethostbyname(const char *name); 例 struct sockaddr_in sin; struct hostent *shost; shost = gethostbyname(argv[1]); sin.sin_addr = *(struct in_addr *)hp->h_addr;

  11. sample #include <netdb.h> int main (int argc, char *argv[] ) { int sock_fd; struct sockaddr_in sin; char buf[BUF_SIZE]; int readlen; struct hostent *hp; hp = gethostbyname(argv[1]); /* add */ sock_fd = socket(AF_INET, SOCK_STREAM, 0); sin.sin_family = AF_INET; sin.sin_port = htons(SERV_PORT); sin.sin_addr = *(struct in_addr *)hp->h_addr; }

  12. hostent構造体 struct hostent{ char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype /* host address type */ int h_length /* length of address */ char **h_addr_list; /* list of addresses */ } #define h_addr h_addr_list[0] /* for backward compatibility

  13. 練習: ポイント sock_fd = socket(AF_INET, SOCK_STREAM, 0); sin.sin_family = AF_INET; sin.sin_port = htons(SERV_PORT); sin.sin_addr = *(struct in_addr *)hp->h_addr; connect(sock_fd, (struct sockaddr *)&sin, sizeof(sin)); fgets(buf,sizeof(buf),stdin); write(sock_fd, buf, readlen); readlen = read(sock_fd, buf, sizeof(buf)); printf("%s\n",buf);

  14. AF-independent Code: itojun’s Rules avoid struct in_addr and struct in6_addr. use getaddrinfo() and getnameinfo() everywhere. do not hardcode knowledge about particular AF. http://www.kame.net/newsletter/19980604/ http://www.faqs.org/rfcs/rfc3493.html http://www.usenix.org/publications/library/proceedings/usenix2000/freenix/metzprotocol.html

  15. avoid struct in_addr avoid struct in_addr and struct in6_addr use sockaddr_storage instead more generic, but we still run into trouble: see the Mac problems from last week

  16. use getaddrinfo() everywhere use getaddrinfo() and getnameinfo() everywhere. avoid gethostbyname(), inet_aton()‏

  17. no AF-dependent code Bad example:/* BAD EXAMPLE */switch (sa->sa_family) {case AF_INET: salen = sizeof(struct sockaddr_in);break;} Instead, use res->ai_addrlen returned by getaddrinfo(3)‏

  18. itojun’s Rules: inet_pton()‏ avoid using when possible not very AF-independent stick with getaddrinfo()‏

  19. 実習:getpeername()‏ printpeername()という関数をつくりましょう。 mylib.cに入れよう。 TCPサーバに使いましょう。 inet_ntop()かgetnameinfo()をつかう。

  20. Homework (Two Weeks!) More detailed project proposal Program that prints all addresses for a given name, using getaddrinfo() Must be done using a function in a separate library file, mylib.c Must have Makefile for library mylib.a TCP client that reads index.html from a web server Should use same functions to print out info about host & connection

  21. More Complete Proposal for Term Project “Value Proposition” (why do I care?) Related work (has it been done before?) Key Idea How you will evaluate Schedule with technical milestones 次ページにポイント説明あり

More Related