linux下telnet客户端通过守护进程向服务器端发送心跳包怎么实现?

矮油 发布于 2012/02/13 17:33
阅读 1K+
收藏 0

由于学的不是很好,大妈写的不是很好,大家将就看看吧- -!

我的客户端代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <curses.h>

#define PORT 1111
#define BUF_SIZE 2048

typedef struct user
{
 char name[20];
 char pass[10];
 struct user *next;
}USR;

void regist(int ssockfd)
{
 int sockfd = ssockfd;
 
 char name[20];
 char pass[10];
 char pass1[10];
 char info[128];
 char g;
 int ret;

 do{
  memset(info, 0, sizeof(info));
  memset(name, 0, sizeof(name));
  memset(pass, 0, sizeof(pass));
  memset(pass1, 0, sizeof(pass1));
  printf("请输入用户名(#键退出注册):");
  scanf("%s", name);
  if(!strcmp(name, "#"))
  {
   write(sockfd, name, strlen(name));
   break;
  }

  write(sockfd, name, strlen(name));
  printf("正在检验用户名是否已被使用...");
  read(sockfd, &g, sizeof(char));//检查是否读取到数据,待会写上!!
  if(g == 'o')
  {
   printf("该用户名可用!\n");
   do{
    printf("请输入新密码:");
    scanf("%s", pass);
    getchar();
    printf("请再输入一遍密码:");
    scanf("%s", pass1);
    getchar();

    if(!strcmp(pass,pass1))
    {
     printf("两次密码一致\n");
     break;
    }
    printf("两次密码不一致,请重新输入!\n");
   }while(1);
   sprintf(info, "%2d%s%s", strlen(name), name, pass);
   printf("info :%s\n", info);
   write(sockfd, info, strlen(info));
   ret = read(sockfd, &g, sizeof(char));
   if(ret == 0)
   {
    printf("server closed!\n");
    break;
   }

   else if(ret < 0)
   {
    perror("read");
    break;
   }

   else
   {
    printf(" g = %c\n", g);
    
     printf("注册成功!\n");
     printf("您的用户名为 %s ,请记住用户名和密码!\n", name);
     break;
   }

  }

  else
  { 
   printf("该用户名已被使用,请输入其他可用用户名\n");
  }
 }while(1); 
}

void cmd(int ssockfd)
{
 char command[BUF_SIZE];
 char getbuf[BUF_SIZE];
 int sockfd = ssockfd;
 int ret;

 do{
  printf("please enter command:");
  fflush(stdout);
  scanf("%s", command);
  write(sockfd, command, strlen(command));

  ret = read(sockfd, getbuf, sizeof(getbuf));
  if(ret == 0)
  {
   printf("server closed!\n");
   break;
  }

  else if(ret < 0)
  {
   perror("read");
   break;
  }

  else
  {
   getbuf[ret] = '\0';
   printf("getbuf : %s\n", getbuf);
   if(!strcmp(getbuf, "exit"))
   {
    break;
   }
  };

 }while(1);
}

void msg(int ssockfd)
{
 char message[BUF_SIZE];
 char getbuf[BUF_SIZE];
 int sockfd = ssockfd;
 int ret;

 do{
  memset(message, 0, BUF_SIZE);
  memset(getbuf, 0, BUF_SIZE);
  printf("请输入要发送的信息:\n");
  fflush(stdin);
  scanf("%s", message);
  getchar();
  write(sockfd, message, strlen(message));
  ret = read(sockfd, getbuf, sizeof(getbuf)-1);
  if(ret == 0)
  {
   printf("server closed!\n");
   break;
  }

  else if(ret < 0)
  {
   perror("read");
   break;
  }

  else
  {
   getbuf[ret] = '\0';
   printf("%s\n", getbuf);
  }
 }while(strcmp("exit", getbuf));
}

void modify(int ssockfd, USR *client)
{
 int sockfd = ssockfd;
 char oldpass[10];
 char pass[10];
 char info[128];
 char pass1[10];
 int flag = 0;
 char g;
 int ret;

 while(1)
 {
  memset(info, 0, sizeof(info));
  memset(pass, 0, sizeof(pass));
  memset(pass1, 0, sizeof(pass1));
  memset(oldpass, 0, sizeof(oldpass));
  printf("请输入旧密码:");
  fflush(stdout);
  scanf("%s", oldpass);
  getchar();
  printf("正在核对密码...");
  if(!strcmp(oldpass, client->pass))
  {
   printf("密码正确!\n");
   while(1)
   {
    printf("请输入新密码:");
    fflush(stdout);
    scanf("%s", pass);
    printf("请再输入一遍密码:");
    fflush(stdout);
    scanf("%s", pass1);
    if(!strcmp(pass, pass1))
    {
     sprintf(info, "%2d%s%s", strlen(client->name), client->name, pass);
     write(sockfd, info, strlen(info));
     ret = read(sockfd, &g, sizeof(char));
     if(ret == 0)
     {
      printf("server closed!\n");
      break;
     }

     else if(ret < 0)
     {
      perror("read");
      break;
     }

     else
     {
      if(g == 'o')
      {
       printf("密码修改成功!\n");
       flag = 1;
       break;
      }

      else
      {
       printf("修改失败!\n");
       break;
      }
     }
    }

    else
    {
     printf("两次密码不一致,请重新输入!\n");
    }
   }
  }
  else
  {
   printf("密码错误,请重新输入!\n");
  }

  if(flag == 1)
  {
   break;
  }

 }
}

