#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 3333
//用户名和密码
char *name="twain";
char *passwd="123";
#define MAX_LISTEN 10
#define BUF_SIZE 1024
#define USERNAME 0
#define PASSWORD 1
#define COMMAND 2
//登录过程状态标识
char *error_user = "error user name";
char *error_password = "error password";
char *correct_user = "correct user";
char *success_login = "correct password";
char *exit_command = "exit";
char *exit_tip="退出连接!";
/*
* 介绍:服务器端
* 功能:
* 1.接受客户端的登录请求并验证
* 2.接受客户端的命令在本地执行并返回执行结果
*/
int command(int connfd,char*command)
{
/*
* 作用:执行command命令
* connfd: 客户端标识
* command: 命令字符串
*/
FILE *fstream = NULL;
char buff[1024];
memset(buff, 0, sizeof(buff));
if ( strcmp(exit_command, command) == 0 ) { //退出
if ( send(connfd, exit_command,strlen(exit_command), 0) < 0 )
printf( "send failed" );
printf("客户端已断开连接!\n");
return 0;
}
if(NULL == (fstream = popen(command,"r")))
{
fprintf(stderr,"execute command failed: %s",strerror(errno));
return -1;
}
while(NULL != fgets(buff, sizeof(buff), fstream))
{
//printf("%s",buff);
if ( send(connfd, buff,strlen(buff), 0) < 0 ) //将命令执行结果发送至客户端
printf( "send failed" );
}/*
if(NULL != fgets(buff, sizeof(buff), fstream))
{
//printf("%s",buff);
if ( send(connfd, buff,strlen(buff), 0) < 0 )
printf( "send failed" );
}else{
if ( send(connfd, "..",sizeof("buff"), 0) < 0 )
printf( "send failed" );
}*/
pclose(fstream);
return 0;
}
int main( int argc, char *argv[] )
{
int sockfd, connfd;
int optval;
int recv_type = USERNA