本文的聊天工具,理论上来说是没有具体区分服务端和客户端的(只针对此处的点对点聊天工具,例如QQ,微信这种IM工具是有服务器端的,要不然咋会出现服务器登不上去的故障呢?),另外服务端和客户端除了socket连线地方略有不同之外(客户端主动连接服务器,服务端被动接受连接),其他的基本都是一致的,所以服务端和客户端融合就是很有必要的了,废话不多说,具体代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdlib.h>
#include <time.h>
#define MAXLINE 4096
#define ELELEN 100
#define SERV_PORT 5000
#define LISTENQ 5
struct im_msg {
char time[ELELEN];
char name[ELELEN];
char data[MAXLINE];
} *imsg;
void get_cur_time(char *time_buf) {
time_t ticks;
char *tmp = NULL;
char time_tmp[100];
size_t len;
memset(time_tmp, 0x00, 100);
ticks = time(NULL);
tmp = ctime(&ticks);
strcat(time_tmp, "[