- #include <stdio.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include "command.h"
- #include "config.h"
- #define BUFFSIZE 50
- #define RESSIZE 512
- #define MAXPENDING 5
- #define FILE_NAME_MAX_SIZE 512
- void Die(char *mess) { perror(mess); exit(1); }
- void initsql()
- {
- }
- void HandleClient(int new_server_socket)
- {
- int fd;//filehandle
- int fcount;//fileread
- FILE *fp;
- int received = -1;
- if ( new_server_socket < 0)
- {
- printf("Server Accept Failed!/n");
- return;
- }
- char buffer[BUFFSIZE];
- char Response[RESSIZE];
- bzero(buffer, BUFFSIZE);
- bzero(Response, RESSIZE);
- //发送和接受缓冲
- while (received = recv(new_server_socket,buffer,BUFFSIZE,0)>0)
- {
- char *cmd;
- char *par;
- char *args;
- cmd=strtok(buffer,splitcmd);
- par=strtok(NULL,splitcmd);
- //命令用空格分开
- printf("cmd:%s/n",cmd);
- printf("par:%s/n",par);
- //显示命令
- if (strcmp(cmd,getcons)==0)//请求连接串
- {
- printf("client reqconnection string/n");
- strcpy(Response,connstr);
- if((send(new_server_socket,Response,RESSIZE,0))!=RESSIZE)
- {
- printf("Response Faild %s/n", connstr);
- break;
- }
- }
- else if(strcmp(cmd,getfile)==0)//请求文件
- {
- fd = open(par,O_RDWR);
- if(fd < 0) Die("can't open the file:%s/n");
- while(fcount = read(fd,Response,RESSIZE))
- {
- write(new_server_socket,Response,fcount);
- }
- //fclose(fd);
- }
- else if (strcmp(cmd,cmdexit)==0)//请求关闭
- {
- close(new_server_socket);
- }
- else if (strcmp(cmd,cmdexec)==0)//执行命令
- {
- system(par);
- /*fp=popen(par,"r");
- while(fcount = read(fp,Response,RESSIZE))
- {
- write(new_server_socket,Response,fcount);
- }
- pclose(fp);
- */
- //args=strtok(par,splitcmd) ;
- //if (execlp(args,strtok(NULL,splitcmd),(char *)0)<0)
- //{
- // send(new_server_socket,par,RESSIZE,0);
- //}
- //else
- //{
- // printf("Client Exec:%s/n",par);
- //}
- }
- else
- {
- printf("nop/n");
- send(new_server_socket,cmd,RESSIZE,0);
- }
- bzero(buffer, BUFFSIZE);
- bzero(cmd, strlen(cmd));
- bzero(par,strlen(par));
- bzero(Response,RESSIZE);
- }
- }
- int main(int argc, char *argv[])
- {
- int sock_send,sock_listen; //新来的sock
- struct sockaddr_in opluserver,localend; //远程opluserver,sock_addr
- char rip;
- int rport;
- char buffer[BUFFSIZE];
- unsigned int echolen;
- int received = 0;
- if (argc != 2)
- {
- fprintf(stderr, "USAGE: xoplus <port>/n");
- exit(1);
- }
- if ((sock_listen= socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
- {
- Die("Can't open listen port");
- exit(1);
- }
- //创建侦听socket
- memset(&localend,0,sizeof(localend));
- localend.sin_family = AF_INET;
- localend.sin_addr.s_addr = htonl(INADDR_ANY);
- localend.sin_port = htons(atoi(argv[1]));
- printf("port%s /n",argv[1]);
- //准备本地socket
- if ((bind(sock_listen,(struct sockaddr *)&localend,sizeof(localend)))<0)
- {
- Die("Bind Port to localend faild/n");
- }
- //绑定端口号
- if (listen(sock_listen,MAXPENDING)<0)
- {
- Die("Can't Listen on port");
- }
- //启动侦听
- while (1)
- {
- unsigned int clientlen = sizeof(opluserver);
- if ((sock_send = accept(sock_listen, (struct sockaddr *) &opluserver, &clientlen)) < 0)
- {
- Die("Failed to accept client connection");
- }
- fprintf(stdout, "Client connected: %s/n", inet_ntoa(opluserver.sin_addr));
- HandleClient(sock_send);
- }
- }
AIX shell
最新推荐文章于 2021-09-29 15:02:35 发布