您的位置:首页 > 运维架构 > Shell

Linux Socket编程bash服务程序

2009-01-23 15:03 441 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->

很早学习Linux
Socket编程时分析的一个程序:client连接Server,在Server端起动bash,然后执行命令。
源程序地址:http://www.cs.utah.edu/~swalton/listings/sockets/programs/part2/chap7/ls-server.c

基本过程是:Server在端口进行监听,client连接以后,fork一个子进程服务于client,然后子进程复制socket文件描述符到stdin、stdout、stderr三个文件描述符。然后通过execl创建一个shell进程替代原先子进程,此时shell进程中的stdin、stdout、stderr文件描述符实际为socket文件描述符,此时任何stdin、stdout、stderr输入、输出、错误信息都是通过socket文件描述符进行交互的。
程序主要结构为:
int fdsock;
14     struct sockaddr_in local;
15     if ((fdsock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
16     {
17         perror("create socket");
18         exit(errno);
19     }
20     memset(&local, 0, sizeof(local));
21     local.sin_family = AF_INET;
22     local.sin_addr.s_addr = htons(INADDR_ANY);
23     local.sin_port = htons(LOCAL_PORT);
24
25     if ( bind(fdsock, (struct sockaddr*) &local, sizeof(local)) != 0)
26     {
27         perror("bind socket");
28         exit(errno);
29     }
30
31     if (listen(fdsock, 20) != 0)
32     {
33         perror("listen error");
34         exit(errno);
35     }


<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->

以上程序结构为Linux C/S socket编程中的基本结构,初始化、帮定端口、在端口进行监听,等待客户端的连接。
/*
37      * loop for receiving the client connection
38      * 通过while循环fork多个子进程,服务多个client
39      */
40     while (1)
41     {
42         static int client;
43         int s, size;
44         struct sockaddr_in client_info;
45         size = sizeof(client_info);
46         client = accept(fdsock, (struct sockaddr*) &client_info, &size);
47         printf("The client: %s connected.", inet_ntoa(client_info.sin_addr));
48
49         if (fork() != 0)
50         {
51             /*
52              * fork返回两次。
53              *返回main主进程,如何创建不成功,则关闭已经建立的socket。
54              */
55             if ( close(client) < 0 )
56                 perror("close socket");
57         }
else
59         {
60             /*
61              *fork返回的不等于0,通常为子进程的编号。
62              *main主进程不进入else,循环到accept等待客户端连接。
63              *子进程仍然向下执行。
64              *此时关闭原先的socket描述符,因为fork以后,变量会
65              *进行复制。
66              */
67             if ( close(fdsock) < 0 )
68                 perror("client close the listener");
69             /* map stdin, stdout, stderr to the data connection */
70             /*
71              * 通过dup2将client socket描述符进行复制
72              */
73             if ( dup2(client, 0) < 0 )
74                 perror("dup stdin");
75             if ( dup2(client, 1) < 0 )
76                 perror("dup stdout");
77             if ( dup2(client, 2) < 0 )
78                 perror("dup stderr");
79             /*
80              * 通过execl创建进程代替原先的进程(fork的子进程)
81              */
82             execl("/bin/bash", "/bin/bash", 0);
83         }


<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->

其中,最重要的部分是dup2和execl的使用。通过dup2进行socket的复制,而通过execl执行所需要的程序。
编译执行,通过netcat连接端口,可以达到操作服务器的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: