int start_listen() { int lis_sock; if((lis_sock = create_listen(g_option.smg_bind_port)) < 0) return lis_sock; printf("本地绑定端口[%d]成功\n", g_option.smg_bind_port); _beginthreadex(NULL, 0, smg_listen, (void *)lis_sock, 0, NULL); return 0; }
int ConnectionServer::Open(size_t thread_num, int port) { struct sockaddr_in addr; struct hostent *host; struct ifreq req; int sock; char *dn_or_ip; sock = socket(AF_INET, SOCK_DGRAM, 0); strncpy(req.ifr_name, "eth0", IFNAMSIZ); if ( ioctl(sock, SIOCGIFADDR, &req) < 0 ) { _err("ConnectionServer::Open ioctl error: %s Cann't get current IP\n", strerror (errno)); sprintf(current_ip,"Get currentip error %d",errno); } else { dn_or_ip = (char *)inet_ntoa(*(struct in_addr *) &((struct sockaddr_in *) &req.ifr_addr)->sin_addr); strcpy(current_ip,dn_or_ip); } _info("ConnectionServer::Open Current IP:%s\n", current_ip); shutdown(sock, 2); ::close(sock); if ((_epoll_fd = epoll_create(MAX_FD_NUM)) == -1) { _err("error: epoll create fail!!\n"); return -1; } if (create_pipe()) { _err("error: create pipe fail!! \n"); return -1; } if (create_listen(_socket_server_listen, port)) { _err("error: create listen fail !! %d\n",port); return -1; } add_input_fd(_pipe_read); add_input_fd(_socket_server_listen); return STaskBase<Connection*>::Open(thread_num); }
int StartPort(int port, int lnr_num, int lnr_len, int is_reused) { int count, i, socket_fd; pid_t pid; struct SHM_CONF * shm_ptr = NULL; count = GetPortShm(port, &shm_ptr); if (count <= 0) { WriteLog(0, 0, OUT_SCREEN, "ini文件在启动之后被修改过,端口[%d]不存在", port); return -1; } if (count != lnr_num) { WriteLog(0, 0, OUT_SCREEN, "ini文件在启动之后被修改过,端口[%d]进程个数错误", port); return -1; } if (shm_ptr == NULL) { WriteLog(0, 0, OUT_SCREEN, "ini文件在启动之后被修改过,端口[%d]对应的SHM为空", port); return -1; } socket_fd = create_listen(port, lnr_len, 1, is_reused); if (socket_fd <= 0) { WriteLog(0, 0, OUT_SCREEN, "在端口[%d]上建立监听失败,%d:%s", port, errno, strerror(errno)); return -1; } for (i = 0 ; i < lnr_num ; i++) { if (shm_ptr[i].run_status != RUN_STATUS_STOPED) { if (shm_ptr[i].proc_id == 0) { shm_ptr[i].run_status = RUN_STATUS_STOPED; ; } else { if (kill(shm_ptr[i].proc_id, 0) != 0) { shm_ptr[i].proc_id = 0; shm_ptr[i].run_status = RUN_STATUS_STOPED; } else { continue; } } } else { shm_ptr[i].proc_id = 0; shm_ptr[i].run_status = RUN_STATUS_STOPED; } pid = fork(); if (pid < 0) { continue; } if (pid == 0) { catch_all_singal(); LsnrRec(port, i, socket_fd); linker_cut(socket_fd, 0); FreeResource(); exit(0); } } linker_cut(socket_fd, 0); return 0; }