bzero(recv_msg, BUFFER_SIZE);
long byte_num = recv((int)current_event.ident,recv_msg,BUFFER_SIZE,0);
if (byte_num > 0) {
if (byte_num > BUFFER_SIZE) {
byte_num = BUFFER_SIZE;
}
recv_msg[byte_num] = '';
printf("客户端(fd = %d):%s ",(int)current_event.ident,recv_msg);
char delims[] = " ";
char *result = NULL;
result = strtok(recv_msg, delims);
int k = 0;
while( result != NULL ) {
if(k==0)
{
strcpy(tableName, result);
}else if (k == 1)
{
strcpy(userID, result);
}
k++;
printf( "result is "%s ", result );
result = strtok( NULL, delims );
}
sendMessage(client_fds[0], tableName, userID, user, &line);
break;
}else if(byte_num < 0){
printf("从客户端(fd = %d)接受消息出错. ",(int)current_event.ident);
}else{
EV_SET(&event_change, current_event.ident, EVFILT_READ, EV_DELETE, 0, 0, NULL);
kevent(kq, &event_change, 1, NULL, 0, NULL);
close((int)current_event.ident);
for (int i = 0; i < CONCURRENT_MAX; i++) {
if (client_fds[i] == (int)current_event.ident) {
Client *exitClient = (Client *)malloc(sizeof(Client));
exitClient->client_ID = (int)current_event.ident;
//                                selectClientWithFds((int)current_event.ident, &line);
//                                int count = selectAllCilent(&line);//查找所有在线客户端
//                           bool   del =  deleteClientFormList(&clientList, *exitClient);
//                                printf("%d ",del);
//                                Traverse(&clientList, showClients);
//                                if(!ListIsEmpty(&clientList))
//                                {
//                                    printf(" 排序后的元素 ");
//                                    int desc = DescClientFromList(&clientList);
//                                    Traverse(&clientList, showClients);
//                                    printf(" 逆置后的元素 ");
//                                    Reverse(&clientList);
//                                    Traverse(&clientList, showClients);
//                                }
//                                printf("在线客户端人数:%d ", ListClientCount(&clientList));
printf("客户端(fd = %d)退出了 ",(int)current_event.ident);
//                                printf("剩余在线客户端个数%d ",count);
client_fds[i] = 0;
break;
}
}
}
}
}
}
}    return NULL;
}
void showClients(Client client)
{
//    printf("客户端 IP: %s, port is %d, fds is %d ",client.ipAddress, client.port, client.fds);
}
//void sendClientStates(int clientCount,Queue *pq)
//{
//
//
//}
//对接收到的服务端消息进行处理,并且进行回应
void * sendMessage(int client_fds, char * message, char *userID, struct userTableInfo *userInfo, Queue *pq)
{
struct userMessage user = selectUserInfoWithUserID(message, userID);
printf("username is %s ", user.name);
send(client_fds, user.name, BUFFER_SIZE, 0);
send(client_fds, user.user_id, BUFFER_SIZE, 0);
send(client_fds, user.birthday, BUFFER_SIZE, 0);
//    pthread_mutex_unlock(&mut);
return NULL;
}
  以上是服务端部分代码,服务端将上线的客户端加入到链表中进行管理,当客户端上线时,服务端根据Socket的消息将客户端有关信息加入到链表中。

  由于所有操作都在同一台计算机中,所以IP地址都是相同的
  下面通过客户端对服务端发送相关指令获取对应的用户信息,该操作是基于客户端的指令,然后服务端通过相应指令对数据库进行查询,由于时间问题,目前该项目还不是很完善。

  由于代码都是基于C语言,可以在linux, mac, windows等环境运行。