void user(int ssockfd, USR *client)
{//用户主目录
 char i;
 int flag = 0;
 int ret;
 char g;
 do{
  printf("欢迎 %s 登录telnet!\n", client->name);
  printf("1.命令\n");
  printf("2.信息\n");
  printf("3.修改密码\n");
  printf("0.退出\n");
  printf("请选择:");
  fflush(stdin);
  scanf("%c", &i);
  if((i >= '0') && (i <= '3'))
  {
   write(ssockfd, &i, 4);

   ret = read(ssockfd, &g, 4);
   if(ret == 0)
   {
    printf("server closed!\n");
    break;
   }

   else if(ret < 0)
   {
    perror("read");
    break;
   }

   else
   {
    printf("%c\n", g);
    switch(g)
    {
     case 'c': cmd(ssockfd);
         break;

     case 'm': msg(ssockfd);
         break;

     case 'n': modify(ssockfd, client);
         break;

     case 'q': flag = 1;
         break;
    }
   }
  }
  else
  {
   printf("只能输入0或1或2或3,请重新输入!\n");
  }
 }while(flag != 1);
}

void login(int ssockfd)
{
 char name[20];
 char pass[10];
 char g;//服务器返回的验证信息
 int i = 3;//限制登录次数
 int ret = 0;
 int sockfd = ssockfd;//接收套接字
 USR *client = NULL;
 char info[128];

 do
 {
  printf("请输入用户名(#键退出):");
  scanf("%s", name);
  getchar();
  if(!strcmp("#", name))
  {
   sprintf(info, "%2d%s%s", strlen(name), name, "pass");
   write(sockfd, info, strlen(info));
   break;
  }
  printf("请输入密码:");
  scanf("%s", pass);

  sprintf(info, "%2d%s%s", strlen(name), name, pass);
  printf("info : %s\n", info);
  
  write(sockfd, info, strlen(info));

  if((ret = read(sockfd, &g, 1)) < 0)
  {
   printf("connect closed!\n");
   break;
  }

  else if(ret < 0)
  {
   perror("read");
   break;
  }

  else
  {
   
   printf("revieve message : %c\n", g);
   if(g == 'o')
   {
    client = (USR *)malloc(sizeof(USR));
    strcpy(client->name, name);
    strcpy(client->pass, pass);
    client->next = NULL;

    printf("登录成功!\n");
    user(sockfd, client);
    free(client);
    break;
   }
   else
   {
    i--;
    printf("用户名或密码错误,你还有%d次机会输入!\n", i);
    if(i == 0)
    {
     printf("您的机会以用完,客户端即将退出!\n");
     close(sockfd);
     
     exit(0);
    }
   }
  }
 }while(1);
}

int main(int argc, char **argv)
{
 int sockfd, ret = 0;
 struct sockaddr_in clieaddr;
 char ip[16];
 char i;
 char g;

 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)//创建套接字
 {
  perror("socket");
  exit(0);
 }
 printf("sockfet success!\n");

 clieaddr.sin_family = AF_INET;
 clieaddr.sin_port = htons((short)PORT);

 if(inet_pton(AF_INET, argv[1], &clieaddr.sin_addr.s_addr) < 0)//转换ip地址格式
 {
  perror("inet_pton");
  exit(0);
 }
 printf("IP : %s\n", inet_ntop(AF_INET, &clieaddr.sin_addr.s_addr, ip, sizeof(ip)));

 if(connect(sockfd, (struct sockaddr*)&clieaddr, sizeof(clieaddr)) < 0)//
 {
  perror("connect");
  exit(0);
 }
 printf("connect success!\n");

 do{
  printf("telnet 客户端\n");
  printf("1.用户登录\n");
  printf("2.用户注册\n");
  printf("0.退出系统\n");
  printf("请选择:");
  fflush(stdout);
  scanf("%c", &i);
  printf("i = %c\n", i);
  if((i >= '0') && (i <= '2'))
  {
   write(sockfd, &i, 4);
   ret = read(sockfd, &g, sizeof(char));
   if(ret == 0)
   {
    printf("server closed!\n");
    break;
   }

   else if(ret < 0)
   {
    perror("read");
    break;
   }

   else
   {
    printf("revieve : %c\n", g);
    switch(g)
    {
     case 'l': login(sockfd);
         break;

     case 'r': regist(sockfd);
         break;

     case 'q': printf("退出系统\n");
         break;

    } 
   }
  }
  
  else
  {
   printf("只能输入0或1或2,请重新输入!\n");
  }
 }while(g != 'q');
 close(sockfd);
 return 0;
}
加载中
返回顶部
顶部