📄 client.c
字号:
/* * ===================================================================================== * * Filename: server.c * * Description: This program is used to send command * * Version: 1.0 * Created: 04/04/2009 01:01:02 AM * Revision: none * Compiler: gcc * * Author: Shan Yafeng * Company: * * ===================================================================================== */#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <errno.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#define PORT 20001#define MAXSIZE 255#define back_log 5#define cmd_size 15#define path_size 30int main(){ int socket_fd,newfd; struct sockaddr_in host,client; int len=sizeof(struct sockaddr); char cmd[cmd_size],path[path_size],buf[MAXSIZE]; char cmd_total[MAXSIZE]; FILE *popen_fd; int size; if ((socket_fd=socket(AF_INET,SOCK_STREAM,0))<0){ perror("Socket"); exit(1); } bzero(&host,sizeof(struct sockaddr)); bzero(&client,sizeof(struct sockaddr)); host.sin_family=AF_INET; host.sin_port=htons(PORT); host.sin_addr.s_addr=INADDR_ANY; if (bind(socket_fd,(struct sockaddr *)&host,sizeof(struct sockaddr))<0){ perror("Bind"); exit(1); } if (listen(socket_fd,back_log)<0){ perror("Listen"); exit(1); } printf("Listening....\n"); while(1){ while((newfd=accept(socket_fd,(struct sockaddr *)&client,&len))<0){} printf("Connection Finished\n"); printf("Connect from:%s:%d\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port)); memset(cmd,0,cmd_size); memset(path,0,path_size);/* if (fscanf(newfd,"%s%s",cmd,path)<0){ perror("Fscanf"); exit(1); }*/// cmd[0]='l';cmd[1]='s';// path[0]='/';path[1]='p';path[2]='r';path[3]='o';path[4]='c';// memset(cmd_total,0,MAXSIZE);// if (sprintf(cmd_total,"%s %s",cmd,path)<0){// perror("sprintf");// exit(1);// } memset(cmd_total,0,MAXSIZE); while(recv(newfd,cmd_total,MAXSIZE-1,0)<0){}; printf("%s\n",cmd_total); if((popen_fd=popen(cmd_total,"r"))<0){ perror("Popen"); exit(1); } memset(buf,0,MAXSIZE);// size=fread(buf,MAXSIZE-1,1,popen_fd);// printf("%s\n",buf); while ((size=fread(buf,MAXSIZE-1,1,popen_fd))!=0){ if (size>0){ send(newfd,buf,strlen(buf),MSG_DONTWAIT); printf("%s",buf); memset(buf,0,MAXSIZE); } } if(strlen(buf)>1){ buf[strlen(buf)-1]=0; send(newfd,buf,strlen(buf),MSG_DONTWAIT); printf("%s",buf); } fflush; printf("\nCmd have done\n");// close(newfd); } sleep(8); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -