域名服務(DNS)
如果你不知道 DNS 的意思,那么我告訴你,它代表域名服務(Domain Name Service)。它主要的功能是:你給它一個容易記憶的某站點的地址, 它給你 IP 地址(然后你就可以使用 bind(), connect(), sendto() 或者其它 函數) 。當一個人輸入:
$ telnet whitehouse.gov
telnet 能知道它將連接 (connect()) 到 "198.137.240.100"。
但是這是如何工作的呢? 你可以調用函數 gethostbyname():
#include
struct hostent *gethostbyname(const char *name);
很明白的是,它返回一個指向 struct hostent 的指針。這個數據結構 是這樣的:
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
這里是這個數據結構的詳細資料:
struct hostent:
h_name – 地址的正式名稱。
h_aliases – 空字節-地址的預備名稱的指針。
h_addrtype –地址類型; 通常是AF_INET。
h_length – 地址的比特長度。
h_addr_list – 零字節-主機網絡地址指針。網絡字節順序。
h_addr - h_addr_list中的第一地址。
gethostbyname() 成功時返回一個指向結構體 hostent 的指針,或者 是個空 (NULL) 指針。(但是和以前不同,不設置errno,h_errno 設置錯 誤信息。請看下面的 herror()。)
但是如何使用呢? 有時候(我們可以從電腦手冊中發現),向讀者灌輸 信息是不夠的。這個函數可不象它看上去那么難用。
這里是個例子:
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
struct hostent *h;
if (argc != 2) { /* 檢查命令行 */
fprintf(stderr,"usage: getip address\n");
exit(1);
}
if ((h=gethostbyname(argv[1])) == NULL) { /* 取得地址信息 */
herror("gethostbyname");
exit(1);
}
printf("Host name : %s\n", h->h_name);
printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}
在使用 gethostbyname() 的時候,你不能用 perror() 打印錯誤信息 (因為 errno 沒有使用),你應該調用 herror()。
相當簡單,你只是傳遞一個保存機器名的字符串(例如 "whitehouse.gov") 給 gethostbyname(),然后從返回的數據結構 struct hostent 中獲取信息。
唯一也許讓人不解的是輸出 IP 地址信息。h->h_addr 是一個 char *, 但是 inet_ntoa() 需要的是 struct in_addr。因此,我轉換 h->h_addr 成 struct in_addr *,然后得到數據。
--------------------------------------------------------------------------------
客戶-服務器背景知識
這里是個客戶--服務器的世界。在網絡上的所有東西都是在處理客戶進 程和服務器進程的交談。舉個telnet 的例子。當你用 telnet (客戶)通過23 號端口登陸到主機,主機上運行的一個程序(一般叫 telnetd,服務器)激活。 它處理這個連接,顯示登陸界面,等等。
圖2:客戶機和服務器的關系
圖 2 說明了客戶和服務器之間的信息交換。
注意,客戶--服務器之間可以使用SOCK_STREAM、SOCK_DGRAM 或者其它(只要它們采用相同的)。一些很好的客戶--服務器的例子有 telnet/telnetd、 ftp/ftpd 和 bootp/bootpd。每次你使用 ftp 的時候,在遠 端都有一個 ftpd 為你服務。
一般,在服務端只有一個服務器,它采用 fork() 來處理多個客戶的連 接。基本的程序是:服務器等待一個連接,接受 (accept()) 連接,然后 fork() 一個子進程處理它。這是下一章我們的例子中會講到的。
--------------------------------------------------------------------------------
簡單的服務器
這個服務器所做的全部工作是在流式連接上發送字符串 "Hello, World!\n"。你要測試這個程序的話,可以在一臺機器上運行該程序,然后 在另外一機器上登陸:
$ telnet remotehostname 3490
remotehostname 是該程序運行的機器的名字。
服務器代碼:
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 3490 /*定義用戶連接端口*/
#define BACKLOG 10 /*多少等待連接控制*/
main()
{
int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd
*/
struct sockaddr_in my_addr; /* my address information */
struct sockaddr_in their_addr; /* connector's address information */
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
bzero(&(my_addr.sin_zero),; /* zero the rest of the struct */
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct
sockaddr))== -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) { /* main accept() loop */
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, \
&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n", \
inet_ntoa(their_addr.sin_addr));
if (!fork()) { /* this is the child process */
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); /* parent doesn't need this */
while(waitpid(-1,NULL,WNOHANG) > 0); /* clean up child processes */
}
}
如果你很挑剔的話,一定不滿意我所有的代碼都在一個很大的main() 函數中。如果你不喜歡,可以劃分得更細點。
你也可以用我們下一章中的程序得到服務器端發送的字符串。
--------------------------------------------------------------------------------
簡單的客戶程序
這個程序比服務器還簡單。這個程序的所有工作是通過 3490 端口連接到命令行中指定的主機,然后得到服務器發送的字符串。
客戶代碼:
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3490 /* 客戶機連接遠程主機的端口 */
#define MAXDATASIZE 100 /* 每次可以接收的最大字節 */
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; /* connector's address information */
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { /* get the host info */
herror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; /* host byte order */
their_addr.sin_port = htons(PORT); /* short, network byte order */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero),; /* zero the rest of the struct */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct
sockaddr)) == -1) {
perror("connect");
exit(1);
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
注意,如果你在運行服務器之前運行客戶程序,connect() 將返回 "Connection refused" 信息,這非常有用。
--------------------------------------------------------------------------------