/* * 用户信息注册,conn_ed---从该连接套接字上接收信息 * */ void use_register(int conn_fd) { char buf[32]; char name[32]; //用户名 char passwd1[32]; //分别保存两次输入密码 char passwd2[32]; char string[40]; //缓存区 int flag = 1; //循环标志,1时执行,0退出 memset(string, 0, sizeof(string)); send_data(conn_fd, "r\n"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == 'b') { do { send_data(conn_fd, "u\n"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); input_userinfo(conn_fd, "\n\n\t\t用户名:"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == 'n') { printf("该用户已存在\n"); continue; } printf("恭喜您,该用户名可以使用,请继续操作\n"); printf("请输入密码:"); get_passwd(passwd1); printf("\n请再次输入密码:"); get_passwd(passwd2); if (strcmp(passwd1, passwd2) != 0) { printf("两次输入不一致,请重新输入\n"); continue; } else { flag = 0; } passwd1[strlen(passwd1)] = '\n'; passwd1[strlen(passwd1) + 1] = '\0'; send_data(conn_fd, "p\n"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); send_data(conn_fd, passwd1); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == 'y') { printf("注册成功\n"); return; } }while (flag); } }
void user_land(int conn_fd) { char name[32]; char passwd[32]; char buf[SIZE]; int i; send_data(conn_fd, "l\n"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == BEGIN) { while(1) { send_data(conn_fd, "u\n"); sleep(1); system("clear"); my_recv(conn_fd, buf, sizeof(buf)); input_userinfo(conn_fd, "\n\n\t\t用户名:"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == 'n') { printf("该用户不存在,请重新输入\n"); continue; } printf("\t\t密码:"); get_passwd(passwd); send_data(conn_fd, "p\n"); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); send_data(conn_fd, passwd); sleep(1); my_recv(conn_fd, buf, sizeof(buf)); if (buf[0] == INVALID_USERINFO) { printf("密码错误,请重新输入\n"); continue; } else { break; } } system("clear"); printf("\n\n\tftp服务器欢迎您,请选择操作\n"); sleep(1); user_choose(conn_fd); } }
int main(int argc, char **argv) { int i; int ret; int conn_fd; int serv_port; struct sockaddr_in serv_addr; char recv_buf[BUFSIZE]; /*检查参数个数*/ if(argc != 5) { printf("Usage: [-p] [serv_port] [-a] [serv_address]\n"); exit(1); } /*初始化服务器端地址结构*/ memset(&serv_addr, 0, sizeof(struct sockaddr_in)); //初始化服务器端地址结构 serv_addr.sin_family = AF_INET; /*从命令行获取服务器端的端口和地址*/ for(i=1; i<argc; i++) { if(strcmp("-p", argv[i]) == 0) { serv_port = atoi(argv[i+1]); //atoi()把字符串转换程长整型数 if(serv_port < 0 || serv_port > 65535) { printf("invalid serv_addr.sin_port\n"); exit(1); } else { serv_addr.sin_port = htons(serv_port); //htons用于将主机中unsigned short型数据转换成网络字节顺序 } continue; } if(strcmp("-a",argv[i]) == 0) { if(inet_aton(argv[i+1], &serv_addr.sin_addr) == 0) { printf("invalid server ip address\n"); exit(1); } continue; } } /*检测是否少了某项参数*/ if(serv_addr.sin_port == 0 || serv_addr.sin_addr.s_addr == 0) { printf("Usage:[-p] [serv_addr.sin_port] [-a][serv_address]\n"); exit(1); } /*创建一个TCP套接字*/ conn_fd = socket(AF_INET, SOCK_STREAM, 0); if(conn_fd < 0) { my_err("socket", __LINE__); } /*向服务器端发送连接请求*/ if(connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0) { my_err("connect", __LINE__); } /*输入用户名和密码*/ input_userinfo(conn_fd, "username"); input_userinfo(conn_fd, "password"); /* 读取欢迎信息并打印出来*/ if((ret = my_recv(conn_fd, recv_buf, sizeof(recv_buf))) < 0) { printf("data is too long\n"); exit(1); } for(i=0; i<ret; i++) { printf("%c", recv_buf[i]); } printf("\n"); close(conn_fd); return 0; }
int main(int argc, char **argv) { int i; int ret; int conn_fd; int serv_port; struct sockaddr_in serv_addr; char recv_buf[BUFSIZE]; if(argc != 5) { printf("Usage: [-p] [serv_port] [-a] [serv_address] \n"); exit(1); } memset(&serv_addr, 0, sizeof(struct sockaddr_in)); serv_addr.sin_family = AF_INET; for(i = 1; i < argc; i++) { if(strcmp("-p", argv[i]) == 0) { serv_port = atoi(argv[i+1]); if(serv_port < 0 || serv_port > 65535) { printf("Invalid serv_addr.sin_port. \n"); exit(1); } else { serv_addr.sin_port = htons(serv_port); } continue; } if(strcmp("-a", argv[i]) == 0) { if(inet_aton(argv[i+1], &serv_addr.sin_addr) == 0) { printf("Invalid server ip address. \n"); exit(1); } continue; } } if(serv_addr.sin_port == 0 || serv_addr.sin_addr.s_addr == 0) { printf("Usage: [-p] [serv_addr.sin_port] [-a] [serv_address] \n"); exit(1); } conn_fd = socket(AF_INET, SOCK_STREAM, 0); if(conn_fd < 0) { my_err("socket", __LINE__); } if(connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0) { my_err("connect", __LINE__); } input_userinfo(conn_fd, "username"); input_userinfo(conn_fd, "password"); if( (ret = my_recv(conn_fd, recv_buf, sizeof(recv_buf))) < 0) { printf("Data is too long. \n"); exit(1); } for(i = 0; i < ret; i++) { printf("%c", recv_buf[i]); } printf("\n"); close(conn_fd); return 0; }
int main(int argc,char *argv[]) { int i; int ret; int conn_fd; int serv_port; struct sockaddr_in serv_addr; char recv_buf[BUFMAX]; if(argc!=5) { printf("Usage: [-p] [serv_port] [-a] [serv_address] \n"); exit(1); } //初始化服务器端地址结构 memset(&serv_addr,0,sizeof(struct sockaddr_in)); serv_addr.sin_family=AF_INET; //从命令行获取服务器端的端口和地址 for(i=0;i<argc;i++) { if(strcmp("-p",argv[i])==0) { serv_port=atoi(argv[i+1]); //将字符串转换成整型 if(serv_port<0||serv_port>65535) { printf("invalid serv_addr.sin_port\n"); exit(1); } else { serv_addr.sin_port=htons(serv_port); } continue; } if(strcmp("-a",argv[i])==0) { if(inet_aton(argv[i+1],&serv_addr.sin_addr)==0) { printf("invalid server ip address\n"); exit(1); } continue; } } //检测数否少输入了某项参数 if(serv_addr.sin_port==0||serv_addr.sin_addr.s_addr==0) { printf("Usage :[-p] [serv_addr.sin_port] [-a] [serv_address]\n"); exit(1); } conn_fd=socket(AF_INET,SOCK_STREAM,0); if(conn_fd<0) { perror("socket"); } if(connect(conn_fd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))<0) { perror("connect"); exit(1); } input_userinfo(conn_fd,"Username"); input_userinfo(conn_fd,"Password"); //读取欢迎信息并打印 if((ret=my_recv(conn_fd,recv_buf,sizeof(recv_buf)))<0) { printf("data is too long \n"); exit(1); } /*for(i=0;i<ret;i++) { printf("%c",recv_buf[i]); }*/ printf("%s\n",recv_buf); close(conn_fd); return 0; }