???????????????о?????Socket??????????????????????????????????????????????????????????????????????????????????????
struct sockaddr_in server_addr;
pthread_mutex_t mut;
struct kevent events[10];
//IP??????????Э????
server_addr.sin_len = sizeof(struct sockaddr_in);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(ipAddress);
bzero(&(server_addr.sin_zero)??8);
//????socket
int server_sock_fd = socket(AF_INET?? SOCK_STREAM?? 0);
if (server_sock_fd == -1) {
perror("socket error");
return NULL;
}
int on=1;
if((setsockopt(server_sock_fd??SOL_SOCKET??SO_REUSEADDR??&on??sizeof(on)))<0)
{
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
//??socket
int bind_result = bind(server_sock_fd?? (struct sockaddr *)&server_addr?? sizeof(server_addr));
if (bind_result == -1) {
perror("bind error");
return NULL;
}
//????
if (listen(server_sock_fd?? BACKLOG) == -1) {
perror("listen error");
return NULL;
}
struct timespec timeout = {10??0};
//kqueue
int kq = kqueue();
if (kq == -1) {
perror("????kqueue????! ");
exit(1);
}
struct kevent event_change;
EV_SET(&event_change?? STDIN_FILENO?? EVFILT_READ?? EV_ADD?? 0?? 0?? NULL);
kevent(kq?? &event_change?? 1?? NULL?? 0?? NULL);
EV_SET(&event_change?? server_sock_fd?? EVFILT_READ?? EV_ADD?? 0?? 0?? NULL);
kevent(kq?? &event_change?? 1?? NULL?? 0?? NULL);
while (1) {
int ret = kevent(kq?? NULL?? 0?? events?? 10?? &timeout);
if (ret < 0) {
printf("kevent ????! ");
continue;
}else if(ret == 0){
printf("kenvent ???! ");
continue;
}else{
//ret > 0 ???????????events??
for (int i = 0; i < ret; i++) {
struct kevent current_event = events[i];
//kevent?е?ident???????????
if (current_event.ident == STDIN_FILENO) {
//???????
bzero(input_msg?? BUFFER_SIZE);
fgets(input_msg?? BUFFER_SIZE?? stdin);
//???? ".quit" ???????????
if (strcmp(input_msg?? QUIT_CMD) == 0) {
exit(0);
}
for (int i=0; i<CONCURRENT_MAX; i++) {
if (client_fds[i]!=0) {
send(client_fds[i]?? input_msg?? BUFFER_SIZE?? 0);
}
}
}else if(current_event.ident == server_sock_fd){
//???μ?????????
struct sockaddr_in client_address;
socklen_t address_len;
int client_socket_fd = accept(server_sock_fd?? (struct sockaddr *)&client_address?? &address_len);
if (client_socket_fd > 0) {
int index = -1;
for (int i = 0; i < CONCURRENT_MAX; i++) {
if (client_fds[i] == 0) {
index = i;
client_fds[i] = client_socket_fd;
break;
}
}
if (index >= 0) {
EV_SET(&event_change?? client_socket_fd?? EVFILT_READ?? EV_ADD?? 0?? 0?? NULL);
kevent(kq?? &event_change?? 1?? NULL?? 0?? NULL);
printf("??????(fd = %d)?????? %s:%d "??client_socket_fd??inet_ntoa(client_address.sin_addr)??ntohs(client_address.sin_port));
/*???????*/
strcpy(client.ipAddress?? inet_ntoa(client_address.sin_addr));
client.client_port = ntohs(client_address.sin_port);
client.client_ID = client_socket_fd;
/*???????????????洢???б???*/
//                            if (AddClient(client?? &clientList) == false) {
//                                fprintf(stderr?? "Problem allcating memory ");
//                                break;
//                            }
//                            if(ListIsFull(&clientList))
//                            {
//                                puts("The list is now full. ");
//                                break;
//                            }
//                            if(ListIsEmpty(&clientList))
//                            {
//                                printf("NO client Connection");
//                            }else
//                            {
//                                Traverse(&clientList?? showClients);
//                            }
//                            printf("????????????:%d "?? ListClientCount(&clientList));
if(!QueueIsFull(&line))
{
client.client_ID = client_socket_fd;
client.client_port = ntohs(client_address.sin_port);
strcpy(client.ipAddress?? inet_ntoa(client_address.sin_addr));
EnQueue(client?? &line); //?????????????????
char msg[250];
char wellcome[250];
sprintf(msg??"??????(fd = %d) ?????? %s:%d"??client_socket_fd??inet_ntoa(client_address.sin_addr)??ntohs(client_address.sin_port));
sprintf(wellcome?? "WellComm To NB Socket Server System your fd is %d addr is %s and port is %d"??client_socket_fd??inet_ntoa(client_address.sin_addr)??ntohs(client_address.sin_port));
//?????п????????????????????????????????
for(int i=0; i<QueueItemCount(&line); i++)
{
if(index != i)
{
send(client_fds[i]?? msg?? BUFFER_SIZE?? 0);
}else
{
send(client_fds[i]?? wellcome?? BUFFER_SIZE?? 0);
}
}
}
}else{
bzero(input_msg?? BUFFER_SIZE);
strcpy(input_msg?? "???????????????????????????????! ");
send(client_socket_fd?? input_msg?? BUFFER_SIZE?? 0);
printf("?????????????????????????????? %s:%d "??inet_ntoa(client_address.sin_addr)??ntohs(client_address.sin_port));
}
}
}else{
//????????????????????