1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #define LOGIN_FAILURE 0 #define LOGIN_SUCCESS 1 void printf_client_info(struct sockaddr_in *addr,char *buf) { printf("===============================\n"); printf("user IP : %s\n",inet_ntoa(addr->sin_addr)); printf("user port : %d\n", ntohs(addr->sin_port)); printf("user data : %s\n",buf); } int init_socket(const char *ip,const char *port) { int sockfd; struct sockaddr_in my_addr; memset(&my_addr,0,sizeof(my_addr)); socklen_t addrlen=sizeof(my_addr); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(atoi(port)); my_addr.sin_addr.s_addr=inet_addr(ip); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { perror("[ERROR] socket failed!"); return -1; } if(bind(sockfd,(struct sockaddr *)&my_addr,addrlen)<0) { perror("[ERROR] failed to bind "); return -1; } return sockfd; } int login_manage(const char *ip,const char *port) { int sockfd; int sockfd_new; char login_check; char login_message[64]={0}; struct sockaddr_in client_addr; socklen_t addrlen=sizeof(client_addr); sockfd=init_socket(ip,port); while(1) { login_check=LOGIN_FAILURE; memset(login_message,0,sizeof(login_message)); recvfrom(sockfd,login_message,sizeof(login_message),0,(struct sockaddr *)&client_addr,&addrlen); printf("[DEBUG:] recved login_message\n"); if(strcmp(login_message,"root")==0) { login_check=LOGIN_SUCCESS; memset(login_message,0,sizeof(login_message)); pid_t fpid=fork(); if(fpid==-1) { perror("[ERROR] fork() failed!"); return -1; }else if(fpid==0) { close(sockfd); sockfd_new=init_socket(ip,"0"); sendto(sockfd,&login_check,sizeof(login_check),0,(struct sockaddr *)&client_addr,addrlen); break; }
}else{ sendto(sockfd,&login_check,sizeof(login_check),0,(struct sockaddr *)&client_addr,addrlen); }
} return sockfd_new; } void recv_data(int sockfd_new) { int n = 0; char buf[1024] = {0};
struct sockaddr_in client_addr; socklen_t len = sizeof(client_addr); printf("[DEBUG]: enter recv_data:\n"); while(1) { memset(buf,0,sizeof(buf)); n = recvfrom(sockfd_new,buf,sizeof(buf),0,(struct sockaddr *)&client_addr,&len); if(n < 0) { perror("Fail to sendto"); exit(EXIT_FAILURE); } printf("[DEBUG] now is up on printf_client_info()...\n");
printf_client_info(&client_addr,buf);
if(strncmp(buf,"quit",4) == 0) break; }
close(sockfd_new); exit(EXIT_SUCCESS); return ;
} int main(int argc,const char *argv[]) { int sockfd; unsigned char login_flag; if(argc < 3) { fprintf(stderr,"Usage : %s ip port!\n",argv[0]); exit(EXIT_FAILURE); } sockfd = login_manage(argv[1],argv[2]); printf("child process recv_data:\n"); recv_data(sockfd);
return 0; }
|