Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket 实验汇总 1. 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question:

Similar presentations


Presentation on theme: "Socket 实验汇总 1. 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question:"— Presentation transcript:

1 Socket 实验汇总 1

2 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question: 1. How to use lseek() to append a file to another file? 2. When using O_APPEND to open a file for reading and writing, can we read the file from any location using lseek()? Can we update the data in any part of the file using lseek()? Please write a program to verify your answer.

3 3 Open() open Be used to open or create a file int open (char *pathname, int oflag, int mode); Name of the file O_RDONLY O_WRONLY O_RDWR … ( ) Only used when creating a file to indicate the access authority File descriptor or -1

4 4 Lab2( 不验收 ) Find the fore mentioned header files in your system in.h, types.h, netdb.h, endian.h, socket.h, … Find the host byte order of your machine Use man to learn the usage of netstat, ifconfig, ping and traceroute

5 Lab2-continue( 需验收 ) Write a program to find the DNS information about a given host The host may be specified in domain name or IP address, e.g.,./ www.baidu.com./ 119.75.218.77 Use gethostbyaddr() and gethostbyname() Your program shall list the official name, all the aliases, all the IP addresses in numbers-and-dots format 5

6 Host entry 6 /* Description of data base entry for a single host. */ 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 from name server. */ #define h_addr h_addr_list[0] /* The first address in the address list. */ };

7 一些提示 如何判断是根据 domain name 查 IP 还是根据 IP 查 domain name ? Inet_aton() 的返回值 DNS 查询后返回的是指向一个 struct hostent 的指针,如何引用 struct 中的 分量? Ptr->h_length 如何访问到 alias 列表和 IP 地址列表中的每个值? for (p=ptr->h_alias; *p!=NULL; p++) 如何打印 IP 地址? Inet_ntoa () 或 inet_ntop () 段错误 7

8 通过 IP 地址查询域名 可以使用 8.8.8.8 或者学习使用 nslookup 命令,先查询某 个 IP 地址是否可以正确解析 8

9 切换 Terminal Alt F2 ,切换到新 Terminal Alt F1 ,回到之前的 Terminal 9

10 How to print IP address? Conversion between binary data (unsigned long) and dot-decimal notation 10 #include char * inet_ntoa( struct in_addr in) int inet_aton( const char * cp, struct in_addr * inp ) string(dot-decimal) network address(binary) 1- successful 0-failed Only support IPv4

11 11 Lab3( 需验收 ) Write the programs for data sending based on UDP You can design the running command format, e.g.,./ …… The server may be specified in domain name or IP address One or more data can be delivered each time Each time the server receives data, the source (client’s IP address and port number) and the data should be displayed on the screen

12 12 An Example Only an example, you can design the format by yourself

13 验收时间 3 月 28 日之前 13


Download ppt "Socket 实验汇总 1. 2 Lab1( 需验收 ) Run all the sample programs today and answer the mentioned questions. Learn about the O_APPEND flag used in open(). Question:"

Similar presentations


Ads by Google