Recursive, rlsd, rlsrlsd
The server-side program generally has the following processes: bind, listen, and accept. Then there are various operations after the client and the server are connected.
In contrast, the client program is relatively simple. You only need to obtain the sock_id first and connect it with the corresponding sock. The others are as follows.
Server code:
1 #include<stdio.h> 2 #include<unistd.h> 3 #include<sys/types.h> 4 #include<sys/socket.h> 5 #include<netinet/in.h> 6 #include<netdb.h> 7 #include<time.h> 8 #include<string.h> 9 #define PORTNUM 1500010 #define HOSTLEN 25611 #define oops(msg) {printf("error:%s\n",msg);return 1;}12 int main(int ac,char * av[]){13 struct sockaddr_in saddr;14 struct hostent * hp;15 char hostname[HOSTLEN];16 int sock_id,sock_fd;17 FILE * sock_fpi,* sock_fpo;18 FILE * pipe_fp;19 char dirname[BUFSIZ];20 char command[BUFSIZ];21 int dirlen,c;22 sock_id=socket(PF_INET,SOCK_STREAM,0);//set socket23 if(sock_id==-1)24 oops("socket");25 bzero((void *)&saddr,sizeof(saddr));//empty saddr26 gethostname(hostname,HOSTLEN);27 printf("hostname:%s\n",hostname);28 hp=gethostbyname(hostname);29 bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);30 saddr.sin_port=htons(PORTNUM);31 saddr.sin_family=AF_INET;32 if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr))!=0)//bind address33 oops("bind");34 if(listen(sock_id,1)!=0)//listen35 oops("listen");36 while(1){37 sock_fd=accept(sock_id,NULL,NULL);//start accept38 if(sock_fd==-1)39 oops("accept");40 if((sock_fpi=fdopen(sock_fd,"r"))==NULL)41 oops("fdopen reading");42 if(fgets(dirname,BUFSIZ-5,sock_fpi)==NULL)43 oops("reading dirname");44 sanitize(dirname);45 if((sock_fpo=fdopen(sock_fd,"w"))==NULL)46 oops("fdopen writing");47 sprintf(command,"ls %s",dirname);48 if((pipe_fp=popen(command,"r"))==NULL)49 oops("popen");50 while((c=getc(pipe_fp))!=EOF)51 putc(c,sock_fpo);52 pclose(pipe_fp);53 pclose(sock_fpo);54 pclose(sock_fpi);55 }56 }57 sanitize(char * str){58 char * src,* dest;59 for(src=dest=str;*src;src++)60 if(*src=='/'||isalnum(*str))61 *dest++=*src;62 *dest='\0';63 }
Client code:
1 #include<stdio.h> 2 #include<linux/types.h> 3 #include<sys/socket.h> 4 #include<netinet/in.h> 5 #include<netdb.h> 6 #define oops(msg) {printf("error:%s\n",msg);return 1;} 7 #define PORTNUM 15000 8 main(int ac,char * av[]){ 9 struct sockaddr_in servadd;10 struct hostent * hp;11 int sock_id,sock_fd;12 char message[BUFSIZ];13 char buffer[BUFSIZ];14 int messlen,n_read;15 sock_id=socket(PF_INET,SOCK_STREAM,0);//attention:PF_INT NOT AF_INT16 if(sock_id==-1)17 oops("socket");18 bzero(&servadd,sizeof(servadd));19 hp=gethostbyname(av[1]);20 if(hp==NULL)21 oops(av[1]);22 bcopy(hp->h_addr,(struct sockaddr *)&servadd.sin_addr,hp->h_length);23 servadd.sin_port=htons(PORTNUM);24 servadd.sin_family=AF_INET;25 if(connect(sock_id,(struct sockaddr *)&servadd,sizeof(servadd))!=0)//connect26 oops("connect");27 if(write(sock_id,av[2],strlen(av[2]))==-1)28 oops("write");29 if(write(sock_id,"\n",1)==-1)30 oops("write");31 while((n_read=read(sock_id,buffer,BUFSIZ))>0)32 if(write(1,buffer,n_read)==-1)33 oops("write");34 close(sock_id);35 }
The call is as follows:
Server startup:./rlsd &
Client Access:./rls jsonzhang-Vostro-23-3340 ~ /Desktop/workplace/