整理一下老师讲的。
头文件link.h
#ifndef LINK_H
#define LINK_H
#include <stdio.h>
typedef struct Node
{
int iId;
int sockfd;
struct Node *pNext;
}Node;//节点
typedef struct List
{
int iLen;
Node *pFirstNode;
}List;//用于储存多个用户的链表
Node *makeNode();
List *makeList();
void insertList(List *pList, Node *pNode);
Node *findNode(const List *pList, int iId);
void showList(const List *pList);
#endif
头文件protocol.h
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <stdio.h>
typedef unsigned int uint;
enum ENUM_MSG_TYPE
{
ENUM_MSG_MIN = 0,
ENUM_MSG_PRIVATE_CHAT, //私聊
ENUM_MSG_GROUP_CHAT, //群聊
ENUM_MSG_REQUEST_USR_LIST, //请求用户列表
ENUM_MSG_RESPONSE_USR_LIST, //回复用户列表
ENUM_MSG_MAX = 0x0fffffff//保持占32位
};
typedef struct PDU
{
uint iPDULen; //整个协议数据单元的大小
uint iType; //消息类型:群聊,私聊,获取用户列表
uint iDataLen; //消息的长度
int iFromId; //消息的发送者
int iToId; //消息的接收者
char caData[4]; //要发送的消息
}PDU;
PDU *makePDU(int iDataLen);
uint parsePDU(PDU *pPDU);
#endif
客户端与服务器都要和这个文件一起编译 protocol.c
#include "protocol.h"
#include <stdlib.h>
#include <string.h>
PDU *makePDU(int iDataLen)//创建数据协议存储端
{
PDU *pPDU = NULL;
uint iLen = sizeof(PDU) - 4*sizeof(char) + iDataLen;
//四个成员的大小-4个可变长得到前面三个的长度+要发送数据的长度=整个结构体的大小
pPDU = (PDU *)malloc(iLen);
if (NULL != pPDU)
{
memset(pPDU, 0, iLen);//初始化
pPDU->iPDULen = iLen;//协议数据单元的大小
pPDU->iType = ENUM_MSG_MIN;//消息类型初始值
pPDU->iDataLen = iDataLen;
}
return pPDU;
}
uint parsePDU(PDU *pPDU)
{
uint iType = ENUM_MSG_MIN;
if (NULL != pPDU)
{
iType = pPDU->iType;
}
return iType;
}
服务器 server.c
/*socket()*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> //struct sockaddr_in
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include "link.h"
#include "protocol.h"
List *pList = NULL;
void sendPDUToClient(int sockfd, const PDU *pPDU)
{
if (sockfd < 0 || NULL == pPDU)
{
return;
}
int ret = -1;
int iLeft = pPDU->iPDULen;//剩余单元的大小
int iSended = 0;//已发送的人数
while (iLeft)
{
ret = write(sockfd, (char *)pPDU+iSended, iLeft);
if (-1 == ret)
{
if (EINTR == errno)
{
continue;
}
perror("write");
return;
}
iSended += ret;
iLeft -= ret;
}
}
void sendUsrListToClient(int sockfd, const List *pList)//把使用者链表发送给客户端
{
if (sockfd < 0 || NULL == pList)
{
return;
}
int iDataLen = pList->iLen * sizeof(int);
PDU *pPDU = makePDU(iDataLen);//一点一点拼接字节
if (NULL == pPDU)
{
return;
}
Node *pNode = pList->pFirstNode;
int i = 0;
while (NULL != pNode)
{
memcpy(pPDU->caData+i*sizeof(int)
, &(pNode->iId), sizeof(int));
i++;
pNode = pNode->pNext;
}
pPDU->iType = ENUM_MSG_RESPONSE_USR_LIST;
sendPDUToClient(sockfd, pPDU);//发送信息给客户端
}
void updateClientsUsrList(const List *pList)
{
if (NULL == pList)
{
return;
}
PDU *pPDU = makePDU(sizeof(int));
if (NULL == pPDU)
{
return;
}
memcpy(pPDU->caData, &(pList->pFirstNode->iId)
, sizeof(int));
pPDU->iType = ENUM_MSG_RESPONSE_USR_LIST;
Node *pNode = pList->pFirstNode->pNext;
while (NULL != pNode)
{
printf("id:%d, sockfd:%d\n", pNode->iId, pNode->sockfd);
sendPDUToClient(pNode->sockfd, pPDU);
pNode = pNode->pNext;
}
}
PDU *readDataFromClient(int sockfd)
{
int ret = -1;
uint iPDULen = 0;
ret = read(sockfd, &iPDULen, sizeof(uint));
if (-1 == ret)
{
perror("read");
return NULL;
}
PDU *pPDU = makePDU(iPDULen-3*sizeof(int));
if (NULL == pPDU)
{
return NULL;
}
pPDU->iPDULen = iPDULen;
int iLeft = iPDULen - sizeof(int);
int iReaded = sizeof(int);
while (iLeft)
{
ret = read(sockfd, (char *)pPDU+iReaded, iLeft);
if (-1 == ret)
{
if (EINTR == errno)
{
continue;
}
perror("while read");
return NULL;
}
iLeft -= ret;
iReaded += ret;
}
return pPDU;
}
void handleGroupChat(PDU *pPDU)//群发
{
if (NULL != pPDU)
{
Node *pNode = pList->pFirstNode;
while (NULL != pNode)
{
sendPDUToClient(pNode->sockfd, pPDU);
pNode = pNode->pNext;
}
}
}
void handlePrivateChat(PDU *pPDU)//私聊
{
if (NULL != pPDU)
{
Node *pNode = NULL;
pNode = findNode(pList, pPDU->iToId);
if (NULL != pNode)
{
sendPDUToClient(pNode->sockfd, pPDU);
}
}
}
void *handleClient(void *arg)//处理客户端发来的信息
{
int clientSockfd = (int)arg;
PDU *pPDU = NULL;
uint iType = 0;
while (1)
{
pPDU = readDataFromClient(clientSockfd);
if (NULL != pPDU)
{
iType = parsePDU(pPDU);
switch (iType)
{
case ENUM_MSG_REQUEST_USR_LIST:
break;
case ENUM_MSG_GROUP_CHAT:
handleGroupChat(pPDU);
break;
case ENUM_MSG_PRIVATE_CHAT:
handlePrivateChat(pPDU);
break;
default:
break;
}
}
}
}
int main(void)
{
pList = makeList();
if (NULL == pList)
{
return -1;
}
int iBaseId = 1001;
int sockfd = -1;
//创建socket描述符,用于监听接受客户端的连接
//AF_INET:ipv4
//SOCK_STREAM:tcp协议
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
return -1;
}
int ret = -1;
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET; //ipv4
serverAddr.sin_port = htons(8888); //port
//server ip
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//根据自己电脑的地址,ifconfig查询
bzero(&(serverAddr.sin_zero), 8);
//将sockfd和地址进行绑定
ret = bind(sockfd, (struct sockaddr *)&serverAddr
, sizeof(serverAddr));
if (-1 == ret)
{
perror("bind");
return -1;
}
//监听客户端的连接
//sockfd:监听的socket描述符
//5:表示未经过处理的连接请求队列可以容纳的最大数目
ret = listen(sockfd, 5);
if (-1 == ret)
{
perror("listen");
return -1;
}
struct sockaddr_in clientAddr;
int iLen = sizeof(clientAddr);
int clientSockfd = -1;
Node *pNode = NULL;
pthread_t thread;
while (1)
{
//阻塞等待客户端的连接,直到有客户端连接
//成功返回socket描述符,
//该socket描述符用于服务器与客户端进行通信
//失败返回-1
//sockfd:监听的socket描述符
//clientAddr:用于保存客户端的地址等信息
//iLen:地址长度变量指针
clientSockfd = accept(sockfd
, (struct sockaddr *)&clientAddr
, &iLen);
if (-1 == clientSockfd)
{
perror("accept");
return -1;
}
printf("there is a client connected...\n");
pthread_create(&thread, NULL, handleClient
, (void *)clientSockfd);
pNode = makeNode();
if (NULL == pNode)
{
continue;
}
pNode->iId = iBaseId;
pNode->sockfd = clientSockfd;
iBaseId++;
insertList(pList, pNode);
sendUsrListToClient(clientSockfd, pList);
updateClientsUsrList(pList);
}
return 0;
}
客户端 client.c
/*socket()*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> //struct sockaddr_in
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include "link.h"
#include "protocol.h"
#define MAX_DATA_LEN 64
List *pList = NULL;
uint g_iId = 0;
void getUsrListFromPDU(PDU *pPDU)
{
if (NULL == pPDU || NULL == pList)
{
return;
}
int iNum = pPDU->iDataLen / sizeof(int);
Node *pNode = NULL;
int i = 0;
while (iNum)
{
pNode = makeNode();
if (NULL == pNode)
{
return;
}
memcpy(&(pNode->iId), pPDU->caData+i*sizeof(int)
, sizeof(int));
insertList(pList, pNode);
i++;
iNum--;
}
if (0 == g_iId)
{
pNode = pList->pFirstNode;
while (NULL != pNode->pNext)
{
pNode = pNode->pNext;
}
g_iId = pNode->iId;
}
showList(pList);
}
PDU *readDataFromServer(int sockfd)
{
int ret = -1;
uint iPDULen = 0;
ret = read(sockfd, &iPDULen, sizeof(uint));
if (-1 == ret)
{
perror("read");
return NULL;
}
PDU *pPDU = makePDU(iPDULen-3*sizeof(int));
if (NULL == pPDU)
{
return NULL;
}
pPDU->iPDULen = iPDULen;
int iLeft = iPDULen - sizeof(int);
int iReaded = sizeof(int);
while (iLeft)
{
ret = read(sockfd, (char *)pPDU+iReaded, iLeft);
if (-1 == ret)
{
if (EINTR == errno)
{
continue;
}
perror("while read");
return NULL;
}
iLeft -= ret;
iReaded += ret;
}
return pPDU;
}
void showChatData(PDU *pPDU)
{
if (NULL != pPDU)
{
char caMsg[MAX_DATA_LEN] = {'\0'};
memcpy(caMsg, pPDU->caData, pPDU->iDataLen);
printf("%d says: %s\n", pPDU->iFromId, caMsg);
}
}
void *readThread(void *arg)
{
int sockfd = (int)arg;
PDU *pPDU = NULL;
uint iType = 0;
while (1)
{
pPDU = readDataFromServer(sockfd);
iType = parsePDU(pPDU);
switch (iType)
{
case ENUM_MSG_RESPONSE_USR_LIST:
getUsrListFromPDU(pPDU);
break;
case ENUM_MSG_GROUP_CHAT:
case ENUM_MSG_PRIVATE_CHAT:
showChatData(pPDU);
break;
default:
break;
}
}
return NULL;
}
void sendPDUToServer(int sockfd, const PDU *pPDU)
{
if (sockfd < 0 || NULL == pPDU)
{
return;
}
int ret = -1;
int iLeft = pPDU->iPDULen;
int iSended = 0;
while (iLeft)
{
ret = write(sockfd, (char *)pPDU+iSended, iLeft);
if (-1 == ret)
{
if (EINTR == errno)
{
continue;
}
perror("write");
return;
}
iSended += ret;
iLeft -= ret;
}
}
void readDataFromSTDIN(char caData[MAX_DATA_LEN])
{
memset(caData, '\0', MAX_DATA_LEN);
int ret = read(STDIN_FILENO, caData, MAX_DATA_LEN);
if (-1 == ret)
{
perror("read");
exit(0);
}
if ('\0' != caData[MAX_DATA_LEN-1]
&& '\n' != caData[MAX_DATA_LEN-1])
{
while ('\n' != getchar())
{}
}
caData[MAX_DATA_LEN-1] = '\0';
}
int main(void)
{
pList = makeList();
if (NULL == pList)
{
return -1;
}
int sockfd = -1;
//创建sockfd用于和服务器通信
//AF_INET: ipv4
//SOCK_STREAM:tcp
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
return -1;
}
int ret = -1;
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET; //ipv4
serverAddr.sin_port = htons(8888); //port
//server ip
serverAddr.sin_addr.s_addr = inet_addr("192.168.16.193");
bzero(&(serverAddr.sin_zero), 8);
//连接服务器
ret = connect(sockfd, (struct sockaddr *)&serverAddr
, sizeof(serverAddr));
if (-1 == ret)
{
perror("connect");
return -1;
}
pthread_t thread;
ret = pthread_create(&thread, NULL
, readThread, (void*)sockfd);
if (0 != ret)
{
printf("%s\n", strerror(ret));
return -1;
}
//write data to server
char caData[MAX_DATA_LEN] = {'\0'};
int iLen = 0;
PDU *pPDU = NULL;
int iToId = 0;
char caMsg[MAX_DATA_LEN] = {'\0'};
while (1)
{
//1002 hello
readDataFromSTDIN(caData);
memset(caMsg, '\0', MAX_DATA_LEN);
sscanf(caData, "%d%s", &iToId, caMsg);
iLen = strlen(caMsg);//取长度
if (iLen > 0)
{
pPDU = makePDU(iLen);
memcpy(pPDU->caData, caMsg, iLen);
pPDU->iFromId = g_iId;
if (0 == iToId)
{
pPDU->iType = ENUM_MSG_GROUP_CHAT;
}
else
{
pPDU->iToId = iToId;
pPDU->iType = ENUM_MSG_PRIVATE_CHAT;
}
sendPDUToServer(sockfd, pPDU);
}
}
return 0;
}
使用时修改一下客户端里的ip地址与开服务器的人机器对应,就可以实现简单对话和群聊。很多知识点不熟练,虽然看懂了但自己写还是很费劲。话说很多公司就专门需要做这方面的人啊,真希望我自己可以从头到尾写一个出来。