总共有四种解决粘包的方法:
1.特殊字节结尾
2.包头加包长度
3.固定包大小
4.短连接
我们使用其中最常用的方法即包头加包长度:
下面请看代码
Server:
#include <QCoreApplication>
#include <winsock2.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//1.火锅 烤肉 中餐 -- WSAStartup()
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
//2.店长 --socket()
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
if(sock == INVALID_SOCKET)
{
WSACleanup();
return 1;
}
//3.找个地方 --bind()
sockaddr_in addrserver;
addrserver.sin_addr.S_un.S_addr = 0;
addrserver.sin_family = AF_INET;
addrserver.sin_port = htons(8899);
if(bind(sock,(sockaddr*)&addrserver,sizeof(addrserver)) == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
return 1;
}
//4.宣传 --listen()
if(listen(sock,3) == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
return 1;
}
//5.拉客人进门 交给服务员-- accept()
SOCKET sockWaiter = accept(sock,0,0);
char *pszbuf = nullptr;
int nReNum;
int nPackSize;
while(1)
{
//接收包大小
nReNum = recv(sockWaiter,(char*)&nPackSize,sizeof(int),0);
//接收包内容
if(nReNum <= 0 || nPackSize <= 0)
{
continue;
}
//6.等客人开口 --recv()
int offset = 0;
pszbuf = new char[nPackSize];
while(nPackSize)
{
nReNum = recv(sockWaiter,pszbuf + offset,nPackSize,0);
if(nReNum > 0)
{
offset += nReNum;
nPackSize -= nReNum;
}
}
cout << pszbuf << endl;
delete []pszbuf;
pszbuf = nullptr;
}
//8.服务员店长下班 --closesocket()
closesocket(sock);
//9.关门 --WSACleanup()
WSACleanup();
return a.exec();
}
Client:
#include <QCoreApplication>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//1.加载库
//2.socket()
//3.connect()
//4.send()
//5.recv()
//6.closesocket()
//7.WSACleanup()
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
return 1;
}
else
printf("The Winsock 2.2 dll was found okay\n");
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);
if(INVALID_SOCKET == sock)
{
WSACleanup();
return 1;
}
sockaddr_in addrserver;
addrserver.sin_family = AF_INET;
addrserver.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrserver.sin_port = htons(8899);
if(SOCKET_ERROR == connect(sock,(sockaddr*)&addrserver,sizeof(addrserver)))
{
closesocket(sock);
WSACleanup();
return 1;
}
char szbuf[1024] = {0};
while(1)
{
cin >> szbuf;
//发送包长度
int nSize = sizeof(szbuf);
send(sock,(char*)&nSize,sizeof(nSize),0);
//发送包内容
send(sock,szbuf,sizeof(szbuf),0);
int nReNum = recv(sock,szbuf,sizeof(szbuf),0);
if(nReNum > 0)
{
cout << "Server say :" <<szbuf << endl;
}
}
closesocket(sock);
WSACleanup();
return a.exec();
}