通行证│用户名: 密码: 验证码: 验证码,看不清楚?请点击刷新验证码 电信网通铁通移动   在线
资源搜索:
热门搜索:Linux VB C语言 PhotoShop Flash TCP/IP
   首页 | 文章 | 软件 | 动画 | 资源 | 励志 | 骗术 | 论坛 | 邮箱 | 会员中心 | 军事 | 科技 | 博客 | 图片 | 商城 | 最新更新 | 800g资源 | 爱心黑客
您现在的位置: 爱国者黑客 >> 资源 >> 操作系统 >> Linux >> 编程开发 >> 文章正文
Beejs 网络编程指南 Internet Sockets(2)
责任编辑:admin   更新日期:2005-8-6
gethostname()--Who am I?
甚至比 getpeername() 还简单的函数是 gethostname()。他返回你程序所运行的机器的主机名字。然后你可以使用 gethostbyname() 以获得你的机器的 IP 地址。
下面是定义:


#include

int gethostname(char *hostname, size_t size);

参数很简单:hostname 是一个字符数组指针,他将在函数返回时保存主机名。size 是 hostname 数组的字节长度。

函数调用成功时返回 0,失败时返回 -1,并设置 errno。


--------------------------------------------------------------------------------


DNS--You say "whitehouse.gov", I say "198.137.240.100"
如果你不知道 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 - Official name of the host.
h_aliases - A NULL-terminated array of alternate names for the host.
h_addrtype - The type of address being returned; usually AF_INET.
h_length - The length of the address in bytes.
h_addr_list - A zero-terminated array of network addresses for the host. Host addresses are in Network Byte Order.
h_addr - The first address in h_addr_list.

gethostbyname() 成功时返回一个指向 struct hostent 的指针,或者是个空 (NULL) 指针。(但是和以前不同,errno 不设置,h_errno 设置错误信息。请看下面的 herror()。)

但是如何使用呢? 这个函数可不象他看上去那么难用。

这里是个例子:

#include
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
struct hostent *h;

if (argc != 2) { /* error check the command line */
fprintf(stderr,"usage: getip address\n");
exit(1);
}

if ((h=gethostbyname(argv[1])) == NULL) { /* get the host info */
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 *,然后得到数据


--------------------------------------------------------------------------------


Client-Server Background
这里是个客户--服务器的世界。在网络上的所有东西都是在处理客户进程和服务器进程的交谈。举个 telnet 的例子。当你用 telnet (客户)通过 23 号端口登陆到主机,主机上运行的一个程序(一般叫 telnetd,服务器)激活。他处理这个连接,显示登陆界面,等等。



Figure 2. The Client-Server Relationship.

图 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 /* the port users will be connecting to */

#define BACKLOG 10 /* how many pending connections queue will hold */

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), 8); /* 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 /* the port client will be connecting to */

#define MAXDATASIZE 100 /* max number of bytes we can get at once */

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), 8); /* 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" 信息。


--------------------------------------------------------------------------------


数据报 Sockets
我不想讲更多了,所以我给出代码 talker.c 和 listener.c。

listener 在机器上等待在端口 4590 来的数据包。talker 发送数据包到一定的机器,他包含用户在命令行输入的东西。

这里就是 listener.c:

#include
#include
#include
#incl

[1] [2] [3] 下一页

  • 上一篇文章: 没有了
  • 下一篇文章:
  • 热门文章
    Olldbg常见问题
    汇编语言的艺术(组合语言的艺术)--观
    汇编语言的艺术(组合语言的艺术)--准
    汇编语言的艺术(组合语言的艺术)--基
    汇编语言的艺术(组合语言的艺术)--基
    汇编语言---程式设计 (4)
    虚拟8086模式
    SYS命令使用说明
    javascript + CSS 实现动态菜单显
    推荐文章
    自制Windows XP SP2自动安装光盘
    SQLServer注入工具改进版 v1.02
    使用photoshop CS进行自然美肤
    Photoshop绘制诺基亚手机
    PHOTOSHOP制作秋日之梦
    PHOTOSHOP鼠绘名模王爱萍
    Photoshop制作晶莹飞溅的水珠
    教你用PHOTOSHOP做放大镜
    鼠绘美女及服装修画全过程