Document

advertisement
Socket实验汇总
1
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.
2
Open()
Name of the file
open
File descriptor or -1


O_RDONLY
O_WRONLY
O_RDWR
…
(<fcntl.h>)
Be used to open or create a file
int open (char *pathname, int oflag, int mode);
Only used when
creating a file to
indicate the access
authority
3
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
4
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
Host entry
/* 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. */
};
6
一些提示

如何判断是根据domain name查IP还是根据IP查domain name?


DNS查询后返回的是指向一个struct hostent的指针,如何引用struct中的
分量?


for (p=ptr->h_alias; *p!=NULL; p++)
如何打印IP地址?


Ptr->h_length
如何访问到alias列表和IP地址列表中的每个值?


Inet_aton()的返回值
Inet_ntoa ()或inet_ntop ()
段错误
7
通过IP地址查询域名


可以使用8.8.8.8
或者学习使用nslookup命令,先查询某
个IP地址是否可以正确解析
8
切换Terminal


Alt F2,切换到新Terminal
Alt F1,回到之前的Terminal
9
How to print IP address?

Conversion between binary data (unsigned long) and
dot-decimal notation
#include <arpa/inet.h>
Only support IPv4
char * inet_ntoa( struct in_addr in)
string(dot-decimal)
network address(binary)
int inet_aton( const char * cp , struct in_addr * inp )
1- successful
0-failed
10
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
11
An Example

Only an example, you can design the format by yourself
12
验收时间

3月28日之前
13
Download