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文件描述符进行交互的。
程序主要结构为:
<!--
/* 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编程中的基本结构,初始化、帮定端口、在端口进行监听,等待客户端的连接。
<!--
/* 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连接端口,可以达到操作服务器的效果。
/* 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连接端口,可以达到操作服务器的效果。
相关文章推荐
- Linux socket编程-最简单的服务器和客户端程序
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Linux网络编程之UDP Socket程序示例
- Linux下socket 编程模拟C/S服务
- Linux下socket编程示范(一个服务…
- C/C++ socket编程教程之二:一个简单的Linux下的socket程序
- TCP/TP编程 - 一个简单的Linux下C写的socket服务器客户端程序
- linux网络编程之socket(九):使用select函数改进客户端/服务器端程序
- linux基础编程 套接字socket 完整的服务器端多线程socket程序【转】
- Linux Socket编程实例(一个Hello World程序)
- linux基础编程 套接字socket 完整的服务器端多线程socket程序
- Linux socket编程-最简单的服务器和客户端程序
- linux网络编程之socket(二):C/S程序的一般流程和基本socket函数
- Linux Socket编程实例(一个Hello World程序)
- Linux 网络程序编程初步 Socket
- linux网络编程之socket(十四):基于UDP协议的网络程序
- Linux Socket编程实例(一个Hello World程序)
- linux网络编程之socket(三):最简单的回射客户/服务器程序
- linux网络编程之socket(十四):基于UDP协议的网络程序
- linux网络编程之socket(三):最简单的回射客户/服务器程序、time_wait 状态