/* * Peaks at next line in file and counts number of fields. * Can be used to determine number of samples in file. * File is rewound to beginning after. * Returns -1 on failure. */ long impute_count_fields(gzFile fh) { char *line, *cur, *tok; char delim[] = " \t"; long n; /* read a line */ line = util_gzgets_line(fh); if(line == NULL) { my_err("%s:%d: no lines left in file, cannot count fields\n", __FILE__, __LINE__); } cur = line; n = 0; while((tok = strsep(&cur, delim)) != NULL) { n++; } my_free(line); /* rewind to beginning of file */ if(gzseek(fh, 0L, SEEK_SET) != 0) { my_err("%s:%d: could not rewind filehandle", __FILE__, __LINE__); } return n; }
void impute_parse_haplotypes(char *haplotypes, char *cur, long n_samples) { long expect_n, i, val; char *tok; char delim[] = " \t"; expect_n = n_samples * 2; i = 0; while((tok = strsep(&cur, delim)) != NULL) { if(i >= expect_n) { my_err("%s:%d: more haplotype values per row than expected (%ld)." " Current token: '%s'", __FILE__, __LINE__, expect_n, tok); } /* expect values to be either 0 or 1 */ if(strcmp(tok, "0") == 0) { haplotypes[i] = 0; } else if(strcmp(tok, "1") == 0) { haplotypes[i] = 1; } else { my_err("%s:%d: expected haplotype values to be either 0 or 1 but got " "%s", __FILE__, __LINE__, tok); } i++; } if(i != expect_n) { my_err("%s:%d: fewer haplotype values (%ld) per row than expected (%ld)", __FILE__, __LINE__, expect_n); } }
int Make_Listen_Socket( void ) { int sock_fd; struct sockaddr_in server; //Make socket if( ( sock_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ) my_err( "Socket",__LINE__ ); //reuse address int opt = SO_REUSEADDR; if( setsockopt( sock_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) ) < 0 ) my_err( "Setsock Opt",__LINE__ ); //initalization socket bzero( &server,sizeof( server ) ); server.sin_family = AF_INET; server.sin_port = htons( PORT ); server.sin_addr.s_addr = htonl( INADDR_ANY ); //bind socket, address, port if( bind( sock_fd,( struct sockaddr* )&server, sizeof( server ) ) == -1) my_err( "Bind",__LINE__ ); //socket : Closed --> listen if( listen( sock_fd, BACKLOG ) == -1 ) my_err( "Listen",__LINE__ ); return sock_fd; }
/** * Converts an integer offset to a sequence coordinate. Populates * provided SeqCoord data structure (does not create a new one), * with start, end and chromosome. Returns index of chromosome * in chr_tab->chr_array. */ int chr_table_offset_to_coord(ChrTable *chr_tab, unsigned int offset, SeqCoord *c) { int i, found_offset, chr_idx; found_offset = FALSE; chr_idx = 0; for(i = 0; i < chr_tab->n_chr; i++) { if(offset >= chr_tab->offset[i]) { c->start = offset - chr_tab->offset[i] + 1; c->end = c->start; c->strand = STRAND_FWD; c->chr = &chr_tab->chr_array[i]; c->seqname = NULL; found_offset = TRUE; chr_idx = i; } } if(!found_offset) { my_err("%s:%d: invalid offset %d", __FILE__, __LINE__, offset); } if(c->end > c->chr->len) { my_err("%s:%d: invalid offset %d implies position past end of chromosome", __FILE__, __LINE__, offset); } return chr_idx; }
void Open_Html( char Read_Buf[] , char path[]) { int FileD; int ret,len; if( path[ strlen( path ) - 1 ] == '/') strcat( path,"index.html\0" ); // printf( "\n\n-------%s-------\n",path ); if( ( FileD = open( path,O_RDONLY ) ) == -1 ) my_err( "Open File",__LINE__ ); //get the length of the index.html,and keep the point is in the start if( lseek( FileD, 0, SEEK_END ) == -1 ) my_err( "Lseek",__LINE__ ); if( ( len = lseek( FileD, 0, SEEK_CUR ) ) == -1 ) my_err( "Lseek",__LINE__ ); if( ( lseek( FileD, 0, SEEK_SET ) ) == -1 ) my_err( "Lseek",__LINE__ ); if( ( ret = read( FileD, Read_Buf, len ) ) < 0 ) my_err( "Read",__LINE__ ); close( FileD ); }
/*检查ID有效性*/ int user_ID_cmp (char user_ID[IDMAX]) { struct user_data user_data_ptemp ; FILE *fp = NULL ; int user_id_max, user_id ; memset (&user_data_ptemp, 0, sizeof (struct user_data)) ; user_id = atoi (user_ID) ; /**/ char buf[BUFMAX] ; getcwd (buf,sizeof (buf)) ; printf ("\n%s\n", buf) ; /**/ if((fp = fopen ("./user_data.txt", "rb+")) == NULL) { my_err ("fopen", __LINE__) ; return -1 ; } if(fread (&user_data_ptemp, sizeof (struct user_data), 1, fp) <= 0) { my_err ("fread", __LINE__) ; return -1 ; } user_id_max = atoi (user_data_ptemp.user_ID) ; fclose (fp) ; if(user_id < 1000 || user_id >= user_id_max) { return 0 ; } else { return 1 ; } }
int system_init()//初始化连接 { int sock_fd; struct sockaddr_in serv_addr ; if((sock_fd=socket(AF_INET,SOCK_STREAM,0)) == -1)//创建一个TCP套接字 { my_err("socket error\n",__LINE__); } int opt,optlen=sizeof(optlen); if( (setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&opt,optlen))==-1)//设置该套接字可以使之重新绑定端口 my_err(" server setsockopt() error ",__LINE__); //初始化服务器地址结构 bzero(&serv_addr ,sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); inet_pton(AF_INET ,IP ,&serv_addr.sin_addr); //将套接字绑定至端口 if((bind(sock_fd ,(struct sockaddr *)&serv_addr, sizeof(struct sockaddr) )) == -1) { my_err("bind error",__LINE__); } //将套接字转化为监听套接字 if( (listen(sock_fd,QUEUELEN)) == -1) { my_err("listen error",__LINE__); } fprintf(stderr,"等待客户端连接....\n"); return sock_fd ;// 返回新的socket 描述符 }
/*创建侦听套结字的函数*/ int sock_get (int * const sock_fd) { struct sockaddr_in sockaddr ; /*创建套结字*/ if((*sock_fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { my_err ("socket", __LINE__) ; return -1 ; } /*绑定套结字*/ memset (&sockaddr, 0, sizeof (struct sockaddr_in)) ; sockaddr.sin_family = AF_INET ; /*设置地址类型*/ sockaddr.sin_port = htons (9526) ; /*设置短口号*/ inet_aton ("0.0.0.0", &sockaddr.sin_addr) ; /*设置IP地址为本机*/ if(bind (*sock_fd, (struct sockaddr *)&sockaddr, sizeof (struct sockaddr_in)) < 0) { my_err ("bind", __LINE__) ; return -1 ; } /*将主动套结字转换为侦听套结字,最大请求连接队列长度为 LISTEN_MAX*/ if(listen (*sock_fd, LISTEN_MAX) < 0) { my_err ("listen", __LINE__) ; return -1 ; } return 0 ; }
void connect_to_client(int sock_fd ) //等待客户端连接 { int newfd; struct sockaddr_in wait_addr;//等待连接的客户端 socklen_t len = sizeof(struct sockaddr_in); int i = 0; while(1) { if((newfd = accept(sock_fd ,(struct sockaddr *)&wait_addr ,&len)) == -1) {//newfd储存接收客户端连接请求所产生的套接字 my_err("server accept() error",__LINE__); } printf(" %s 请求连接。\n",inet_ntoa(wait_addr.sin_addr));//网络字节顺序的二进制IP地址 for(i=0;i < QUEUELEN;i++) { if(clients[i].sock_fd == 0) { clients[i].sock_fd = newfd;//将接收客户端的套接字放入专门存储客户端套接字的结构体之中 printf("目前连接客户端对应的套接字 : %d \n", clients[i].sock_fd);//打印目前连接客户端的套接字 break; } } if((pthread_create(&clients[i].tid ,NULL ,( void* )pthreads ,&clients[i].sock_fd)) == -1)//创建线程,不同的线程id对应不同的 my_err("pthread_create() ",__LINE__); } close(sock_fd);//关闭套接字 close(newfd);//关闭连接的套接字 }
int main() { int fd; char buf[32]; if((fd = open("temp ",O_RDWR |O_CREAT |O_TRUNC ,S_IRWXU )) <0 ) { my_err("open ",__LINE__ ); } if(unlink ("temp") <0) { my_err("unlink ",__LINE__); } printf("file unlinked \n"); if(write (fd ,"temp ",5) <0) { my_err("write",__LINE__); } if((lseek(fd ,0 ,SEEK_SET ))==-1) { my_err("lseek ",__LINE__); } if(read (fd ,buf ,5) <0 ) { my_err("read ",__LINE__); } printf("%s \n",buf); return 0; }
SRef<Resampler *> Resampler_Registry::create( uint32_t inputFreq, uint32_t outputFreq, uint32_t duration, uint32_t nChannels ) { SRef<SPlugin *> plugin; plugin = find_plugin("float_resampler"); if( !plugin ) plugin = find_plugin("simple_resampler"); if( !plugin ) { my_err("Can't create resampler"); return NULL; } SRef<Resampler_Plugin *> resampler = dynamic_cast<Resampler_Plugin *>(*plugin); if( !resampler ) { my_err("dynamic_cast<ResamplerPlugin *> failed"); return NULL; } my_dbg << "Creating resampler " << resampler->get_name() << std::endl; return resampler->create_resampler( inputFreq, outputFreq, duration, nChannels ); }
int COMM_MAKE__CONNECT( char **argv ) { int socket_fd; struct sockaddr_in server; struct hostent *he; if( ( socket_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) my_err( "Socket",__LINE__ ); bzero( &server, sizeof( server ) ); if( ( he = gethostbyname ( argv[ 1 ] )) == NULL ) my_err( "Get name",__LINE__ ); server.sin_family = AF_INET; server.sin_port = htons( atoi( argv[ 2 ] ) ); server.sin_addr = *( ( struct in_addr * )he->h_addr ); if( connect( socket_fd, ( struct sockaddr * )&server, sizeof( server ) ) != 0 ) my_err( "Connect",__LINE__ ); printf( "Server %s connected successful!\n",argv[ 1 ] ); return ( socket_fd ); }
/** * get genotype probabilities by parsing GP token from VCF line */ void vcf_parse_gp(VCFInfo *vcf_info, float *geno_probs, char *cur, long gp_idx) { char delim[] = "\t"; char inner_delim[] = ":"; char *tok, *inner_tok, *inner_cur; char gtype[VCF_MAX_FORMAT]; long i, n, n_geno_probs, expect_geno_probs; float prob_homo_ref, prob_het, prob_homo_alt, prob_sum; expect_geno_probs = vcf_info->n_sample * 3; n_geno_probs = 0; while((tok = strsep(&cur, delim)) != NULL) { /* each genotype string is delimited by ':' * each GP portion is delimited by ',' */ util_strncpy(gtype, tok, sizeof(gtype)); i = 0; inner_cur = gtype; while((i <= gp_idx) && (inner_tok = strsep(&inner_cur, inner_delim)) != NULL) { if(i == gp_idx) { n = sscanf(inner_tok, "%g,%g,%g", &prob_homo_ref, &prob_het, &prob_homo_alt); if(n != 3) { if(strcmp(inner_tok, ".") == 0) { /* '.' indicates missing data * set all probabilities to 0.333 */ prob_homo_ref = prob_het = prob_homo_alt = 0.333; } else { my_err("%s:%d: failed to parse genotype probabilities from " "string '%s'", __FILE__, __LINE__, inner_tok); } } /* check that probs sum to 1.0, normalize if they don't */ prob_sum = prob_homo_ref + prob_het + prob_homo_alt; if((prob_sum > 1.001) || (prob_sum < 0.999)) { prob_homo_ref = prob_homo_ref / prob_sum; prob_het = prob_het / prob_sum; prob_homo_alt = prob_homo_alt / prob_sum; } geno_probs[n_geno_probs] = prob_homo_ref; geno_probs[n_geno_probs + 1] = prob_het; geno_probs[n_geno_probs + 2] = prob_homo_alt; n_geno_probs += 3; } i++; } } if(n_geno_probs != expect_geno_probs) { my_err("%s:%d: expected %ld genotype probabilities per line, but got " "%ld", __FILE__, __LINE__, expect_geno_probs, n_geno_probs); } }
//SSL initalization SSL_CTX *COMMUNICATE_SSL_INIT( SSL_CTX *ctx ) { //initalization SSL lib SSL_library_init(); //loading SSL algorithms OpenSSL_add_all_algorithms(); //loading all SSL err information SSL_load_error_strings(); //use SSL V2 and V3 to produce a SSL_CTX------SSL content text ctx = SSL_CTX_new( SSLv23_server_method() ); if ( ctx == NULL ) my_err( "SSL_CTX_new",__LINE__ ); //loading user CA if( SSL_CTX_use_certificate_file( ctx, CERT_FILE, SSL_FILETYPE_PEM ) <=0 ) my_err( "SSL_CTX_use_certificate_file",__LINE__ ); //loading user key if( SSL_CTX_use_PrivateKey_file( ctx, PKEY_FILE, SSL_FILETYPE_PEM ) <=0 ) my_err( "SSL_CTX_use_PrivateKey_file",__LINE__ ); //check user key if( !SSL_CTX_check_private_key( ctx ) ) my_err( "SSL_CTX_check_private_key",__LINE__ ); return ctx; }
/*向新登陆的用户发送在线用户链表的信息,并向所有在线用户发送有新用户登陆的信息*/ int server_login_send (int sock_fd, char user_name[NAMEMAX], char user_ID[IDMAX]) { struct cmd *cmdptemp = NULL ; struct online_list *online_list_ptemp = online_list_PHEAD ; char buf[BUFMAX] ; /*把新用户登陆信息发送给所有在线用户*/ memset (buf, 0, sizeof (buf)) ; server_bag_cat (buf, user_ID, user_name) ; cmdptemp = server_bag ("0", "0", buf, ONLINE) ; if(server_send (cmdptemp) < 0) { my_err ("server_send", __LINE__) ; printf ("server_login_send end\n") ; return -1 ; } /*遍历在线用户列表,把所有在线用户信息发送给新登陆用户*/ while(online_list_ptemp != NULL) { if(strcmp (online_list_ptemp -> user_ID, user_ID) == 0) { online_list_ptemp = online_list_ptemp -> next ; continue ; } memset (buf, 0, sizeof (buf)) ; server_bag_cat (buf, online_list_ptemp -> user_ID, online_list_ptemp -> user_name) ; cmdptemp = server_bag ("0", user_ID, buf, ONLINE) ; if(send (sock_fd, cmdptemp, sizeof (struct cmd), 0) < 0) { my_err ("send", __LINE__) ; return -1 ; } online_list_ptemp = online_list_ptemp -> next ; } printf ("server_login_send right end\n") ; return 0 ; }
int main(void) { pthread_t tid; int i = 0, j = 0, sock, sock_fd, option_value = 1, length = sizeof(option_value); int leng = sizeof(struct sockaddr); struct sockaddr_in sockaddr_ser, sockaddr_cli; if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) my_err("socket", __LINE__); if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&option_value, length) < 0) my_err("setsockopt", __LINE__); memset(&sockaddr_ser, 0, sizeof(sockaddr_ser)); sockaddr_ser.sin_family = AF_INET; sockaddr_ser.sin_port = htons(PORT); sockaddr_ser.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(sock, (struct sockaddr *)&sockaddr_ser, sizeof(struct sockaddr_in)) < 0) my_err("bind", __LINE__); listen(sock, LISTEN); memset(user, 0, sizeof(user)); memset(fd, 0, 128); while(1) { if((sock_fd = accept(sock, (struct sockaddr *)&sockaddr_cli, &leng)) < 0) my_err("accept", __LINE__); printf("accept a new client, ip:%s\n", inet_ntoa(sockaddr_cli.sin_addr)); fd[i++] = sock_fd; strcpy(ip[j++], inet_ntoa(sockaddr_cli.sin_addr)); pthread_create(&tid, NULL, loop, NULL); } }
/*用户注册*/ int server_register (struct cmd *pcmd, int sock_fd) { char *buf ; FILE *fp = NULL; int fd_ID ; struct user_data user_data_ptemp ; char user_name[NAMEMAX], user_passward[PASSWARDMAX] ; int i = 0, n = 0, flag = 0 ; buf = pcmd -> cmd_data ; /*解析出用户名和密码*/ for(i = 0; i < 256 && buf[i] != '\0'; i++) { if(buf[i] == '#') { n = 0 ; flag = 1 ; continue ; } if(flag == 0) { user_name[n++] = buf[i] ; user_name[n] = '\0' ; } else { user_passward[n++] = buf[i] ; user_passward[n] = '\0' ; } } /*分配新用户ID*/ memset (&user_data_ptemp, 0, sizeof (struct user_data)) ; if((fd_ID = user_ID_get()) == -1) { return -1 ; } if(sprintf (user_data_ptemp.user_ID, "%d", fd_ID) <= 0) { my_err ("sprintf", __LINE__) ; return -1 ; } strcpy (user_data_ptemp.user_name, user_name) ; strcpy (user_data_ptemp.user_passward, user_passward) ; /*存储新用户信息,完成注册*/ if((fp = fopen ("./user_data.txt", "ab+")) == NULL) { my_err ("fopen", __LINE__) ; return -1 ; } if(fwrite (&user_data_ptemp, sizeof (struct user_data), 1, fp) == 0) { my_err ("fwrite", __LINE__) ; return -1 ; } fclose (fp) ; struct cmd *ptemp = NULL ; ptemp = server_bag ("0", user_data_ptemp.user_ID, user_data_ptemp.user_ID, REGISTER) ; if(send (sock_fd, ptemp, sizeof (struct cmd), 0) < 0) { my_err ("send", __LINE__) ; return -1 ; } server_user_log (user_data_ptemp.user_ID, "register") ; printf ("i send end\n") ; return 0 ; }
void process_clietn( int connect_fd, struct sockaddr_in client ) { int ret; char recv_buf[ MAXDATASIZE ], send_buf[ MAXDATASIZE ]; char chooseWay,start_platform,end_platform; int status = 0; if( ( ret = recv( connect_fd,recv_buf, MAXDATASIZE , 0 ) ) < 0 ) { my_err( "Recv",__LINE__ ); status = -1; } recv_buf[ret-1] = '\0'; strcpy( chooseWay, recv_buf ); if( ( ret = recv( connect_fd,recv_buf, MAXDATASIZE , 0 ) ) < 0 ) { my_err( "Recv",__LINE__ ); status = -1; } recv_buf[ret-1] = '\0'; strcpy( start_platform, recv_buf ); if( ( ret = recv( connect_fd,recv_buf, MAXDATASIZE , 0 ) ) < 0 ) { my_err( "Recv",__LINE__ ); status = -1; } recv_buf[ret-1] = '\0'; strcpy( end_platform, recv_buf ); //recive 0 bytes.----means recive nothing if( ret == 0 ) { close( connect_fd ); printf( "Client are disconnected.\n" ); return; } //status < 0.----means transport has problem if( status < 0 ) { strcpy( send_buf, "Transmission problems" ); send( connect_fd, send_buf, strlen( send_buf ), 0 ); } }
int main(int argc, char *argv[]) { int fd; int ret; struct flock lock; char read_buf[32]; // 打开或创建文件 if ((fd = open("example_fc_lock", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1) { my_err("open", __LINE__); } if (write(fd, "test lock", 10) != 10) { my_err("write", __LINE__); } // 初始化lock结构 锁整个文件 memset(&lock, 0, sizeof(struct flock)); lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; // 设置读锁 lock.l_type = F_RDLCK; if (lock_test(fd, &lock) == 0) { // 测试可以设置锁 lock.l_type = F_RDLCK; lock_set(fd, &lock); } // 读数据 lseek(fd, 0, SEEK_SET); if ((ret = read(fd, read_buf, 10)) < 0) { my_err("read", __LINE__); } read_buf[ret] = '\0'; printf("%s\n", read_buf); // 按任意键 getchar(); // 设置写锁 lock.l_type = F_WRLCK; if (lock_test(fd, &lock) == 0) { lock.l_type = F_WRLCK; lock_set(fd, &lock); } // 释放锁 lock.l_type = F_UNLCK; lock_set(fd, &lock); close(fd); return 0; }
int check_passwd(const char *name, const char *passwd) { char pathname[32] = {"/home/qiong/userinfo/"}; char tmp[40]; int len; int fd; int ret; if (name == NULL || passwd == NULL) { sys_log("用户名或密码为空"); return -2; } strcat(pathname, name); if ((fd = open(pathname, O_RDONLY)) == -1) { return -1; } if (lseek(fd, 0, SEEK_END) == -1) { err_log("lseek 错误"); my_err("lseek", __LINE__); } if ((len = lseek(fd, 0, SEEK_END)) == -1) { err_log("lseek 错误"); my_err("lseek", __LINE__); } if (lseek(fd, 0, SEEK_SET) == -1) { err_log("lseek 错误"); my_err("lseek", __LINE__); } if ((ret = read(fd, tmp, len)) < 0) { err_log("read 错误"); my_err("read", __LINE__); } close(fd); return (strncmp(tmp, passwd, strlen(passwd))); //若返回0,则表示用户名和密码都正确 }
int main(int argc, char *argv[]) { int fd; char write_buf[32] = "Hello world!"; //在当前目录下创建文件example_63.c //if ((fd = creat ("example_63.c", S_IRWXU)) == -1) { if ((fd = open ("example_63.c", O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) == -1) { my_err ("open", errno); } else { printf ("create filr success!\n"); } //写数据 if (write (fd, write_buf, strlen(write_buf)) != strlen(write_buf)) { my_err ("write", errno); } my_read (fd); //演示文件间隔 printf ("/*------------------------*/\n"); if (lseek (fd, 10, SEEK_END) == -1) { my_err ("lseek", errno); } if (write (fd, write_buf, strlen(write_buf)) != strlen(write_buf)) { my_err ("write", errno); } my_read (fd); close (fd); return EXIT_SUCCESS; }
int func_retr(t_ftp *ftp) { int i; socklen_t data_len; i = 0; data_len = sizeof(ftp->data_addr); if (ftp->log != 2) return (dprintf(ftp->pasv_socket, "530 login with USER and PASS.\r\n"), -1); if ((ftp->data_socket = accept(ftp->pasv_socket, (struct sockaddr *)&ftp->data_addr, &data_len)) == -1) { if (close(ftp->pasv_socket) == -1) return (-1); return (my_err("Problem with accept.\n")); } dprintf(ftp->client_socket, "150 File status okay; about to open data "); dprintf(ftp->client_socket, "connection.\r\n"); send_file(ftp, i); if (close(ftp->data_socket) == -1) return (-1); return (0); }
void send_file(t_ftp *ftp, int i) { char **tab; char *line; FILE *fd; size_t len; ssize_t read; line = NULL; len = 0; if ((tab = my_word_tab(ftp->cmd, 0, 0, 0)) == NULL) { my_err("550 Invalid command.\r\n"); return ; } i = size_tab(tab); if (i == 2) { fd = fopen(tab[1], "r"); while ((read = getline(&line, &len, fd)) != -1) write(ftp->data_socket, line, read); dprintf(ftp->client_socket, "226 Closing data connection.\r\n"); dprintf(ftp->client_socket, "Requested file action successful (for exam"); dprintf(ftp->client_socket, "ple, file transfer or file abort)..\r\n"); ftp->log = 1; } my_free(tab); }
/*输入用户名, 然后通过fd发送出去*/ void input_userinfo(int conn_fd, const char *string) { char input_buf[32]; char recv_buf[BUFSIZE]; int flag_userinfo; /*输入用户信息直到正确为止*/ do { printf("%s :",string); if (get_userinfo(input_buf, 32) < 0) { //调用get_userinfo()获取用户输入数据存入input_buf printf("error return from get_userinfo\n"); exit(1); } if (send(conn_fd, input_buf, strlen(input_buf), 0) < 0) { //在TCP套接字上发送数据,fd为套接字描述符,buf为要发送的缓冲区 my_err("send", __LINE__); } /*从连接套接字上读取一次数据*/ if(my_recv(conn_fd,recv_buf, sizeof(recv_buf)) < 0) { //从连接套接字上读取一次数据 printf("data is too long\n"); exit(1); } if(recv_buf[0] == VALID_USERINFO) { flag_userinfo = VALID_USERINFO; } else { printf("%s error,input again", string); //错误,将flag_userinfo置为用户信息无效 flag_userinfo = INVALID_USERINFO; } } while(flag_userinfo = INVALID_USERINFO); }
//send information bysl int SSL_PROCESS_SEND( SSL *ssl ) { int client_len; char send_buf[ MAXSIZE + 1 ]; bzero( send_buf,MAXSIZE + 1 ); if( fgets( send_buf, MAXSIZE, stdin ) == NULL) my_err( "Input",__LINE__ ); client_len = SSL_write( ssl, send_buf, strlen( send_buf ) ); if( client_len <= 0 ) { printf( "Send : %s error ! Error code : %d, Error Info : %s.\n ",send_buf, errno, strerror( errno ) ); return -1; } else { printf( "Send : %s Successfule ! Altogether send %d bytes.\n",send_buf, client_len ); if( strcmp( send_buf,"QUIT__\n" ) == 0 ) return 4; return 0; } }
void Process_Client( int connect_fd, struct sockaddr_in client ) { int ret = 0; char recv_buf[ MAXDATASIZE ],send_buf[MAXDATASIZE]; char Read_Buf[65535]; //receive the information of the clients request if( ( ret = recv( connect_fd,recv_buf, MAXDATASIZE, 0 ) ) < 0 ) my_err( "Receive",__LINE__ ); recv_buf[ ret - 1 ] = '\0'; int i = 0,j = 0; char path[128]; strcpy( path,"/var/www/html" ); j = strlen( "/var/www/html" ); for( i = 4; recv_buf[ i ] != ' ' ; i++,j++ ) path[ j ] = recv_buf[ i ]; Open_Html( Read_Buf,path ); send( connect_fd, Read_Buf, strlen( Read_Buf ),0 ); // printf( "\n%s\n",recv_buf ); // printf( "######################\n%s\n#######################",Read_Buf ); }
int main() { int sock_fd,connect_fd; int client_len; struct sockaddr_in client; pid_t pid; sock_fd = Make_Listen_Socket(); client_len = sizeof( client ); while( 1 ) { if( ( connect_fd = accept( sock_fd, ( struct sockaddr * )&client ,&client_len ) ) == -1 ) my_err( "Accept",__LINE__ ); //print some information printf( "\nAccept a new client.\n\t\tIP : %s\n",inet_ntoa( client.sin_addr ) ); //father and the son region if( ( pid = fork() ) > 0 ) { close( connect_fd ); continue; } else if( pid == 0 ) { close( sock_fd ); Process_Client( connect_fd, client ); } } return 0; }
//输入用户名 ,然后通过fd发送出去 void input_userinfo(int conn_fd ,const char *string) { char input_buf[32]; char recv_buf[BUFSIZE]; int flag_userinfo; //输入用户信息直到正确为止 do { printf("%s:" ,string); if(get_userinfo(input_buf ,32) < 0) { printf("errno return from get_userinfo"); exit(1); } if(send(conn_fd ,input_buf ,strlen(input_buf) ,0) < 0) { my_err("send" ,__LINE__); } //从连接套接字上读取一次数据 if(my_recv(conn_fd ,recv_buf ,sizeof(recv_buf)) < 0) { printf("data is too long\n"); exit(1); } if(recv_buf[0] == VALID_USERINFO) { flag_userinfo = VALID_USERINFO; } else { printf("%s errno ,input again,",string); flag_userinfo = INVALID_USERINFO; } } while(flag_userinfo == INVALID_USERINFO); }
/*接受客户端的连接*/ int server_accept (int *serv_fd, struct pollfd *poll_fd) { struct link_list *link_list_ptemp ; struct sockaddr_in sockaddr ; int sockaddr_len = sizeof (struct sockaddr_in) ; int client_fd ; if((client_fd = accept (*serv_fd, (struct sockaddr *)&sockaddr, &sockaddr_len)) < 0) { my_err ("accept", __LINE__) ; return -1 ; } printf ("new client_fd : %d\n", client_fd) ; /*将新套结字描述符加入监视列表*/ poll_fd -> fd = client_fd ; poll_fd -> events = POLLRDNORM ; /*将此连接套结字加入到连接用户链表中*/ link_list_ptemp = (struct link_list *)malloc (sizeof (struct link_list)) ; link_list_ptemp -> sock_fd = client_fd ; link_list_ptemp -> sockaddr.sin_family = sockaddr.sin_family ; link_list_ptemp -> sockaddr.sin_port = sockaddr.sin_port ; link_list_ptemp -> sockaddr.sin_addr.s_addr = sockaddr.sin_addr.s_addr ; link_list_PHEAD = link_list_add (link_list_PHEAD, link_list_ptemp) ; return 0 ; }
int my_recv(int conn_fd, char *data_buf, int len) { static char recv_buf[BUFSIZE]; static char *pread; static int len_remain = 0; int i; if(len_remain <= 0) { if((len_remain = recv(conn_fd, recv_buf, sizeof(recv_buf), 0)) < 0) { my_err("recv", __LINE__); } else if(len_remain == 0){ return 0; } pread = recv_buf; } for(i=0; *pread != '\n'; i++) { if(i > len){ return -1; } data_buf[i] = *pread ++; len_remain --; } len_remain--; pread++; return i; }