设为首页 加入收藏

TOP

Telnet模拟系统(Linux c)(四)
2019-01-25 12:08:36 】 浏览:502
Tags:Telnet 模拟 系统 Linux
nbsp; /* 从服务器接受消息 */

    if ( recv(connfd, recv_buf, BUF_SIZE, 0) < 0 )

        printf( "recv failed" );

 

/* 用户名处理 */

    if ( flag == 'u' ) {

        if ( strcmp(recv_buf, error_user) == 0 ) {  //用户名错误

            puts( error_user );

            return FALSE;

        }

    }

/* 密码处理 */

    else if ( flag == 'p' ) {

        if ( strcmp(recv_buf, error_password) == 0 )

          {  puts( error_password );  //密码错误

 return FALSE;

}

    }

/* 命令处理 */

else if( flag == 'c' )

{

if ( strcmp(recv_buf, exit_command) == 0 ) {  //退出

printf("连接已断开\n");          

return FALSE;

     }

puts(recv_buf); //命令结果输出

}

    return TRUE;

}

 

7章 附录

7.1服务器端

 

#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
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c语言----<项目>_小游戏<.. 下一篇C语言实现邻接矩阵创建无向图&图..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目