服务端发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//定义一个结构体 struct DataPackage { int age; char name[256]; }; //处理请求时,根据请求发出结构体的信息 if (...) { DataPackage dpkMsg {0,"No One"}; dpkMsg.age = 20; dpkMsg.name = "aet"; send(_clientSock,reinterpret_cast<const char *>(&dpkMsg),sizeof(DataPackage),0); } else { //do something } |
客户端接收
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//定义同样的结构体 struct DataPackage { int age; char name[256]; }; //请求服务器后,服务器发送信息过来,需要接收一下 char msgBuff[256] = {}; auto length = recv(client_sock, msgBuff, 256, 0); if (length > 0) { DataPackInfo * data_pack = reinterpret_cast<DataPackInfo*>(msgBuff); if(data_pack->age == -1) { cout << "Error is:" << data_pack->name << endl; } else { cout << "Age is:" << data_pack->age << endl; cout << "Name is:" << data_pack->name << endl; } } else { cout << "Invalid data!!!" << endl; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ Socket:相关记述11/09
- ♥ Socket:创建TCP服务端10/17
- ♥ Linux 高性能服务器编程:网络基础编程一11/27
- ♥ 51CTO:Linux C++网络编程五08/20
- ♥ Linux 高性能服务器编程:网络基础编程二11/28
- ♥ Socket基础:TCP篇10/16