socket 实验汇总 1. 2 lab1( 需验收 ) run all the sample programs today and answer the...

13
Socket 实实实实 1

Upload: linda-letitia-ross

Post on 03-Jan-2016

291 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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:

Socket实验汇总

1

Page 2: 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:

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.

Page 3: 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:

3

Open()

open Be used to open or create a file int open (char *pathname, int oflag, int mode);

Name of the file

O_RDONLYO_WRONLYO_RDWR…(<fcntl.h>)

Only used when creating a file to indicate the access authority

File descriptor or -1

Page 4: 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:

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

Page 5: 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:

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., ./<exefile> www.baidu.com ./<exefile> 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

Page 6: 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:

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. */};

Page 7: 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:

一些提示 如何判断是根据 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

Page 8: 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:

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

8

Page 9: 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:

切换 Terminal

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

9

Page 10: 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:

How to print IP address? Conversion between binary data (unsigned long) and

dot-decimal notation

10

#include <arpa/inet.h>

char * inet_ntoa( struct in_addr in)

int inet_aton( const char * cp , struct in_addr * inp )

string(dot-decimal)

network address(binary)

1- successful0-failed

Only support IPv4

Page 11: 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:

11

Lab3(需验收 ) Write the programs for data sending

based on UDP You can design the running command format,

e.g., ./<exefile> <server> <data> …… 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

Page 12: 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:

12

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

Page 13: 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:

验收时间 3 月 28日之前

13