就现在所知,socket 通讯,传送字符串是最方便的选择。可以在服务端和客户端建立两个相同的结构体来实现这个。
发送和接收的buf里,如果不是用for遍历(使用sizeof(buf)),而是用cout或者printf()方法,只会打印第一个字段。
简单的socket 服务端:
#include <Winsock2.h>
#include <cstdio>
#include <iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
struct myMesg_char
{
char name[20] ;
char sex[5] ;
char watch[10] ;
char tall[5] ;
};
void main()
{
WSADATA wsaData;
SOCKET sockServer;
SOCKADDR_IN addrServer;
SOCKET sockClient;
SOCKADDR_IN addrClient;
WSAStartup(MAKEWORD(2, 2), &wsaData);
sockServer = socket(AF_INET, SOCK_STREAM, 0);
addrServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY); // 任何IP
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(20302); // 端口号
bind(sockServer, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
listen(sockServer, 5);
printf("server start:\nlisten...\n");
int len = sizeof(SOCKADDR);
char sendBuf[100];
char recvBuf[100] = {0};
sockClient = accept(sockServer, (SOCKADDR*)&addrClient, &len);
recv(sockClient, recvBuf, 100, 0);
cout << "recvBuf: " << recvBuf << endl;
for (int i = 0; i < sizeof(recvBuf); i++)
cout << i << " " << recvBuf[i] << endl;
myMesg_char atel = {0};
memcmp(atel.name, recvBuf, sizeof(atel));
cout << "\ntall : " << atel.name << endl;
cout << "atel.sex: " << atel.sex << endl;
cout << "atel.watch: " << atel.watch << endl;
closesocket(sockClient);
WSACleanup();
}
客户端:
#include <Winsock2.h>
#include <cstdio>
#include <WS2tcpip.h>
#include <iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
struct myMesg
{
char name[20] ;
char sex[5] ;
char watch[10];
float tall ;
};
struct myMesg_char
{
char name[20] ;
char sex[5] ;
char watch[10] ;
char tall[5] ;
};
void main()
{
WSADATA wsaData;
SOCKET sockClient;
SOCKADDR_IN addrServer;
WSAStartup(MAKEWORD(2, 2), &wsaData);
sockClient = socket(AF_INET, SOCK_STREAM, 0);
struct in_addr s;
//127.0.0.1
inet_pton(AF_INET, "127.0.0.1", (void *)&s);
addrServer.sin_addr.s_addr = s.s_addr;
addrServer.sin_family = AF_INET;
addrServer.sin_port = htons(20302);
connect(sockClient, (SOCKADDR*)&addrServer, sizeof(SOCKADDR));
myMesg atel ;
memset(&atel,0,sizeof(atel));
myMesg_char atel_char;
memset(&atel_char, 0, sizeof(atel_char));
sprintf_s(atel.name,"%s","SuperMan"); // sprintf_s() 和memcpy还是有很大差别的。
memcpy(atel.sex, "Man", sizeof(atel.sex));
memcpy(atel.watch, "SEIKO", sizeof(atel.watch));
atel.tall = 185.0f;
memcpy(atel_char.name, atel.name, sizeof(atel_char.name));
memcpy(atel_char.sex, atel.sex, sizeof(atel_char.sex));
memcpy(atel_char.watch, atel.watch, sizeof(atel_char.watch));
sprintf_s(atel_char.tall,"%4.0f",atel.tall);
char buff[50] = { 0 };
for (int i = 0; i < sizeof(buff); i++)
{
cout << "buff before: " << buff[i] << endl;
}
//memcpy(buff, &atel_char, sizeof(atel_char)); // 使用字符串结构体,非常实惠,另一端也是char可以直接memcpy复制。
memcpy(buff, &atel, sizeof(atel)); // 使用混合类型的结构体 float类型不好处理,最少不如用char实惠
for (int i = 0; i < sizeof(buff); i++)
{
cout << "buff after: " << buff[i] << endl; // 这样可以打印出所有内容,而不是只打印第一个字段。
}
send(sockClient, buff, sizeof(buff), 0);
memset(buff, '0', sizeof(buff));
closesocket(sockClient);
WSACleanup();
}
如果struct 里有整数和其他类型,也是可以的。但是char是最好的。